Ultron/ultron/people/views.py

375 lines
12 KiB
Python
Raw Normal View History

2022-01-07 18:08:39 +01:00
2021-12-09 16:53:44 +01:00
from django.contrib.auth.decorators import login_required
from django.db.models import Q, F, Avg
from django.db.models.functions import TruncDay
from django.http import HttpResponseRedirect, JsonResponse
2022-01-07 18:08:39 +01:00
from django.shortcuts import render, get_object_or_404
from django.views.decorators.http import require_http_methods
import pendulum
2021-12-09 16:53:44 +01:00
from ultron.followup.models import Event
from ultron.followup.forms import GymnastHasRoutineForm
2021-12-19 09:30:51 +01:00
from ultron.followup.models import (
Chrono,
LearnedSkill,
MindState,
Skill,
Point,
Accident,
HeightWeight,
NumberOfRoutineDone,
2021-12-19 09:30:51 +01:00
)
2021-12-09 16:53:44 +01:00
2022-01-07 18:08:39 +01:00
from .models import Gymnast
from .forms import GymnastForm
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET"])
def gymnast_lookup(request):
"""
Récupère la liste des gymnastes à la volée suivant des caractères de
recherche entrés. (min 3 caractères)
"""
results = []
pattern = request.GET.get("pattern", None)
# Ignore queries shorter than length 3
if pattern is not None and len(pattern) > 3:
model_results = Gymnast.objects.filter(
Q(last_name__icontains=pattern) | Q(first_name__icontains=pattern)
)
results = [{"ID": x.id, "Name": str(x)} for x in model_results]
# json = simplejson.dumps(results)
# return HttpResponse(json, content_type="application/json")
return JsonResponse(results, safe=False)
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET"])
def gymnast_listing(request):
"""
Liste tous les gymnasts connus
"""
gymnast_list = Gymnast.objects.all()
2021-12-19 09:30:51 +01:00
context = {"gymnast_list": gymnast_list}
return render(request, "people/gymnasts/list.html", context)
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET"])
def gymnast_details(request, gymnast_id, tab=None):
2021-12-09 16:53:44 +01:00
"""
Récupère toutes les informations d'un gymnaste.
"""
2021-12-19 09:30:51 +01:00
gymnast = get_object_or_404(Gymnast, pk=gymnast_id)
2021-12-09 16:53:44 +01:00
2021-12-19 09:30:51 +01:00
gymnast_nb_known_skills = gymnast.known_skills.distinct(
"skill"
).count() # devrait disparaitre
2021-12-09 16:53:44 +01:00
nb_skill = Skill.objects.all().count()
2021-12-19 09:30:51 +01:00
nb_known_skill = (
LearnedSkill.objects.filter(gymnast=gymnast_id).distinct("skill").count()
)
2022-01-18 09:35:37 +01:00
if nb_skill != 0:
percentage_known_skill = (nb_known_skill / nb_skill) * 100
else:
percentage_known_skill = 0
2021-12-19 09:30:51 +01:00
chronos_list = Chrono.objects.filter(gymnast=gymnast_id).order_by("-date")[:10]
straightjump_score = (
2022-01-07 18:08:39 +01:00
Chrono.objects.filter(gymnast=gymnast_id)
.filter(chrono_type=0)
.order_by("-date")
2021-12-19 09:30:51 +01:00
)
best_straightjump = (
2022-01-07 18:08:39 +01:00
Chrono.objects.filter(gymnast=gymnast_id)
.filter(chrono_type=0)
.order_by("-score")[:1]
2021-12-19 09:30:51 +01:00
)
best_routine = (
2022-01-07 18:08:39 +01:00
Chrono.objects.filter(gymnast=gymnast_id)
.filter(chrono_type=1)
.order_by("-score")[:1]
2021-12-19 09:30:51 +01:00
)
2021-12-13 11:26:44 +01:00
2022-01-07 19:28:33 +01:00
# nb_unknown_skill = nb_skill - gymnast_nb_known_skills
2021-12-09 16:53:44 +01:00
context = {
"gymnast": gymnast,
"gymnast_nb_known_skills": gymnast_nb_known_skills,
"chronos_list": chronos_list,
"straightjump_score": straightjump_score,
"best_routine": best_routine,
"best_straightjump": best_straightjump,
"nb_skill": nb_skill,
"nb_known_skill": nb_known_skill,
"percentage_known_skill": percentage_known_skill,
"tab": tab,
}
return render(request, "people/gymnasts/details.html", context)
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET"])
2021-12-18 22:40:15 +01:00
def gymnast_display_event(request, gymnast_id):
2021-12-09 16:53:44 +01:00
"""
Renvoie deux listes d'évènements : ceux à venir et ceux passés.
"""
today = pendulum.now().date()
2021-12-18 22:40:15 +01:00
next_event_list = Event.objects.filter(gymnasts=gymnast_id, datebegin__gte=today)
2021-12-19 09:30:51 +01:00
previous_event_list = Event.objects.filter(
gymnasts=gymnast_id, datebegin__lte=today
)
2021-12-09 16:53:44 +01:00
context = {
"next_event_list": next_event_list,
"previous_event_list": previous_event_list,
}
return render(request, "people/gymnasts/list_event.html", context)
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET"])
2021-12-18 22:40:15 +01:00
def gymnast_display_accident(request, gymnast_id):
2021-12-09 16:53:44 +01:00
"""
Renvoie deux listes d'évènements : ceux à venir et ceux passés.
"""
2021-12-18 22:40:15 +01:00
accident_list = Accident.objects.filter(gymnast=gymnast_id)
2021-12-09 16:53:44 +01:00
2021-12-19 09:30:51 +01:00
context = {"accident_list": accident_list, "gymnast_id": gymnast_id}
2021-12-09 16:53:44 +01:00
return render(request, "people/gymnasts/list_accident.html", context)
2021-12-09 16:53:44 +01:00
2021-12-13 15:51:07 +01:00
@login_required
@require_http_methods(["GET"])
def gymnast_display_physiological(request, gymnast_id):
2021-12-13 15:51:07 +01:00
"""
Renvoie les listes des tailles/poids, état d'esprit et accidents.
"""
2021-12-29 14:40:34 +01:00
accident_list = Accident.objects.filter(gymnast=gymnast_id).order_by("date")
mindstate_list = MindState.objects.filter(gymnast=gymnast_id).order_by("date")
2021-12-19 09:30:51 +01:00
height_weight_list = HeightWeight.objects.filter(gymnast=gymnast_id).order_by(
2021-12-29 14:40:34 +01:00
"date"
2021-12-19 09:30:51 +01:00
)
2021-12-13 15:51:07 +01:00
context = {
2021-12-19 09:30:51 +01:00
"accident_list": accident_list,
"mindstate_list": mindstate_list,
"height_weight_list": height_weight_list,
"gymnast_id": gymnast_id,
2021-12-13 15:51:07 +01:00
}
return render(request, "people/gymnasts/physiological_followup.html", context)
2021-12-13 15:51:07 +01:00
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET"])
2021-12-18 22:40:15 +01:00
def gymnast_display_scores_chrono(request, gymnast_id):
2021-12-09 16:53:44 +01:00
"""
Selectionne tous les scores réalisés par le gymnaste
"""
2021-12-18 22:40:15 +01:00
score_list = Point.objects.filter(gymnast=gymnast_id).order_by("-event__datebegin")
2021-12-09 16:53:44 +01:00
score_routine1_list = score_list.filter(routine_type=0)
score_routine2_list = score_list.filter(routine_type=1)
score_routine3_list = score_list.filter(routine_type=2)
2021-12-18 22:40:15 +01:00
chrono_list = Chrono.objects.filter(gymnast=gymnast_id).order_by("date")
chrono_10c = Chrono.objects \
.filter(gymnast=gymnast_id) \
.filter(chrono_type=0) \
.values('date') \
.annotate(score_avg=Avg('score')) \
.order_by('date')
chrono_r1 = Chrono.objects \
.filter(gymnast=gymnast_id) \
.filter(chrono_type=1) \
.values('date') \
.annotate(score_avg=Avg('score')) \
.order_by('date')
chrono_r2 = Chrono.objects \
.filter(gymnast=gymnast_id) \
.filter(chrono_type=2) \
.values('date') \
.annotate(score_avg=Avg('score')) \
.order_by('date')
chrono_rf = Chrono.objects \
.filter(gymnast=gymnast_id) \
.filter(chrono_type=3) \
.values('date') \
.annotate(score_avg=Avg('score')) \
.order_by('date')
2021-12-09 16:53:44 +01:00
context = {
"score_list": score_list,
"score_routine1_list": score_routine1_list,
"score_routine2_list": score_routine2_list,
"score_routine3_list": score_routine3_list,
"chrono_list": chrono_list,
"chrono_10c": chrono_10c,
"chrono_r1": chrono_r1,
"chrono_r2": chrono_r2,
"chrono_rf": chrono_rf,
2021-12-18 22:40:15 +01:00
"gymnast_id": gymnast_id,
2021-12-09 16:53:44 +01:00
}
return render(request, "people/gymnasts/list_scores_chronos.html", context)
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET"])
2021-12-18 22:40:15 +01:00
def gymnast_display_mindstate(request, gymnast_id):
2021-12-09 16:53:44 +01:00
"""
Selectionne tous les scores réalisés par le gymnaste
"""
2021-12-18 22:40:15 +01:00
mindstate_list = MindState.objects.filter(gymnast=gymnast_id).order_by("-date")
2021-12-09 16:53:44 +01:00
context = {
"mindstate_list": mindstate_list,
2021-12-18 22:40:15 +01:00
"gymnast_id": gymnast_id,
2021-12-09 16:53:44 +01:00
}
return render(request, "people/gymnasts/list_mindstate.html", context)
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET"])
2021-12-18 22:40:15 +01:00
def gymnast_display_routines(request, gymnast_id):
2021-12-09 16:53:44 +01:00
"""
Tag affichant les séries d'un gymnaste.
"""
2021-12-18 22:40:15 +01:00
gymnast = get_object_or_404(Gymnast, pk=gymnast_id)
2022-01-07 18:08:39 +01:00
ghr_list = gymnast.has_routine.prefetch_related("routine").filter(
dateend__isnull=True
)
routine_done_list = NumberOfRoutineDone.objects.filter(gymnast=gymnast_id).order_by(
"-date"
)[:8]
2022-01-07 18:08:39 +01:00
context = {
"ghr_list": ghr_list,
"routine_done_list": routine_done_list,
"gymnast_id": gymnast_id,
}
return render(request, "people/gymnasts/list_routine.html", context)
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET", "POST"])
2021-12-18 22:40:15 +01:00
def link_routine_to_gymnast(request, gymnast_id=None):
2021-12-19 09:30:51 +01:00
""" """
2021-12-09 16:53:44 +01:00
2021-12-18 22:40:15 +01:00
if gymnast_id:
gymnast = get_object_or_404(Gymnast, pk=gymnast_id)
data = {"gymnast": gymnast_id, "gymnast_related": str(gymnast), "dateend": None}
2021-12-09 16:53:44 +01:00
else:
gymnast = None
data = {"dateend": None}
if request.method == "POST":
form = GymnastHasRoutineForm(request.POST)
if form.is_valid():
form.save()
2021-12-18 22:40:15 +01:00
if gymnast_id is not None:
return HttpResponseRedirect("/gymnast/details/" + str(gymnast_id) + "/")
2021-12-09 16:53:44 +01:00
2022-01-07 18:08:39 +01:00
return HttpResponseRedirect("/gymnast/")
2021-12-09 16:53:44 +01:00
else:
form = GymnastHasRoutineForm(instance=gymnast, initial=data)
2021-12-18 22:40:15 +01:00
context = {"form": form, "gymnast_id": gymnast_id}
return render(request, "people/gymnasts/link_to_routine.html", context)
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET", "POST"])
2021-12-18 22:40:15 +01:00
def gymnast_create_or_update(request, gymnast_id=None):
2021-12-09 16:53:44 +01:00
"""
Formulaire de creation et modification d'un gymnaste.
"""
2021-12-18 22:40:15 +01:00
if gymnast_id:
gymnast = get_object_or_404(Gymnast, pk=gymnast_id)
2021-12-19 09:30:51 +01:00
data = {"club_related": gymnast.club}
2021-12-09 16:53:44 +01:00
else:
gymnast = None
data = {}
if request.method == "POST":
gymnast_form = GymnastForm(request.POST, instance=gymnast)
if gymnast_form.is_valid():
gymnast = gymnast_form.save()
return HttpResponseRedirect("/gymnast/details/" + str(gymnast.id))
2022-01-07 19:28:33 +01:00
print(gymnast_form.errors)
2021-12-09 16:53:44 +01:00
form = GymnastForm(instance=gymnast, initial=data)
2021-12-18 22:40:15 +01:00
context = {"form": form, "gymnast_id": gymnast_id}
return render(request, "people/gymnasts/create.html", context)
2021-12-09 16:53:44 +01:00
@login_required
@require_http_methods(["GET"])
2021-12-18 22:40:15 +01:00
def gymnast_display_skill(request, gymnast_id):
2022-01-07 19:28:33 +01:00
"""Tag affichant les statistiques de skill d'un gymnaste
Le nombre de saut qu'il sait faire (total, par niveau, par rank, …), calcule la complétude, …
2021-12-09 16:53:44 +01:00
.. todo:: Générer UNE fois la liste de skill que le gymnaste ne sait pas faire (1 query)
et les counts puis, dans le template on parcourt plusieurs fois cette même liste mais on
affiche conditionnellement (par age, par rank, ...)
"""
context = {}
2021-12-18 22:40:15 +01:00
gymnast = get_object_or_404(Gymnast, pk=gymnast_id)
2021-12-19 09:30:51 +01:00
gymnast_nb_known_skills = gymnast.known_skills.distinct("skill").count()
2021-12-09 16:53:44 +01:00
context = gymnast.get_informations_from_level()
context.update(gymnast.get_informations_from_rank())
2021-12-19 09:30:51 +01:00
planified_skill = (
Skill.objects.filter(plan__gymnast=gymnast.id)
.order_by("-plan__date")
.annotate(plan_date=F("plan__date"))
)
2021-12-18 22:40:15 +01:00
context["planified_skill"] = planified_skill
2021-12-09 16:53:44 +01:00
if gymnast.gender:
2021-12-19 09:30:51 +01:00
context["skill_by_age"] = Skill.objects.filter(
2022-01-09 16:08:54 +01:00
age_girl_masterised__lte=gymnast.age
2021-12-19 09:30:51 +01:00
).exclude(known_by__gymnast=gymnast.id)
2021-12-09 16:53:44 +01:00
else:
2021-12-19 09:30:51 +01:00
context["skill_by_age"] = Skill.objects.filter(
2022-01-09 16:08:54 +01:00
age_boy_masterised__lte=gymnast.age
2021-12-19 09:30:51 +01:00
).exclude(known_by__gymnast=gymnast.id)
skill_whith_help = (
Skill.objects.filter(known_by__gymnast=gymnast_id, known_by__cando=1)
.exclude(known_by__gymnast=gymnast.id, known_by__cando__gte=2)
.distinct()
)
skill_not_chained = (
Skill.objects.filter(known_by__gymnast=gymnast_id, known_by__cando=2)
.exclude(known_by__gymnast=gymnast.id, known_by__cando=3)
.distinct()
)
2021-12-09 16:53:44 +01:00
context["gymnast"] = gymnast
context["skill_whith_help"] = skill_whith_help
context["skill_not_chained"] = skill_not_chained
context["gymnast_nb_known_skills"] = gymnast_nb_known_skills
return render(request, "people/gymnasts/list_skill.html", context)