On rajoute un mixin pour éliminer les élection archivées

This commit is contained in:
Tom Hubrecht 2021-03-29 00:21:41 +02:00
parent 363b181d22
commit 4ac776fba6
2 changed files with 18 additions and 18 deletions

View file

@ -1,4 +1,5 @@
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.db.models import Q
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
@ -92,3 +93,15 @@ class ClosedElectionMixin(CreatorOnlyMixin, SingleObjectMixin):
filters[f_prefix + "end_date__lt"] = timezone.now() filters[f_prefix + "end_date__lt"] = timezone.now()
filters[f_prefix + "archived"] = False filters[f_prefix + "archived"] = False
return filters return filters
class NotArchivedMixin:
"""Permet de ne garder que les élections non archivées ou dont on est l'admin"""
def get_queryset(self):
user = self.request.user
qs = super().get_queryset()
if user.is_authenticated:
return qs.filter(Q(archived=False) | Q(created_by=user))
return qs.filter(archived=False)

View file

@ -1,6 +1,5 @@
from django.contrib import messages from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin from django.contrib.messages.views import SuccessMessageMixin
from django.db.models import Q
from django.http import Http404, HttpResponseRedirect from django.http import Http404, HttpResponseRedirect
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
@ -30,6 +29,7 @@ from .mixins import (
ClosedElectionMixin, ClosedElectionMixin,
CreatorOnlyEditMixin, CreatorOnlyEditMixin,
CreatorOnlyMixin, CreatorOnlyMixin,
NotArchivedMixin,
OpenElectionOnlyMixin, OpenElectionOnlyMixin,
) )
from .models import Election, Option, Question from .models import Election, Option, Question
@ -329,20 +329,12 @@ class DelOptionView(CreatorOnlyEditMixin, BackgroundUpdateView):
# ############################################################################# # #############################################################################
class ElectionListView(ListView): class ElectionListView(NotArchivedMixin, ListView):
model = Election model = Election
template_name = "elections/election_list.html" template_name = "elections/election_list.html"
def get_queryset(self):
user = self.request.user
qs = super().get_queryset()
if user.is_authenticated:
return qs.filter(Q(archived=False) | Q(created_by=user))
return qs.filter(archived=False) class ElectionView(NotArchivedMixin, DetailView):
class ElectionView(DetailView):
model = Election model = Election
template_name = "elections/election.html" template_name = "elections/election.html"
@ -364,15 +356,10 @@ class ElectionView(DetailView):
return context return context
def get_queryset(self): def get_queryset(self):
user = self.request.user return super().get_queryset().prefetch_related("questions__options")
qs = super().get_queryset().prefetch_related("questions__options")
if user.is_authenticated:
return qs.filter(Q(archived=False) | Q(created_by=user))
return qs.filter(archived=False)
class ElectionVotersView(DetailView): class ElectionVotersView(NotArchivedMixin, DetailView):
model = Election model = Election
template_name = "elections/election_voters.html" template_name = "elections/election_voters.html"