kadenios/elections/models.py
2021-03-19 16:08:02 +01:00

150 lines
4.7 KiB
Python

from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _
from .staticdefs import (
CAST_FUNCTIONS,
CONNECTION_METHODS,
QUESTION_TYPES,
TALLY_FUNCTIONS,
)
from .utils import CastFunctions, TallyFunctions, choices_length
# #############################################################################
# Models regarding an election
# #############################################################################
class Election(models.Model):
name = models.CharField(_("nom"), max_length=255)
short_name = models.SlugField(_("nom bref"), unique=True)
description = models.TextField(_("description"), blank=True)
start_date = models.DateTimeField(_("date et heure de début"))
end_date = models.DateTimeField(_("date et heure de fin"))
restricted = models.BooleanField(
_("restreint le vote à une liste de personnes"), default=True
)
sent_mail = models.BooleanField(
_("mail avec les identifiants envoyé"), default=False
)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
related_name="elections_created",
on_delete=models.SET_NULL,
blank=True,
null=True,
)
voters = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="cast_elections",
blank=True,
)
results_public = models.BooleanField(_("résultats publics"), default=False)
tallied = models.BooleanField(_("dépouillée"), default=False)
archived = models.BooleanField(_("archivée"), default=False)
class Meta:
ordering = ["-start_date", "-end_date"]
class Question(models.Model):
election = models.ForeignKey(
Election, related_name="questions", on_delete=models.CASCADE
)
text = models.TextField(_("question"), blank=False)
type = models.CharField(
_("type de question"),
choices=QUESTION_TYPES,
default="assentiment",
max_length=choices_length(QUESTION_TYPES),
)
# We cache the maximum number of votes for an option
max_votes = models.PositiveSmallIntegerField(
_("nombre maximal de votes reçus"), default=0
)
voters = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="cast_questions",
blank=True,
)
def cast_ballot(self, user, vote_form):
cast_function = getattr(CastFunctions, CAST_FUNCTIONS[self.type])
cast_function(user, vote_form)
def tally(self):
tally_function = getattr(TallyFunctions, TALLY_FUNCTIONS[self.type])
tally_function(self)
class Meta:
ordering = ["id"]
class Option(models.Model):
question = models.ForeignKey(
Question, related_name="options", on_delete=models.CASCADE
)
text = models.TextField(_("texte"), blank=False)
voters = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="votes",
through="Vote",
blank=True,
)
# For now, we store the amount of votes received after the election is tallied
nb_votes = models.PositiveSmallIntegerField(_("nombre de votes reçus"), default=0)
class Meta:
ordering = ["id"]
class Vote(models.Model):
option = models.ForeignKey(Option, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
# #############################################################################
# Modification of the base User Model
# #############################################################################
class User(AbstractUser):
election = models.ForeignKey(
Election,
related_name="registered_voters",
null=True,
blank=True,
on_delete=models.CASCADE,
)
full_name = models.CharField(_("Nom et Prénom"), max_length=150, blank=True)
def get_username(self):
return "__".join(self.username.split("__")[1:])
def can_vote(self, request, election):
# Si c'est un·e utilisateur·ice CAS, iel peut voter dans les élections
# ouvertes à tou·te·s
if self.election is None:
# If the user is connected via CAS, request.session["CASCONNECTED"] is set
# to True by authens
return not election.restricted and request.session.get("CASCONNECTED")
# Pour les élections restreintes, il faut y être associé
return election.restricted and (self.election == election)
def get_prefix(self):
return self.username.split("__")[0]
def connection_method(self):
method = self.username.split("__")[0]
return CONNECTION_METHODS.get(method, _("identifiants spécifiques"))