Nouveau mixin pour travailler avec des élections terminées

This commit is contained in:
Tom Hubrecht 2021-03-27 12:46:39 +01:00
parent da43ecbffe
commit fdab401eb3
2 changed files with 17 additions and 13 deletions

View file

@ -82,5 +82,17 @@ class CreatorOnlyEditMixin(CreatorOnlyMixin, SingleObjectMixin):
return filters 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: class VoterOnlyMixin:
"""Restreint l'accès aux voteureuses de l'élection""" """Restreint l'accès aux voteureuses de l'élection"""

View file

@ -16,7 +16,6 @@ from django.views.generic import (
RedirectView, RedirectView,
UpdateView, UpdateView,
) )
from django.views.generic.detail import SingleObjectMixin
from .forms import ( from .forms import (
ElectionForm, ElectionForm,
@ -28,6 +27,7 @@ from .forms import (
) )
from .mixins import ( from .mixins import (
AdminOnlyMixin, AdminOnlyMixin,
ClosedElectionMixin,
CreatorOnlyEditMixin, CreatorOnlyEditMixin,
CreatorOnlyMixin, CreatorOnlyMixin,
OpenElectionOnlyMixin, OpenElectionOnlyMixin,
@ -173,7 +173,7 @@ class ElectionUpdateView(CreatorOnlyEditMixin, SuccessMessageMixin, UpdateView):
return reverse("election.admin", args=[self.object.pk]) return reverse("election.admin", args=[self.object.pk])
class ElectionTallyView(CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateView): class ElectionTallyView(ClosedElectionMixin, BackgroundUpdateView):
model = Election model = Election
pattern_name = "election.admin" pattern_name = "election.admin"
success_message = _("Élection dépouillée avec succès !") success_message = _("Élection dépouillée avec succès !")
@ -182,7 +182,7 @@ class ElectionTallyView(CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateVie
return ( return (
super() super()
.get_queryset() .get_queryset()
.filter(end_date__lt=timezone.now(), tallied=False) .filter(tallied=False)
.prefetch_related("questions__options") .prefetch_related("questions__options")
) )
@ -196,9 +196,7 @@ class ElectionTallyView(CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateVie
return super().get(request, *args, **kwargs) return super().get(request, *args, **kwargs)
class ElectionChangePublicationView( class ElectionChangePublicationView(ClosedElectionMixin, BackgroundUpdateView):
CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateView
):
model = Election model = Election
pattern_name = "election.admin" pattern_name = "election.admin"
@ -207,9 +205,6 @@ class ElectionChangePublicationView(
return _("Élection publiée avec succès !") return _("Élection publiée avec succès !")
return _("Élection dé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): def get(self, request, *args, **kwargs):
self.election = self.get_object() self.election = self.get_object()
self.election.results_public = not self.election.results_public self.election.results_public = not self.election.results_public
@ -217,14 +212,11 @@ class ElectionChangePublicationView(
return super().get(request, *args, **kwargs) return super().get(request, *args, **kwargs)
class ElectionArchiveView(CreatorOnlyMixin, SingleObjectMixin, BackgroundUpdateView): class ElectionArchiveView(ClosedElectionMixin, BackgroundUpdateView):
model = Election model = Election
pattern_name = "election.admin" pattern_name = "election.admin"
success_message = _("Élection archivée avec succès !") 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): def get(self, request, *args, **kwargs):
election = self.get_object() election = self.get_object()
election.archived = True election.archived = True