annuaire-eleves/fiches/forms.py

42 lines
1.3 KiB
Python

from django import forms
from django.forms.models import inlineformset_factory
from django.utils.translation import gettext_lazy as _
from fiches.models import Address, Department, Mail, Phone, Profile, Social
class ProfileForm(forms.ModelForm):
birth_date = forms.DateField(
input_formats=["%Y-%m-%d"],
required=False,
)
class Meta:
model = Profile
exclude = ["user"]
class SearchForm(forms.Form):
name = forms.CharField(label=_("Nom/Surnom"), max_length=1023, required=False)
year = forms.IntegerField(label=_("Promotion"), required=False)
department = forms.ModelMultipleChoiceField(
queryset=Department.objects.all(), required=False
)
def clean(self):
cleaned_data = super().clean()
if (
not cleaned_data["name"]
and not cleaned_data["year"]
and not cleaned_data["department"]
):
raise forms.ValidationError(_("Tous les champs sont vides"), code="invalid")
PhoneFormSet = inlineformset_factory(Profile, Phone, exclude=[])
SocialFormSet = inlineformset_factory(Profile, Social, exclude=[])
MailFormSet = inlineformset_factory(Profile, Mail, exclude=[])
AddressFormSet = inlineformset_factory(Profile, Address, exclude=[])