2021-04-15 17:26:27 +02:00
|
|
|
from translated_fields import language_code_formfield_callback
|
|
|
|
|
2020-12-18 00:19:18 +01:00
|
|
|
from django import forms
|
|
|
|
from django.forms.models import inlineformset_factory
|
2020-12-19 22:22:23 +01:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-12-18 00:19:18 +01:00
|
|
|
|
2020-12-19 22:22:23 +01:00
|
|
|
from .models import Election, Option, Question
|
2020-12-23 16:28:38 +01:00
|
|
|
from .utils import check_csv
|
2020-12-19 22:22:23 +01:00
|
|
|
|
|
|
|
|
2020-12-20 02:32:26 +01:00
|
|
|
class ElectionForm(forms.ModelForm):
|
2021-04-15 17:01:17 +02:00
|
|
|
formfield_callback = language_code_formfield_callback
|
|
|
|
|
2020-12-19 22:22:23 +01:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
if cleaned_data["start_date"] < timezone.now():
|
|
|
|
self.add_error(
|
|
|
|
"start_date", _("Impossible de faire débuter l'élection dans le passé")
|
|
|
|
)
|
|
|
|
elif cleaned_data["start_date"] >= cleaned_data["end_date"]:
|
|
|
|
self.add_error(
|
|
|
|
"end_date", _("Impossible de terminer l'élection avant de la commencer")
|
|
|
|
)
|
2021-05-26 20:12:25 +02:00
|
|
|
|
|
|
|
if self.instance.sent_mail and not cleaned_data["restricted"]:
|
|
|
|
self.add_error(
|
|
|
|
"restricted",
|
|
|
|
_(
|
|
|
|
"Le mail d'annonce a déjà été envoyé, il n'est pas possible "
|
|
|
|
"d'ouvrir l'élection à tout le monde"
|
|
|
|
),
|
|
|
|
)
|
2020-12-19 22:22:23 +01:00
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Election
|
2021-04-14 01:59:23 +02:00
|
|
|
fields = [
|
2021-04-15 17:01:17 +02:00
|
|
|
*Election.name.fields,
|
2021-04-14 01:59:23 +02:00
|
|
|
"start_date",
|
|
|
|
"end_date",
|
2021-04-15 17:01:17 +02:00
|
|
|
*Election.description.fields,
|
|
|
|
"restricted",
|
|
|
|
*Election.vote_restrictions.fields,
|
2021-04-14 01:59:23 +02:00
|
|
|
]
|
|
|
|
widgets = {
|
2021-04-15 17:01:17 +02:00
|
|
|
"description_en": forms.Textarea(attrs={"rows": 4}),
|
|
|
|
"description_fr": forms.Textarea(attrs={"rows": 4}),
|
|
|
|
"vote_restrictions_en": forms.Textarea(attrs={"rows": 4}),
|
|
|
|
"vote_restrictions_fr": forms.Textarea(attrs={"rows": 4}),
|
2021-04-14 01:59:23 +02:00
|
|
|
}
|
2020-12-18 00:19:18 +01:00
|
|
|
|
|
|
|
|
2020-12-22 01:17:38 +01:00
|
|
|
class UploadVotersForm(forms.Form):
|
|
|
|
csv_file = forms.FileField(label=_("Sélectionnez un fichier .csv"))
|
|
|
|
|
2020-12-23 11:19:30 +01:00
|
|
|
def clean_csv_file(self):
|
|
|
|
csv_file = self.cleaned_data["csv_file"]
|
2020-12-23 16:28:38 +01:00
|
|
|
if csv_file.name.lower().endswith(".csv"):
|
|
|
|
for error in check_csv(csv_file):
|
|
|
|
self.add_error(None, error)
|
|
|
|
else:
|
|
|
|
self.add_error(
|
|
|
|
None,
|
|
|
|
_("Extension de fichier invalide, il faut un fichier au format CSV."),
|
|
|
|
)
|
2020-12-23 17:08:07 +01:00
|
|
|
csv_file.seek(0)
|
2020-12-23 11:19:30 +01:00
|
|
|
return csv_file
|
|
|
|
|
2020-12-22 01:17:38 +01:00
|
|
|
|
2020-12-24 00:41:29 +01:00
|
|
|
class VoterMailForm(forms.Form):
|
2021-05-27 00:26:49 +02:00
|
|
|
objet = forms.CharField(label=_("Objet"))
|
2020-12-24 00:41:29 +01:00
|
|
|
message = forms.CharField(widget=forms.Textarea)
|
|
|
|
|
|
|
|
|
2020-12-20 02:32:26 +01:00
|
|
|
class QuestionForm(forms.ModelForm):
|
2021-04-15 17:01:17 +02:00
|
|
|
formfield_callback = language_code_formfield_callback
|
|
|
|
|
2020-12-20 02:32:26 +01:00
|
|
|
class Meta:
|
|
|
|
model = Question
|
2021-04-15 17:01:17 +02:00
|
|
|
fields = [*Question.text.fields, "type"]
|
|
|
|
widgets = {"text_fr": forms.TextInput, "text_en": forms.TextInput}
|
2020-12-20 02:32:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class OptionForm(forms.ModelForm):
|
2021-04-15 17:01:17 +02:00
|
|
|
formfield_callback = language_code_formfield_callback
|
|
|
|
|
2020-12-20 02:32:26 +01:00
|
|
|
class Meta:
|
|
|
|
model = Option
|
2021-04-17 02:00:34 +02:00
|
|
|
fields = [*Option.text.fields, "abbreviation"]
|
2021-04-15 17:01:17 +02:00
|
|
|
widgets = {"text_fr": forms.TextInput, "text_en": forms.TextInput}
|
2021-04-17 02:00:34 +02:00
|
|
|
help_texts = {
|
|
|
|
"abbreviation": _(
|
2021-04-20 10:04:16 +02:00
|
|
|
"L'abréviation est optionnelle et sert à identifier plus facilement les "
|
2021-04-17 02:00:34 +02:00
|
|
|
"différentes options. Elle est affiché sans espaces et en majuscules."
|
|
|
|
)
|
|
|
|
}
|
2020-12-20 02:32:26 +01:00
|
|
|
|
|
|
|
|
2021-03-31 13:16:10 +02:00
|
|
|
class DeleteVoteForm(forms.Form):
|
|
|
|
def __init__(self, **kwargs):
|
2021-04-09 11:40:16 +02:00
|
|
|
voter = kwargs.pop("voter", None)
|
2021-03-31 13:16:10 +02:00
|
|
|
super().__init__(**kwargs)
|
2021-04-09 11:40:16 +02:00
|
|
|
if voter is not None:
|
|
|
|
self.fields["delete"].label = _("Supprimer le vote de {} ({}) ?").format(
|
2021-04-26 17:54:07 +02:00
|
|
|
voter.full_name, voter.base_username
|
2021-04-09 11:40:16 +02:00
|
|
|
)
|
2021-03-31 13:16:10 +02:00
|
|
|
|
2021-04-09 11:40:16 +02:00
|
|
|
delete = forms.ChoiceField(
|
|
|
|
label=_("Supprimer"), choices=(("non", _("Non")), ("oui", _("Oui")))
|
|
|
|
)
|
2021-03-31 13:16:10 +02:00
|
|
|
|
|
|
|
|
2021-03-29 12:42:34 +02:00
|
|
|
class SelectVoteForm(forms.ModelForm):
|
2020-12-18 00:19:18 +01:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
# We set the option's text as the label for the checkbox
|
|
|
|
instance = kwargs.get("instance", None)
|
|
|
|
if instance is not None:
|
2021-04-17 02:00:34 +02:00
|
|
|
self.fields["selected"].label = str(instance)
|
2020-12-18 00:19:18 +01:00
|
|
|
|
|
|
|
selected = forms.BooleanField(required=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Option
|
|
|
|
fields = []
|
2021-03-19 09:41:23 +01:00
|
|
|
exclude = ["voters"]
|
2020-12-18 00:19:18 +01:00
|
|
|
|
|
|
|
|
2021-03-29 12:42:34 +02:00
|
|
|
class RankVoteForm(forms.ModelForm):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
# We set the option's text as the label for the rank
|
|
|
|
instance = kwargs.get("instance", None)
|
|
|
|
if instance is not None:
|
2021-04-17 02:00:34 +02:00
|
|
|
self.fields["rank"].label = str(instance)
|
2021-03-29 12:42:34 +02:00
|
|
|
|
|
|
|
rank = forms.IntegerField(required=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Option
|
|
|
|
fields = []
|
|
|
|
exclude = ["voters"]
|
|
|
|
|
|
|
|
|
|
|
|
class BallotFormset:
|
2021-03-29 20:35:34 +02:00
|
|
|
select = inlineformset_factory(
|
2021-03-29 12:42:34 +02:00
|
|
|
Question, Option, extra=0, form=SelectVoteForm, can_delete=False
|
|
|
|
)
|
|
|
|
|
2021-03-29 20:35:34 +02:00
|
|
|
rank = inlineformset_factory(
|
2021-03-29 12:42:34 +02:00
|
|
|
Question, Option, extra=0, form=RankVoteForm, can_delete=False
|
|
|
|
)
|