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