Add timeline rapport function

This commit is contained in:
Gregory Trullemans 2022-12-12 21:06:46 +01:00
parent 3dba301c05
commit 912fc1f9d9
1 changed files with 112 additions and 0 deletions

View File

@ -988,3 +988,115 @@ def generate_report(request, gymnast_id, season=None, week_number=None):
],
) # , font_config=font_config)
return response
@login_required
@require_http_methods(["GET"])
def generate_timeline_report(request, gymnast_id, season=None, week_number=None, date=None):
"""Génère une timeline pour un gymnaste. On va chercher tous les
- records (Chrono/ToF),
- points (compétition),
- nouveau apprentissage (learned skill)
- accident
- GymnastHasRoutine
eton les trie par date.
"""
gymnast = get_object_or_404(Gymnast, pk=gymnast_id)
if season is None:
date_begin = pendulum.now().date()
season, week_number = from_date_to_week_number(date_begin)
else:
date_begin, _ = from_week_number_to_date(season, week_number)
date_begin = date_begin.date()
selected_record = []
list_record_straightjumps = list(gymnast.chronos.filter(chrono_type=0).order_by("date"))
last_top_score = 0
for record in list_record_straightjumps:
if record.score > last_top_score:
last_top_score = record.score
selected_record.append(record)
list_record_first_routine = list(gymnast.chronos.filter(chrono_type=1).order_by("date"))
last_top_score = 0
for record in list_record_first_routine:
if record.score > last_top_score:
last_top_score = record.score
selected_record.append(record)
list_record_second_routine = list(gymnast.chronos.filter(chrono_type=2).order_by("date"))
last_top_score = 0
for record in list_record_second_routine:
if record.score > last_top_score:
last_top_score = record.score
selected_record.append(record)
list_record_third_routine = list(gymnast.chronos.filter(chrono_type=3).order_by("date"))
last_top_score = 0
for record in list_record_third_routine:
if record.score > last_top_score:
last_top_score = record.score
selected_record.append(record)
list_record_fourth_routine = list(gymnast.chronos.filter(chrono_type=4).order_by("date"))
last_top_score = 0
for record in list_record_fourth_routine:
if record.score > last_top_score:
last_top_score = record.score
selected_record.append(record)
list_record_fifth_routine = list(gymnast.chronos.filter(chrono_type=5).order_by("date"))
last_top_score = 0
for record in list_record_fifth_routine:
if record.score > last_top_score:
last_top_score = record.score
selected_record.append(record)
# print(selected_record)
list_record = selected_record
list_record.extend(list(gymnast.accident.all().order_by("date")))
# list_record.extend(list(gymnast.points.all().order_by("date")))
list_record.extend(list(gymnast.known_skills.all().order_by("date")))
# print(list_record)
sorted_records = sorted(list_record, key=lambda x: x.date)
# print(sorted_records)
context = {
"SITE_TITLE": settings.SITE_TITLE,
"CLUB_NAME": settings.CLUB_NAME,
"ADDRESS": settings.ADDRESS,
"CITY": settings.CITY,
"ZIP": settings.ZIP,
"HEAD_COACH": settings.HEAD_COACH,
"MOBILE_PHONE": settings.MOBILE_PHONE,
"HEAD_COACH_EMAIL": settings.HEAD_COACH_EMAIL,
"week_number": week_number,
"gymnast": gymnast,
"today": date_begin,
"season": season,
"sorted_records": sorted_records,
}
# return render(request, "people/gymnasts/reports/report_timeline.html", context)
response = HttpResponse(content_type="application/pdf")
response[
"Content-Disposition"
] = "attachment; filename={lastname}-{firstname}-report-timeline.pdf".format(
lastname=gymnast.last_name,
firstname=gymnast.first_name,
)
html = render_to_string("people/gymnasts/reports/report_timeline.html", context)
# font_config = FontConfiguration()
HTML(string=html, base_url=request.build_absolute_uri()).write_pdf(
response,
stylesheets=[
CSS(settings.STATICFILES_DIRS[0] + "/css/gymnast_report.css"),
CSS(settings.STATICFILES_DIRS[0] + "/css/black-dashboard_report.css"),
CSS(settings.STATICFILES_DIRS[0] + "/css/font_awesome_all_5.15.3.css"),
],
) # , font_config=font_config)
return response