from django.shortcuts import render, get_object_or_404 from django.contrib.auth.decorators import login_required from django.views.decorators.http import require_http_methods from django.http import HttpResponse, HttpResponseRedirect from django.db.models import Q from ultron.people.models import Gymnast from ultron.planning.models import Event from ultron.objective.models import Skill from .models import Chrono, MindState, Point, Accident, LearnedSkill, HeightWeight from .forms import ( ChronoForm, LearnedSkillForm, ScoreForm, AccidentForm, MindStateForm, HeightWeightForm, ) from datetime import datetime import simplejson @login_required @require_http_methods(["GET"]) def chrono_listing(request, gymnast_id=None): """Récupère la liste des chronos""" if gymnast_id: chrono_list = Chrono.objects.filter(gymnast=gymnast_id) else: chrono_list = Chrono.objects.all() context = {"chrono_list": chrono_list} return render(request, "followup/chronos/list.html", context) @login_required @require_http_methods(["GET", "POST"]) def chrono_create_or_update(request, chronoid=None, gymnast_id=None): """Création ou modification d'un chrono""" if chronoid: chrono = get_object_or_404(Chrono, pk=chronoid) data = { "gymnast": chrono.gymnast.id, "gymnast_related": str(chrono.gymnast), } else: chrono = None data = None if request.method == "POST": form = ChronoForm(request.POST, instance=chrono) if form.is_valid(): new_chrono = form.save(commit=False) # Calcul du ToF if new_chrono.score_type == 1: new_chrono.tof = new_chrono.score else: tof = round((new_chrono.score * 13) / 15, 3) * 1000 tof = tof - (tof % 5) new_chrono.tof = tof / 1000 new_chrono.save() return HttpResponseRedirect( "/gymnast/details/" + str(new_chrono.gymnast.id) + "/" ) else: if data is None and gymnast_id is not None: gymnast = get_object_or_404(Gymnast, pk=gymnast_id) data = {"gymnast": gymnast_id, "gymnast_related": gymnast} form = ChronoForm(instance=chrono, initial=data) context = {"form": form, "chronoid": chronoid} return render(request, "followup/chronos/create.html", context) @login_required @require_http_methods(["POST"]) def gymnast_learn_skill(request): """ Lie un gymnast à une figure. """ # print(request) # utiliser un FORM pour cette fonction. gymnast_id = request.POST.get("gymnast_id", None) skillid = request.POST.get("skillid", None) cando = request.POST.get("cando", 0) if gymnast_id and skillid: gymnast = Gymnast.objects.get(pk=gymnast_id) skill = Skill.objects.get(pk=skillid) ls = LearnedSkill( gymnast=gymnast, skill=skill, cando=cando, date=datetime.now() ) ls.save() return HttpResponse(status=200) else: print("Erreur : impossible de lier le gymnaste à la figure.") return HttpResponse(status=500) @login_required @require_http_methods(["GET", "POST"]) def learnedskill_create_or_update(request, gymnast_id=None): """ Formulaire de creation et modification d'un lien skill/gymnaste. """ if gymnast_id: gymnast = get_object_or_404(Gymnast, pk=gymnast_id) data = { "gymnast": gymnast.id, "gymnast_related": str(gymnast), } else: data = {} if request.method == "POST": learnedskill_form = LearnedSkillForm(request.POST) if learnedskill_form.is_valid(): learnedskill_form.save() return HttpResponseRedirect( "/gymnast/details/" + str(learnedskill_form.cleaned_data["gymnast"].id) ) else: print(learnedskill_form.errors) form = LearnedSkillForm(initial=data) context = {"form": form, "gymnast_id": gymnast_id} return render(request, "followup/learnedskills/create.html", context) @login_required @require_http_methods(["GET", "POST"]) def score_create_or_update(request, scoreid=None, gymnast_id=None): """ Formulaire de création d'un nouveau score. """ if scoreid: score = get_object_or_404(Point, pk=scoreid) data = { "gymnast_related": str(score.gymnast), "event_related": str(score.event), } else: score = None data = {} if gymnast_id is not None: gymnast = get_object_or_404(Gymnast, pk=gymnast_id) data["gymnast"] = gymnast_id data["gymnast_related"] = str(gymnast) if request.method == "POST": form = ScoreForm(request.POST, instance=score) if form.is_valid(): # print(form.cleaned_data) form.save() return HttpResponseRedirect( # "/gymnast/" + str(form.cleaned_data["gymnast"].id) + "/tab/scores/" "/gymnast/details/" + str(form.cleaned_data["gymnast"].id) + "/" ) # return HttpResponseRedirect("/score/") else: print(form.errors) else: form = ScoreForm(instance=score, initial=data) context = {"form": form, "scoreid": scoreid} return render(request, "followup/scores/create.html", context) @login_required @require_http_methods(["GET"]) def score_listing(request, gymnast_id=None): """ Revoie la liste des scores """ pattern = request.GET.get("pattern", None) if pattern: score_list = Point.objects.filter( Q(event__icontains=pattern) | Q(gymnast__icontains=pattern) ) elif gymnast_id: score_list = Point.objects.filter(gymnast=gymnast_id) else: score_list = Point.objects.all() context = {"score_list": score_list} return render(request, "followup/scores/list.html", context) @login_required @require_http_methods(["GET"]) def accident_listing(request): """ Récupère la liste des accidents suivant un pattern si celui-ci est définit. """ pattern = request.GET.get("pattern", None) if pattern: accident_list = Accident.objects.filter( Q(gymnast__lastname__icontains=pattern) | Q(gymnast__firstname__icontains=pattern) ) else: accident_list = Accident.objects.all() context = {"accident_list": accident_list} return render(request, "followup/accidents/list.html", context) @login_required @require_http_methods(["GET", "POST"]) def accident_create_or_update(request, accidentid=None, gymnast_id=None): """ Formulaire de création d'un nouvel accident. """ if accidentid: accident = get_object_or_404(Accident, pk=accidentid) data = { "gymnast_related": accident.gymnast, "skill_related": accident.skill, } else: accident = None data = {} if gymnast_id is not None: gymnast = get_object_or_404(Gymnast, pk=gymnast_id) data["gymnast"] = gymnast_id data["gymnast_related"] = str(gymnast) if request.method == "POST": form = AccidentForm(request.POST, instance=accident) if form.is_valid(): form.save() if accidentid: return HttpResponseRedirect("/accident/" + str(accidentid) + "/") else: return HttpResponseRedirect("/accident/") else: form = AccidentForm(instance=accident, initial=data) context = {"form": form, "accidentid": accidentid} return render(request, "followup/accidents/create.html", context) @login_required @require_http_methods(["GET"]) def accident_detail(request, accidentid): """ Récupère toutes les informations d'un accident. """ accident = get_object_or_404(Accident, pk=accidentid) context = {"accident": accident} return render(request, "followup/accidents/details.html", context) @login_required @require_http_methods(["GET"]) def mindstate_listing(request, gymnast_id=None): """ Récupère la liste des evaluations mentales suivant (d'un gymnast si définit en paramètre). """ if gymnast_id: mindstate_list = MindState.objects.filter(gymnast=gymnast_id) else: mindstate_list = MindState.objects.all() context = {"mindstate_list": mindstate_list} return render(request, "followup/mindstates/list.html", context) @login_required @require_http_methods(["GET", "POST"]) def mindstate_create_or_update(request, mindstateid=None, gymnast_id=None, eventid=None): """ Formulaire de création d'un nouvel accident. """ if mindstateid: mindstate = get_object_or_404(MindState, pk=mindstateid) data = {"gymnast_related": mindstate.gymnast, "event_related": mindstate.event} else: mindstate = None data = {} if gymnast_id is not None: gymnast = get_object_or_404(Gymnast, pk=gymnast_id) data["gymnast"] = gymnast_id data["gymnast_related"] = str(gymnast) if eventid is not None: event = get_object_or_404(Event, pk=eventid) data["event"] = eventid data["event_related"] = str(event) if request.method == "POST": form = MindStateForm(request.POST, instance=mindstate) if form.is_valid(): form.save() if mindstateid: return HttpResponseRedirect("/mindstate/" + str(mindstateid) + "/") else: return HttpResponseRedirect("/mindstate/") else: form = MindStateForm(instance=mindstate, initial=data) context = {"form": form, "mindstateid": mindstateid} return render(request, "followup/mindstates/create.html", context) @login_required @require_http_methods(["GET"]) def mindstate_detail(request, mindstateid): """ Récupère toutes les informations d'une évaluation psychologique. """ mindstate = get_object_or_404(MindState, pk=mindstateid) context = {"mindstate": mindstate} return render(request, "followup/mindstates/details.html", context) @login_required @require_http_methods(["GET"]) def heightweight_listing(request, gymnast_id=None): """ Récupère la liste des couples taille/poids suivant (d'un gymnast si définit en paramètre). """ if gymnast_id: heightweight_list = HeightWeight.objects.filter(gymnast=gymnast_id) else: heightweight_list = HeightWeight.objects.all() context = {"heightweight_list": heightweight_list} return render(request, "followup/heightweight/list.html", context) @login_required @require_http_methods(["GET", "POST"]) def heightweight_create_or_update(request, heightweightid=None, gymnast_id=None): """ Formulaire de creation et modification d'un couple taille/couple. """ if heightweightid: heightweight = get_object_or_404(HeightWeight, pk=heightweightid) data = {"gymnast_related": heightweight.gymnast} else: heightweight = None data = {} if gymnast_id: gymnast = get_object_or_404(Gymnast, pk=gymnast_id) data["gymnast"] = gymnast_id data["gymnast_related"] = str(gymnast) if request.method == "POST": height_weight_form = HeightWeightForm(request.POST, instance=heightweight) if height_weight_form.is_valid(): height_weight_form.save() return HttpResponseRedirect( "/gymnast/details/" + str(height_weight_form.cleaned_data["gymnast"].id) ) else: print(height_weight_form.errors) form = HeightWeightForm(instance=heightweight, initial=data) context = {"form": form, "gymnast_id": gymnast_id, "heightweightid": heightweightid} return render(request, "followup/heightweight/create.html", context)