diff --git a/elections/forms.py b/elections/forms.py index f05237c..3ff80d3 100644 --- a/elections/forms.py +++ b/elections/forms.py @@ -1,7 +1,27 @@ from django import forms from django.forms.models import inlineformset_factory +from django.utils import timezone +from django.utils.translation import gettext_lazy as _ -from .models import Option, Question +from .models import Election, Option, Question + + +class ElectionCreateForm(forms.ModelForm): + 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 + fields = ["name", "description", "start_date", "end_date"] class VoteForm(forms.ModelForm): diff --git a/elections/views.py b/elections/views.py index f25c07e..6678494 100644 --- a/elections/views.py +++ b/elections/views.py @@ -9,7 +9,7 @@ from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, DetailView, RedirectView, UpdateView from django.views.generic.detail import SingleObjectMixin -from .forms import OptionFormSet +from .forms import ElectionCreateForm, OptionFormSet from .models import Election, Option, Question # TODO: access control *everywhere* @@ -17,9 +17,9 @@ from .models import Election, Option, Question class ElectionCreateView(SuccessMessageMixin, CreateView): model = Election - fields = ["name", "description", "start_date", "end_date"] + form_class = ElectionCreateForm template_name = "elections/election_create.html" - success_message = _("Élection crée avec succès !") + success_message = _("Élection créée avec succès !") def get_success_url(self): return reverse("election.admin", args=[self.object.pk]) @@ -27,7 +27,7 @@ class ElectionCreateView(SuccessMessageMixin, CreateView): def form_valid(self, form): # We need to add the short name and the creator od the election form.instance.short_name = slugify( - str(form.instance.start_date.year) + "_" + form.instance.name + form.instance.start_date.strftime("%Y-%m-%d") + "_" + form.instance.name )[:50] # TODO: Change this if we modify the user model form.instance.created_by = self.request.user @@ -57,8 +57,8 @@ class ElectionUpdateView(SuccessMessageMixin, UpdateView): return reverse("election.admin", args=[self.object.pk]) def get_queryset(self): - # On ne peut plus modifier une élection déjà comptée - return super().get_queryset().filter(tallied=False, archived=False) + # On ne peut plus modifier une élection qui a déjà commencé + return super().get_queryset().filter(start_date__gt=timezone.now()) class ElectionTallyView(SuccessMessageMixin, SingleObjectMixin, RedirectView): @@ -66,7 +66,12 @@ class ElectionTallyView(SuccessMessageMixin, SingleObjectMixin, RedirectView): pattern_name = "election.admin" def get_queryset(self): - return super().get_queryset().prefetch_related("questions__options") + return ( + super() + .get_queryset() + .filter(end_date__lt=timezone.now()) + .prefetch_related("questions__options") + ) def get(self, request, *args, **kwargs): election = self.get_object() @@ -95,6 +100,9 @@ class ElectionChangePublicationView( model = Election pattern_name = "election.admin" + def get_queryset(self): + return super().get_queryset().filter(end_date__lt=timezone.now()) + def get(self, request, *args, **kwargs): election = self.get_object() election.results_public = not election.results_public @@ -112,6 +120,9 @@ class ElectionArchiveView(SuccessMessageMixin, SingleObjectMixin, RedirectView): model = Election pattern_name = "election.admin" + def get_queryset(self): + return super().get_queryset().filter(end_date__lt=timezone.now()) + def get(self, request, *args, **kwargs): election = self.get_object() election.archived = True @@ -148,7 +159,12 @@ class VoteView(SuccessMessageMixin, DetailView): return ( super() .get_queryset() - .filter(election__tallied=False, election__archived=False) + .filter( + election__tallied=False, + election__archived=False, + election__start_date__lt=timezone.now(), + election__end_date__gt=timezone.now(), + ) .select_related("election") )