Jarvis/jarvis/objective/forms.py

248 lines
7.7 KiB
Python
Raw Normal View History

2024-03-22 15:03:44 +01:00
import re
from datetime import date
2023-04-25 17:06:14 +02:00
from django import forms
2024-03-11 10:28:22 +01:00
from django.core.exceptions import ValidationError
2024-03-13 11:15:19 +01:00
from django.contrib.admin.widgets import FilteredSelectMultiple
2024-03-11 10:28:22 +01:00
2024-03-13 11:15:19 +01:00
from .models import (
Educative,
Skill,
Routine,
RoutineSkill,
2024-03-21 17:30:24 +01:00
TrainingRound,
GymnastTraining,
2024-03-25 12:51:12 +01:00
GymnastTrainingRound
2024-03-13 11:15:19 +01:00
)
2023-04-25 17:06:14 +02:00
2024-03-22 15:03:44 +01:00
from .validators import is_valid_routine_type, is_valid_subset, is_valid_regexp
2023-04-25 17:06:14 +02:00
class SkillForm(forms.ModelForm):
class Meta:
model = Skill
fields = ("informations",)
widgets = {
"informations": forms.Textarea(
attrs={
"class": "form-control",
"placeholder": "Informations about the skill : attention point, methodology, biomecanics, …", # pylint: disable=line-too-long
}
),
}
2023-05-16 12:58:11 +02:00
class CombinationForm(forms.ModelForm):
2023-04-25 17:06:14 +02:00
class Meta:
model = Routine
fields = (
"long_label",
"short_label",
"difficulty",
"level",
"is_active",
2024-02-06 11:00:21 +01:00
"is_routine",
"is_competitive",
2023-04-25 17:06:14 +02:00
"informations",
)
widgets = {
"long_label": forms.TextInput(
attrs={"class": "form-control", "placeholder": "Routine's long name"}
),
"short_label": forms.TextInput(
attrs={"class": "form-control", "placeholder": "Routine's short name"}
),
"informations": forms.Textarea(
attrs={
"class": "form-control",
"placeholder": "Informations about the psychological state of mind : context (why, where, …), possible consequencies, …", # pylint: disable=line-too-long
}
),
"difficulty": forms.HiddenInput(),
"level": forms.HiddenInput(),
"is_active": forms.HiddenInput(),
}
2023-05-16 12:58:11 +02:00
class CombinationSkillForm(forms.ModelForm):
2023-04-25 17:06:14 +02:00
class Meta:
model = RoutineSkill
fields = (
"routine",
"skill",
"rank",
)
widgets = {
"routine": forms.HiddenInput(),
"skill": forms.HiddenInput(),
"rank": forms.NumberInput(),
}
2024-02-25 20:02:20 +01:00
2024-03-21 17:30:24 +01:00
class TrainingRoundForm(forms.ModelForm):
educatives = forms.ModelMultipleChoiceField(
required=False,
queryset=Educative.objects.all(),
widget=FilteredSelectMultiple("Educatives", is_stacked=False),
# widget=customFilteredSelectMultiple(verbose_name='test2',is_stacked=False)
)
class Media:
css = {
"all": ["admin/css/widgets.css"],
}
js = ["/admin/jsi18n/"]
class Meta:
model = TrainingRound
fields = ("label", "regexp", "educatives", "informations")
widgets = {
"label": forms.TextInput(
attrs={
"class": "form-control",
"placeholder": "Label (not mandatory)",
"maxlength": 30,
}
),
2024-03-22 15:03:44 +01:00
"regexp": forms.TextInput(attrs={"class": "form-control"}),
2024-03-21 17:30:24 +01:00
"informations": forms.Textarea(
attrs={
"class": "form-control",
"placeholder": "Informations about the passe…", # pylint: disable=line-too-long
}
),
}
def clean_regexp(self):
"""Vérifie que la regexp entrée par l'utilisateur est valide."""
2024-03-22 15:03:44 +01:00
print(self.cleaned_data)
2024-03-21 17:30:24 +01:00
regexp = self.cleaned_data["regexp"]
2024-03-22 15:03:44 +01:00
if not is_valid_regexp(regexp):
2024-03-21 17:30:24 +01:00
raise ValidationError("Entered regexp not valid.")
return regexp
def clean(self):
"""Vérifie le contenu des champs `educatives` par rapport à la valeur de `regexp`. Si
`regexp` est définie par :
- valeurs de ROUTINE_TYPE_CHOICE il faut que Educatives soit VIDE
- avec [x-y]
- [x-y] " Educatives soit NON VIDE
- WC " il y ait 2+ Educatives
- x| " il y ait 1! Educatives
"""
cleaned_data = super().clean()
regexp = cleaned_data["regexp"]
2024-03-22 15:03:44 +01:00
if not regexp:
2024-03-21 17:30:24 +01:00
arguments = regexp.split(" ")
educatives = cleaned_data["educatives"]
2024-03-22 15:03:44 +01:00
if is_valid_routine_type(arguments[0]) and educatives:
2024-03-21 17:30:24 +01:00
raise ValidationError(
"Educatives must be empty with the entered Regexp."
)
2024-03-22 15:03:44 +01:00
if is_valid_subset(arguments[0]) and educatives is None:
2024-03-21 17:30:24 +01:00
raise ValidationError(
"Educatives can't be empty with the entered Regexp."
)
if re.match(r"[1-9]+\|", arguments[0]) and len(educatives) != 1:
raise ValidationError(
"One and only one Educatives allowed with the entered Regexp."
)
if arguments[0] == "WC" and (educatives is None or len(educatives) < 2):
raise ValidationError(
"At least two Educatives with the entered Regexp."
)
return cleaned_data
# def save_model(self, request, obj, form, change):
# obj.save()
# for educative in form.cleaned_data["educatives"]:
# print(educative)
# book = Book.objects.get(pk=bk.id)
# book.quantity -= 1
# if book.quantity == 0:
# book.sold = True;
# book.save()
class GymnastTrainingForm(forms.ModelForm):
# trainingrounds = forms.ModelMultipleChoiceField(
# required=False,
# queryset=Educative.objects.all(),
# widget=FilteredSelectMultiple("TrainingRound", is_stacked=False),
# # widget=customFilteredSelectMultiple(verbose_name='test2',is_stacked=False)
# )
# class Media:
# css = {
# "all": ["admin/css/widgets.css"],
# }
# js = ["/admin/jsi18n/"]
class Meta:
model = GymnastTraining
fields = (
"date",
"label",
"gymnast",
"informations",
)
widgets = {
"gymnast": forms.HiddenInput(),
"date": forms.DateInput(
attrs={
"class": "form-control datepicker",
"placeholder": date.today().strftime("%Y-%m-%d"),
"value": date.today().strftime("%Y-%m-%d"),
}
),
"label": forms.TextInput(
attrs={
"class": "form-control",
"placeholder": "Label (not mandatory)",
"maxlength": 30,
}
),
"informations": forms.Textarea(
attrs={
"class": "form-control",
"placeholder": "Informations about the passe…", # pylint: disable=line-too-long
}
),
}
gymnast_related = forms.CharField(
required=False,
widget=forms.TextInput(
attrs={
"class": "form-control",
"placeholder": "Searching…",
"data-ref": "#id_gymnast",
}
),
)
2024-03-25 12:51:12 +01:00
class GymnastTrainingRoundForm(forms.ModelForm):
class Meta:
model = GymnastTrainingRound
fields = (
"gymnast_training",
"training_round",
"repetition",
"rank",
)
widgets = {
"gymnast_training": forms.HiddenInput(),
"training_round": forms.HiddenInput(),
"repetition": forms.NumberInput(),
"rank": forms.NumberInput(),
}