kadenios/elections/models.py

162 lines
5 KiB
Python
Raw Normal View History

2020-12-20 18:50:38 +01:00
from django.conf import settings
from django.contrib.auth.models import AbstractUser
2020-11-19 17:29:43 +01:00
from django.db import models
2020-11-19 18:40:22 +01:00
from django.utils.translation import gettext_lazy as _
2020-11-19 17:29:43 +01:00
2021-03-19 16:08:02 +01:00
from .staticdefs import (
CAST_FUNCTIONS,
CONNECTION_METHODS,
QUESTION_TYPES,
TALLY_FUNCTIONS,
VALIDATE_FUNCTIONS,
2021-03-19 16:08:02 +01:00
)
from .utils import CastFunctions, TallyFunctions, ValidateFunctions, choices_length
2021-01-27 14:55:28 +01:00
# #############################################################################
# Models regarding an election
# #############################################################################
2020-11-19 18:40:22 +01:00
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)
2020-12-19 15:04:04 +01:00
start_date = models.DateTimeField(_("date et heure de début"))
end_date = models.DateTimeField(_("date et heure de fin"))
2020-11-19 18:40:22 +01:00
2020-12-20 18:50:38 +01:00
restricted = models.BooleanField(
_("restreint le vote à une liste de personnes"), default=True
)
sent_mail = models.BooleanField(
_("mail avec les identifiants envoyé"), default=False
)
2020-11-19 18:40:22 +01:00
created_by = models.ForeignKey(
2020-12-20 18:50:38 +01:00
settings.AUTH_USER_MODEL,
related_name="elections_created",
on_delete=models.SET_NULL,
blank=True,
null=True,
2020-11-19 18:40:22 +01:00
)
voters = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="cast_elections",
2021-03-18 14:49:48 +01:00
blank=True,
)
2020-11-19 18:40:22 +01:00
results_public = models.BooleanField(_("résultats publics"), default=False)
2020-11-20 14:55:31 +01:00
tallied = models.BooleanField(_("dépouillée"), default=False)
2020-11-19 18:40:22 +01:00
archived = models.BooleanField(_("archivée"), default=False)
2020-12-19 23:48:18 +01:00
class Meta:
ordering = ["-start_date", "-end_date"]
2020-11-19 18:40:22 +01:00
class Question(models.Model):
2020-11-20 14:55:31 +01:00
election = models.ForeignKey(
Election, related_name="questions", on_delete=models.CASCADE
)
2020-11-19 18:40:22 +01:00
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
)
2020-11-19 18:40:22 +01:00
voters = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="cast_questions",
2021-03-18 14:49:48 +01:00
blank=True,
)
def is_form_valid(self, vote_form):
validate_function = getattr(ValidateFunctions, VALIDATE_FUNCTIONS[self.type])
return vote_form.is_valid() and validate_function(vote_form)
2021-03-19 14:25:13 +01:00
def cast_ballot(self, user, vote_form):
cast_function = getattr(CastFunctions, CAST_FUNCTIONS[self.type])
cast_function(user, vote_form)
2021-03-19 16:08:02 +01:00
def tally(self):
tally_function = getattr(TallyFunctions, TALLY_FUNCTIONS[self.type])
tally_function(self)
2020-12-19 23:48:18 +01:00
class Meta:
ordering = ["id"]
2020-11-19 18:40:22 +01:00
class Option(models.Model):
2020-11-20 14:55:31 +01:00
question = models.ForeignKey(
Question, related_name="options", on_delete=models.CASCADE
)
2020-11-19 18:40:22 +01:00
text = models.TextField(_("texte"), blank=False)
2020-11-20 14:55:31 +01:00
voters = models.ManyToManyField(
2020-12-20 18:50:38 +01:00
settings.AUTH_USER_MODEL,
2020-11-20 14:55:31 +01:00
related_name="votes",
through="Vote",
2021-03-18 14:49:48 +01:00
blank=True,
2020-11-20 17:45:15 +01:00
)
# 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)
2020-12-19 23:48:18 +01:00
class Meta:
ordering = ["id"]
2020-12-20 18:50:38 +01:00
class Vote(models.Model):
option = models.ForeignKey(Option, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
2020-12-20 18:50:38 +01:00
# #############################################################################
# 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,
)
2020-12-23 18:04:39 +01:00
full_name = models.CharField(_("Nom et Prénom"), max_length=150, blank=True)
2020-12-20 18:50:38 +01:00
def get_username(self):
return "__".join(self.username.split("__")[1:])
2020-12-21 00:07:07 +01:00
def can_vote(self, request, election):
2020-12-20 18:50:38 +01:00
# 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")
2020-12-20 18:50:38 +01:00
# Pour les élections restreintes, il faut y être associé
return election.restricted and (self.election == election)
2020-12-21 00:07:07 +01:00
def get_prefix(self):
return self.username.split("__")[0]
@property
2020-12-21 00:07:07 +01:00
def connection_method(self):
2021-01-27 14:55:28 +01:00
method = self.username.split("__")[0]
return CONNECTION_METHODS.get(method, _("identifiants spécifiques"))
class Meta:
permissions = [
("is_admin", _("Peut administrer des élections")),
]