|
2 | 2 |
|
3 | 3 | This modules contains the root Blueprint
|
4 | 4 | """
|
5 |
| -from flask import redirect, url_for, Blueprint |
6 |
| -from flask import render_template |
| 5 | +from flask import redirect, url_for, Blueprint, send_file |
| 6 | +from flask import render_template, request |
7 | 7 | from flask_login import current_user, login_required
|
8 | 8 |
|
9 | 9 | from collectives.forms.auth import LegalAcceptation
|
| 10 | +from collectives.forms.stats import StatisticsParametersForm |
| 11 | +from collectives.forms import csrf |
10 | 12 | from collectives.models import db, Configuration
|
11 | 13 | from collectives.utils.time import current_time
|
| 14 | +from collectives.utils.stats import StatisticsEngine |
12 | 15 |
|
13 | 16 |
|
14 | 17 | blueprint = Blueprint("root", __name__)
|
@@ -36,3 +39,30 @@ def legal_accept():
|
36 | 39 | db.session.add(current_user)
|
37 | 40 | db.session.commit()
|
38 | 41 | return redirect(url_for("root.legal"))
|
| 42 | + |
| 43 | + |
| 44 | +@blueprint.route("/stats") |
| 45 | +@csrf.exempt |
| 46 | +@login_required |
| 47 | +def statistics(): |
| 48 | + """Displays site event statistics.""" |
| 49 | + form = StatisticsParametersForm(formdata=request.args) |
| 50 | + if form.validate(): |
| 51 | + if form.activity_id.data == form.ALL_ACTIVITIES: |
| 52 | + engine = StatisticsEngine(year=form.year.data) |
| 53 | + else: |
| 54 | + engine = StatisticsEngine( |
| 55 | + activity_id=form.activity_id.data, year=form.year.data |
| 56 | + ) |
| 57 | + else: |
| 58 | + engine = StatisticsEngine(year=StatisticsParametersForm().year.data) |
| 59 | + |
| 60 | + if "excel" in request.args: |
| 61 | + return send_file( |
| 62 | + engine.export_excel(), |
| 63 | + mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| 64 | + download_name="Statistiques Collectives.xlsx", |
| 65 | + as_attachment=True, |
| 66 | + ) |
| 67 | + |
| 68 | + return render_template("stats/stats.html", engine=engine, form=form) |
0 commit comments