kadenios/petitions/forms.py

69 lines
2.1 KiB
Python
Raw Permalink Normal View History

from translated_fields import language_code_formfield_callback
from django import forms
from django.utils.translation import gettext_lazy as _
from .models import Petition, Signature
class PetitionForm(forms.ModelForm):
formfield_callback = language_code_formfield_callback
class Meta:
model = Petition
fields = [
*Petition.title.fields,
*Petition.text.fields,
*Petition.letter.fields,
"launch_date",
]
widgets = {
"text_en": forms.Textarea(attrs={"rows": 3}),
"text_fr": forms.Textarea(attrs={"rows": 4}),
"letter_en": forms.Textarea(attrs={"rows": 3}),
"letter_fr": forms.Textarea(attrs={"rows": 4}),
}
class DeleteForm(forms.Form):
def __init__(self, **kwargs):
signature = kwargs.pop("signature", None)
super().__init__(**kwargs)
if signature is not None:
self.fields["delete"].label = _("Supprimer la signature de {} ?").format(
signature.full_name
)
delete = forms.ChoiceField(
label=_("Supprimer"), choices=(("non", _("Non")), ("oui", _("Oui")))
)
class ValidateForm(forms.Form):
def __init__(self, **kwargs):
signature = kwargs.pop("signature", None)
super().__init__(**kwargs)
if signature is not None:
self.fields["validate"].label = _("Valider la signature de {} ?").format(
signature.full_name
)
validate = forms.ChoiceField(
label=_("Valider"), choices=(("non", _("Non")), ("oui", _("Oui")))
)
class SignatureForm(forms.ModelForm):
def clean_email(self):
email = self.cleaned_data["email"]
if self.instance.petition.signatures.filter(email__iexact=email):
self.add_error(
"email",
_("Une personne a déjà signé la pétition avec cette adresse mail."),
)
return email
class Meta:
model = Signature
fields = ["full_name", "email", "status", "department", "elected"]