2021-03-20 20:07:12 +01:00
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
2021-03-29 00:21:41 +02:00
|
|
|
from django.db.models import Q
|
2020-12-21 00:07:07 +01:00
|
|
|
from django.urls import reverse
|
2020-12-20 01:58:38 +01:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
|
|
|
|
|
|
from .models import Election, Option, Question
|
|
|
|
|
|
|
|
|
2021-03-20 20:07:12 +01:00
|
|
|
class AdminOnlyMixin(PermissionRequiredMixin):
|
|
|
|
"""Restreint l'accès aux admins"""
|
|
|
|
|
|
|
|
permission_required = "elections.is_admin"
|
|
|
|
|
|
|
|
|
2020-12-20 02:31:56 +01:00
|
|
|
class SelectElectionMixin:
|
|
|
|
"""Sélectionne automatiquement les foreignkeys voulues"""
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
if self.model is Question:
|
|
|
|
return qs.select_related("election")
|
|
|
|
elif self.model is Option:
|
|
|
|
return qs.select_related("question__election")
|
|
|
|
return qs
|
|
|
|
|
|
|
|
|
|
|
|
class RestrictAccessMixin(SelectElectionMixin):
|
2020-12-20 01:58:38 +01:00
|
|
|
"""Permet de restreindre l'accès à des élections/questions/options"""
|
|
|
|
|
|
|
|
f_prefixes = {Election: "", Question: "election__", Option: "question__election__"}
|
|
|
|
|
|
|
|
def get_f_prefix(self):
|
|
|
|
return self.f_prefixes.get(self.model, None)
|
|
|
|
|
|
|
|
def get_filters(self):
|
|
|
|
return {}
|
2020-12-19 23:48:18 +01:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-12-20 01:58:38 +01:00
|
|
|
qs = super().get_queryset()
|
|
|
|
if self.model in self.f_prefixes:
|
|
|
|
return qs.filter(**self.get_filters())
|
|
|
|
# On ne sait pas ce qu'on manipule donc on ne renvoie rien
|
|
|
|
return qs.none()
|
|
|
|
|
|
|
|
|
2020-12-20 18:50:38 +01:00
|
|
|
class OpenElectionOnlyMixin(RestrictAccessMixin):
|
2020-12-20 10:35:18 +01:00
|
|
|
"""N'autorise la vue que lorsque l'élection est ouverte"""
|
|
|
|
|
|
|
|
def get_filters(self):
|
|
|
|
f_prefix = self.get_f_prefix()
|
|
|
|
# On ne peut modifier que les élections qui n'ont pas commencé, et
|
|
|
|
# accessoirement qui ne sont pas dépouillées ou archivées
|
|
|
|
# TODO: décider si on choisit pas de juste garder les dates d'ouverture
|
|
|
|
filters = super().get_filters()
|
|
|
|
filters[f_prefix + "start_date__lt"] = timezone.now()
|
|
|
|
filters[f_prefix + "end_date__gt"] = timezone.now()
|
|
|
|
filters[f_prefix + "tallied"] = False
|
|
|
|
filters[f_prefix + "archived"] = False
|
|
|
|
return filters
|
|
|
|
|
|
|
|
|
2021-03-20 20:07:12 +01:00
|
|
|
class CreatorOnlyMixin(AdminOnlyMixin, RestrictAccessMixin):
|
2020-12-20 01:58:38 +01:00
|
|
|
"""Restreint l'accès au créateurice de l'élection"""
|
|
|
|
|
2020-12-21 00:07:07 +01:00
|
|
|
def get_next_url(self):
|
|
|
|
return reverse("kadenios")
|
|
|
|
|
2020-12-20 01:58:38 +01:00
|
|
|
def get_filters(self):
|
|
|
|
filters = super().get_filters()
|
2020-12-19 23:48:18 +01:00
|
|
|
# TODO: change the way we collect the user according to the model used
|
2020-12-20 01:58:38 +01:00
|
|
|
filters[self.get_f_prefix() + "created_by"] = self.request.user
|
|
|
|
return filters
|
|
|
|
|
|
|
|
|
|
|
|
class CreatorOnlyEditMixin(CreatorOnlyMixin, SingleObjectMixin):
|
|
|
|
"""Permet au créateurice de modifier l'élection implicitement"""
|
|
|
|
|
|
|
|
def get_filters(self):
|
|
|
|
# On ne peut modifier que les élections qui n'ont pas commencé
|
|
|
|
filters = super().get_filters()
|
|
|
|
filters[self.get_f_prefix() + "start_date__gt"] = timezone.now()
|
|
|
|
return filters
|
2020-12-19 23:48:18 +01:00
|
|
|
|
|
|
|
|
2021-03-27 12:46:39 +01:00
|
|
|
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
|
2021-03-29 00:21:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|