162 lines
5.2 KiB
Python
162 lines
5.2 KiB
Python
from django import forms
|
|
from django.core.exceptions import ValidationError
|
|
|
|
import re
|
|
|
|
from .models import Skill, Routine, RoutineSkill, Passe
|
|
|
|
|
|
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
|
|
}
|
|
),
|
|
}
|
|
|
|
|
|
class CombinationForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Routine
|
|
fields = (
|
|
"long_label",
|
|
"short_label",
|
|
"difficulty",
|
|
"level",
|
|
"is_active",
|
|
"is_routine",
|
|
"is_competitive",
|
|
"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"}
|
|
),
|
|
# "is_routine": form.,
|
|
# "is_competitive": form.,
|
|
"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(),
|
|
}
|
|
|
|
|
|
class CombinationSkillForm(forms.ModelForm):
|
|
class Meta:
|
|
model = RoutineSkill
|
|
fields = (
|
|
"routine",
|
|
"skill",
|
|
"rank",
|
|
)
|
|
widgets = {
|
|
"routine": forms.HiddenInput(),
|
|
"skill": forms.HiddenInput(),
|
|
"rank": forms.NumberInput(),
|
|
}
|
|
|
|
|
|
class PasseForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Passe
|
|
fields = ("label", "educatives", "regexp", "informations")
|
|
widgets = {
|
|
"label": forms.TextInput(
|
|
attrs={
|
|
"class": "form-control",
|
|
"placeholder": "Label (not mandatory)",
|
|
"maxlength": 30,
|
|
}
|
|
),
|
|
"regexp": forms.TextInput(
|
|
attrs={"class": "form-control", "placeholder": "[2-8]"}
|
|
),
|
|
"informations": forms.Textarea(
|
|
attrs={
|
|
"class": "form-control",
|
|
"placeholder": "Informations about the passe…", # pylint: disable=line-too-long
|
|
}
|
|
),
|
|
"educatives": forms.HiddenInput(),
|
|
}
|
|
|
|
educative_related = forms.CharField(
|
|
required=False,
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
"class": "form-control",
|
|
"placeholder": "Searching educative",
|
|
"data-ref": "#id_educative",
|
|
}
|
|
),
|
|
)
|
|
|
|
# "repetition": forms.NumberInput(
|
|
# attrs={
|
|
# "class": "form-control",
|
|
# "placeholder": "x",
|
|
# "min": "0",
|
|
# "max": "1000",
|
|
# "step": "1",
|
|
# }
|
|
# ),
|
|
|
|
def clean_regexp(self):
|
|
"""Vérifie que la regexp entrée par l'utilisateur est valide."""
|
|
regexp = self.cleaned_data["regexp"]
|
|
if not Passe.is_valid_regexp(regexp):
|
|
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 = ["regexp"]
|
|
|
|
if regexp is not None:
|
|
arguments = regexp.split(" ")
|
|
educatives = cleaned_data["educatives"]
|
|
|
|
if Passe.is_valid_routine_type(arguments[0]) and educatives is not None:
|
|
raise ValidationError(
|
|
"Educatives must be empty with the entered Regexp."
|
|
)
|
|
|
|
if Passe.is_valid_subset(arguments[0]) and educatives is None:
|
|
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
|