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):
|
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")
|
|
|
|
)
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Election
|
2020-12-20 18:50:38 +01:00
|
|
|
fields = ["name", "description", "restricted", "start_date", "end_date"]
|
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):
|
|
|
|
objet = forms.CharField()
|
|
|
|
message = forms.CharField(widget=forms.Textarea)
|
|
|
|
|
|
|
|
|
2020-12-20 02:32:26 +01:00
|
|
|
class QuestionForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Question
|
2021-03-19 11:48:38 +01:00
|
|
|
fields = ["text", "type"]
|
2020-12-24 12:53:08 +01:00
|
|
|
widgets = {"text": forms.TextInput}
|
2020-12-20 02:32:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class OptionForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Option
|
|
|
|
fields = ["text"]
|
2020-12-24 12:53:08 +01:00
|
|
|
widgets = {"text": forms.TextInput}
|
2020-12-20 02:32:26 +01:00
|
|
|
|
|
|
|
|
2020-12-18 00:19:18 +01:00
|
|
|
class VoteForm(forms.ModelForm):
|
|
|
|
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:
|
|
|
|
self.fields["selected"].label = instance.text
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
OptionFormSet = inlineformset_factory(
|
|
|
|
Question, Option, extra=0, form=VoteForm, can_delete=False
|
|
|
|
)
|