2017-02-05 17:10:51 +01:00
|
|
|
from captcha.fields import ReCaptchaField
|
|
|
|
from django import forms
|
|
|
|
from django.contrib.auth.models import User
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.forms import ModelForm
|
|
|
|
from django.forms.models import BaseInlineFormSet, inlineformset_factory
|
2019-01-05 18:14:29 +01:00
|
|
|
|
2018-11-25 00:37:22 +01:00
|
|
|
from petitscours.models import PetitCoursAbility, PetitCoursDemande
|
2017-02-05 17:10:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BaseMatieresFormSet(BaseInlineFormSet):
|
|
|
|
def clean(self):
|
2018-01-16 16:22:52 +01:00
|
|
|
super().clean()
|
2017-02-05 17:10:51 +01:00
|
|
|
if any(self.errors):
|
|
|
|
# Don't bother validating the formset unless each form is
|
|
|
|
# valid on its own
|
|
|
|
return
|
|
|
|
matieres = []
|
|
|
|
for i in range(0, self.total_form_count()):
|
|
|
|
form = self.forms[i]
|
|
|
|
if not form.cleaned_data:
|
|
|
|
continue
|
2018-10-06 12:35:49 +02:00
|
|
|
matiere = form.cleaned_data["matiere"]
|
|
|
|
niveau = form.cleaned_data["niveau"]
|
|
|
|
delete = form.cleaned_data["DELETE"]
|
2017-02-05 17:10:51 +01:00
|
|
|
if not delete and (matiere, niveau) in matieres:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
"Vous ne pouvez pas vous inscrire deux fois pour la "
|
2018-10-06 12:35:49 +02:00
|
|
|
"même matiere avec le même niveau."
|
|
|
|
)
|
2017-02-05 17:10:51 +01:00
|
|
|
matieres.append((matiere, niveau))
|
|
|
|
|
|
|
|
|
|
|
|
class DemandeForm(ModelForm):
|
2018-10-06 12:35:49 +02:00
|
|
|
captcha = ReCaptchaField(attrs={"theme": "clean", "lang": "fr"})
|
2017-02-05 17:10:51 +01:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2018-01-16 16:22:52 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
2018-10-06 12:35:49 +02:00
|
|
|
self.fields["matieres"].help_text = ""
|
2017-02-05 17:10:51 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PetitCoursDemande
|
2018-10-06 12:35:49 +02:00
|
|
|
fields = (
|
|
|
|
"name",
|
|
|
|
"email",
|
|
|
|
"phone",
|
|
|
|
"quand",
|
|
|
|
"freq",
|
|
|
|
"lieu",
|
|
|
|
"matieres",
|
|
|
|
"agrege_requis",
|
|
|
|
"niveau",
|
|
|
|
"remarques",
|
|
|
|
)
|
|
|
|
widgets = {"matieres": forms.CheckboxSelectMultiple}
|
2017-02-05 17:10:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
MatieresFormSet = inlineformset_factory(
|
|
|
|
User,
|
|
|
|
PetitCoursAbility,
|
|
|
|
fields=("matiere", "niveau", "agrege"),
|
2018-10-06 12:35:49 +02:00
|
|
|
formset=BaseMatieresFormSet,
|
2017-02-05 17:10:51 +01:00
|
|
|
)
|