Ultron/ultron/followup/views.py

367 lines
11 KiB
Python

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):
"""Récupère la liste des chronos"""
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, gymnastid=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 gymnastid is not None:
gymnast = get_object_or_404(Gymnast, pk=gymnastid)
data = {"gymnast": gymnastid, "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.
gymnastid = request.POST.get("gymnastid", None)
skillid = request.POST.get("skillid", None)
cando = request.POST.get("cando", 0)
if gymnastid and skillid:
gymnast = Gymnast.objects.get(pk=gymnastid)
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, gymnastid=None):
"""
Formulaire de creation et modification d'un lien skill/gymnaste.
"""
if gymnastid:
gymnast = get_object_or_404(Gymnast, pk=gymnastid)
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, "gymnastid": gymnastid}
return render(request, "followup/learnedskills/create.html", context)
@login_required
@require_http_methods(["GET", "POST"])
def score_create_or_update(request, scoreid=None, gymnastid=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 gymnastid is not None:
gymnast = get_object_or_404(Gymnast, pk=gymnastid)
data["gymnast"] = gymnastid
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):
"""
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)
)
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, gymnastid=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 gymnastid is not None:
gymnast = get_object_or_404(Gymnast, pk=gymnastid)
data["gymnast"] = gymnastid
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, gymnastid=None):
"""
Récupère la liste des evaluations mentales suivant (d'un gymnast si définit en paramètre).
"""
if gymnastid:
mindstate_list = MindState.objects.filter(gymnast=gymnastid)
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, gymnastid=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 gymnastid is not None:
gymnast = get_object_or_404(Gymnast, pk=gymnastid)
data["gymnast"] = gymnastid
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", "POST"])
def heightweight_create_or_update(request, heightweightid=None, gymnastid=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:
mindstate = None
data = {}
if gymnastid:
gymnast = get_object_or_404(Gymnast, pk=gymnastid)
data["gymnast"] = gymnastid
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, "gymnastid": gymnastid, "heightweightid": heightweightid}
return render(request, "followup/heightweight/create.html", context)