From fdab401eb3c3827cc12ae101be90bbe992d1814f Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Sat, 27 Mar 2021 12:46:39 +0100 Subject: [PATCH] =?UTF-8?q?Nouveau=20mixin=20pour=20travailler=20avec=20de?= =?UTF-8?q?s=20=C3=A9lections=20termin=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elections/mixins.py | 12 ++++++++++++ elections/views.py | 18 +++++------------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/elections/mixins.py b/elections/mixins.py index f9784ef..3ddc629 100644 --- a/elections/mixins.py +++ b/elections/mixins.py @@ -82,5 +82,17 @@ class CreatorOnlyEditMixin(CreatorOnlyMixin, SingleObjectMixin): return filters +class ClosedElectionMixin(CreatorOnlyMixin, SingleObjectMixin): + """Permet d'agir sur une élection terminée""" + + def get_filters(self): + f_prefix = self.get_f_prefix() + # L'élection doit être terminée et non archivée + filters = super().get_filters() + filters[f_prefix + "end_date__lt"] = timezone.now() + filters[f_prefix + "archived"] = False + return filters + + class VoterOnlyMixin: """Restreint l'accès aux voteureuses de l'élection""" diff --git a/elections/views.py b/elections/views.py index 2356a03..ec26e4d 100644 --- a/elections/views.py +++ b/elections/views.py @@ -16,7 +16,6 @@ from django.views.generic import ( RedirectView, UpdateView, ) -from django.views.generic.detail import SingleObjectMixin from .forms import ( ElectionForm, @@ -28,6 +27,7 @@ from .forms import ( ) from .mixins import ( AdminOnlyMixin, + ClosedElectionMixin, CreatorOnlyEditMixin, CreatorOnlyMixin, OpenElectionOnlyMixin, @@ -173,7 +173,7 @@ class ElectionUpdateView(CreatorOnlyEditMixin, SuccessMessageMixin, UpdateView): return reverse("election.admin", args=[self.object.pk]) -class ElectionTallyView(CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateView): +class ElectionTallyView(ClosedElectionMixin, BackgroundUpdateView): model = Election pattern_name = "election.admin" success_message = _("Élection dépouillée avec succès !") @@ -182,7 +182,7 @@ class ElectionTallyView(CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateVie return ( super() .get_queryset() - .filter(end_date__lt=timezone.now(), tallied=False) + .filter(tallied=False) .prefetch_related("questions__options") ) @@ -196,9 +196,7 @@ class ElectionTallyView(CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateVie return super().get(request, *args, **kwargs) -class ElectionChangePublicationView( - CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateView -): +class ElectionChangePublicationView(ClosedElectionMixin, BackgroundUpdateView): model = Election pattern_name = "election.admin" @@ -207,9 +205,6 @@ class ElectionChangePublicationView( return _("Élection publiée avec succès !") return _("Élection dépubliée avec succès !") - def get_queryset(self): - return super().get_queryset().filter(end_date__lt=timezone.now()) - def get(self, request, *args, **kwargs): self.election = self.get_object() self.election.results_public = not self.election.results_public @@ -217,14 +212,11 @@ class ElectionChangePublicationView( return super().get(request, *args, **kwargs) -class ElectionArchiveView(CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateView): +class ElectionArchiveView(ClosedElectionMixin, BackgroundUpdateView): model = Election pattern_name = "election.admin" success_message = _("Élection archivée avec succès !") - 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