2024-07-10 13:51:24 +02:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2021-04-15 17:01:17 +02:00
|
|
|
from translated_fields import TranslatedFieldWithFallback
|
|
|
|
|
2020-12-20 18:50:38 +01:00
|
|
|
from django.conf import settings
|
2020-12-20 17:15:37 +01:00
|
|
|
from django.contrib.auth.models import AbstractUser
|
2021-04-09 03:31:43 +02:00
|
|
|
from django.db import models, transaction
|
2024-07-10 13:51:24 +02:00
|
|
|
from django.http.request import HttpRequest
|
2021-06-10 21:13:48 +02:00
|
|
|
from django.template.loader import render_to_string
|
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-07-13 02:41:32 +02:00
|
|
|
from shared.auth import CONNECTION_METHODS
|
2024-07-11 14:54:56 +02:00
|
|
|
from shared.auth.utils import generate_password
|
2021-09-16 16:43:27 +02:00
|
|
|
from shared.json import Serializer
|
2021-05-29 19:48:13 +02:00
|
|
|
from shared.utils import choices_length
|
|
|
|
|
2021-03-19 16:08:02 +01:00
|
|
|
from .staticdefs import (
|
2021-03-29 12:42:34 +02:00
|
|
|
BALLOT_TYPE,
|
2021-03-19 16:08:02 +01:00
|
|
|
CAST_FUNCTIONS,
|
|
|
|
QUESTION_TYPES,
|
|
|
|
TALLY_FUNCTIONS,
|
2021-03-19 22:24:27 +01:00
|
|
|
VALIDATE_FUNCTIONS,
|
2021-03-19 16:08:02 +01:00
|
|
|
)
|
2021-03-29 20:35:34 +02:00
|
|
|
from .utils import (
|
2021-04-17 00:23:33 +02:00
|
|
|
BallotsData,
|
2021-03-29 20:35:34 +02:00
|
|
|
CastFunctions,
|
|
|
|
ResultsData,
|
|
|
|
TallyFunctions,
|
|
|
|
ValidateFunctions,
|
|
|
|
)
|
2021-01-27 14:55:28 +01:00
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from django.db.models.fields.related_descriptors import ManyRelatedManager
|
|
|
|
from django.utils.functional import _StrPromise
|
|
|
|
|
|
|
|
|
2020-12-20 17:15:37 +01:00
|
|
|
# #############################################################################
|
|
|
|
# Models regarding an election
|
|
|
|
# #############################################################################
|
2020-11-19 18:40:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Election(models.Model):
|
2024-07-10 13:51:24 +02:00
|
|
|
registered_voters: models.Manager["User"]
|
|
|
|
questions: models.Manager["Question"]
|
|
|
|
|
2021-04-15 17:01:17 +02:00
|
|
|
name = TranslatedFieldWithFallback(models.CharField(_("nom"), max_length=255))
|
2020-11-19 18:40:22 +01:00
|
|
|
short_name = models.SlugField(_("nom bref"), unique=True)
|
2021-04-15 17:01:17 +02:00
|
|
|
description = TranslatedFieldWithFallback(
|
|
|
|
models.TextField(_("description"), blank=True)
|
|
|
|
)
|
2020-11-19 18:40:22 +01:00
|
|
|
|
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
|
|
|
|
2021-06-17 11:53:09 +02:00
|
|
|
visible = models.BooleanField(_("visible au public"), default=False)
|
|
|
|
|
2021-04-15 17:01:17 +02:00
|
|
|
vote_restrictions = TranslatedFieldWithFallback(
|
|
|
|
models.TextField(_("conditions de vote"), blank=True)
|
|
|
|
)
|
2021-04-14 01:59:23 +02:00
|
|
|
|
2020-12-20 18:50:38 +01:00
|
|
|
restricted = models.BooleanField(
|
|
|
|
_("restreint le vote à une liste de personnes"), default=True
|
|
|
|
)
|
|
|
|
|
2020-12-24 00:41:29 +01:00
|
|
|
sent_mail = models.BooleanField(
|
2021-08-20 00:45:33 +02:00
|
|
|
_("mail avec les identifiants envoyé"), null=True, default=False
|
2020-12-24 00:41:29 +01:00
|
|
|
)
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-12-24 01:41:29 +01:00
|
|
|
voters = models.ManyToManyField(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
related_name="cast_elections",
|
2021-03-18 14:49:48 +01:00
|
|
|
blank=True,
|
2020-12-24 01:41:29 +01:00
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
|
2021-06-28 22:43:35 +02:00
|
|
|
time_tallied = models.DateTimeField(
|
|
|
|
_("date du dépouillement"), null=True, default=None
|
|
|
|
)
|
|
|
|
time_published = models.DateTimeField(
|
|
|
|
_("date de publication"), null=True, default=None
|
|
|
|
)
|
|
|
|
|
2020-12-19 23:48:18 +01:00
|
|
|
class Meta:
|
2021-05-30 00:40:32 +02:00
|
|
|
permissions = [
|
2021-07-13 02:41:32 +02:00
|
|
|
("election_admin", _("Peut administrer des élections")),
|
2021-05-30 00:40:32 +02:00
|
|
|
]
|
2020-12-19 23:48:18 +01:00
|
|
|
ordering = ["-start_date", "-end_date"]
|
|
|
|
|
2020-11-19 18:40:22 +01:00
|
|
|
|
2021-09-16 16:43:27 +02:00
|
|
|
class Question(Serializer, models.Model):
|
2024-07-10 13:51:24 +02:00
|
|
|
options: models.Manager["Option"]
|
|
|
|
duels: models.Manager["Duel"]
|
|
|
|
|
2020-11-20 14:55:31 +01:00
|
|
|
election = models.ForeignKey(
|
|
|
|
Election, related_name="questions", on_delete=models.CASCADE
|
|
|
|
)
|
2021-06-14 14:42:36 +02:00
|
|
|
text = TranslatedFieldWithFallback(
|
|
|
|
models.TextField(_("question"), blank=True, default="")
|
|
|
|
)
|
2021-03-19 11:48:38 +01:00
|
|
|
type = models.CharField(
|
|
|
|
_("type de question"),
|
|
|
|
choices=QUESTION_TYPES,
|
|
|
|
default="assentiment",
|
|
|
|
max_length=choices_length(QUESTION_TYPES),
|
|
|
|
)
|
2020-12-19 18:26:25 +01:00
|
|
|
# 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
|
|
|
|
2020-12-24 01:41:29 +01:00
|
|
|
voters = models.ManyToManyField(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
related_name="cast_questions",
|
2021-03-18 14:49:48 +01:00
|
|
|
blank=True,
|
2020-12-24 01:41:29 +01:00
|
|
|
)
|
|
|
|
|
2021-09-16 16:43:27 +02:00
|
|
|
serializable_fields = ["text_en", "text_fr", "type"]
|
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
def is_form_valid(self, vote_form) -> bool:
|
2021-03-19 22:24:27 +01:00
|
|
|
validate_function = getattr(ValidateFunctions, VALIDATE_FUNCTIONS[self.type])
|
|
|
|
return vote_form.is_valid() and validate_function(vote_form)
|
|
|
|
|
2021-04-09 03:31:43 +02:00
|
|
|
@transaction.atomic
|
2024-07-10 13:51:24 +02:00
|
|
|
def cast_ballot(self, user: "User", vote_form) -> None:
|
2021-03-19 14:25:13 +01:00
|
|
|
cast_function = getattr(CastFunctions, CAST_FUNCTIONS[self.type])
|
|
|
|
cast_function(user, vote_form)
|
|
|
|
|
2021-04-09 03:31:43 +02:00
|
|
|
@transaction.atomic
|
2024-07-10 13:51:24 +02:00
|
|
|
def tally(self) -> None:
|
2021-03-19 16:08:02 +01:00
|
|
|
tally_function = getattr(TallyFunctions, TALLY_FUNCTIONS[self.type])
|
|
|
|
tally_function(self)
|
|
|
|
|
2024-07-11 14:54:56 +02:00
|
|
|
@transaction.atomic
|
|
|
|
def pseudonymize(self):
|
|
|
|
"""
|
|
|
|
Generates a random id for each voter
|
|
|
|
"""
|
|
|
|
|
|
|
|
options = list(self.options.prefetch_related("vote_set"))
|
|
|
|
votes: set[Vote] = set()
|
|
|
|
|
|
|
|
for v in self.voters.all():
|
|
|
|
pseudonym = generate_password(16)
|
|
|
|
|
|
|
|
for opt in options:
|
|
|
|
for vote in opt.vote_set.filter(user=v):
|
|
|
|
vote.pseudonymous_user = pseudonym
|
|
|
|
vote.user = None
|
|
|
|
votes.add(vote)
|
|
|
|
|
|
|
|
Vote.objects.bulk_update(votes, ["pseudonymous_user", "user"])
|
|
|
|
|
2021-06-10 21:13:48 +02:00
|
|
|
@property
|
2024-07-10 13:51:24 +02:00
|
|
|
def results(self) -> str:
|
2021-06-10 21:13:48 +02:00
|
|
|
return render_to_string(
|
|
|
|
f"elections/results/{self.vote_type}_export.txt", {"question": self}
|
|
|
|
)
|
|
|
|
|
2021-03-29 12:42:34 +02:00
|
|
|
def get_formset(self):
|
|
|
|
from .forms import BallotFormset # Avoid circular imports
|
|
|
|
|
|
|
|
return getattr(BallotFormset, BALLOT_TYPE[self.type])
|
|
|
|
|
2021-03-29 20:35:34 +02:00
|
|
|
def get_results_data(self):
|
|
|
|
results_function = getattr(ResultsData, BALLOT_TYPE[self.type])
|
|
|
|
return results_function(self)
|
|
|
|
|
2021-04-17 00:23:33 +02:00
|
|
|
def display_ballots(self):
|
|
|
|
display_function = getattr(BallotsData, BALLOT_TYPE[self.type])
|
|
|
|
return display_function(self)
|
|
|
|
|
2021-03-29 20:35:34 +02:00
|
|
|
@property
|
|
|
|
def vote_type(self):
|
|
|
|
return BALLOT_TYPE[self.type]
|
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return str(self.text)
|
2021-03-29 20:35:34 +02:00
|
|
|
|
2020-12-19 23:48:18 +01:00
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
2020-11-19 18:40:22 +01:00
|
|
|
|
2021-09-16 16:43:27 +02:00
|
|
|
class Option(Serializer, models.Model):
|
2024-07-11 14:54:56 +02:00
|
|
|
vote_set: models.Manager["Vote"]
|
|
|
|
|
2020-11-20 14:55:31 +01:00
|
|
|
question = models.ForeignKey(
|
|
|
|
Question, related_name="options", on_delete=models.CASCADE
|
|
|
|
)
|
2021-04-15 17:01:17 +02:00
|
|
|
text = TranslatedFieldWithFallback(models.TextField(_("texte"), blank=False))
|
2021-04-20 10:04:16 +02:00
|
|
|
abbreviation = models.CharField(_("abréviation"), max_length=3, blank=True)
|
2021-03-19 11:48:38 +01:00
|
|
|
|
2021-04-04 23:38:21 +02:00
|
|
|
winner = models.BooleanField(_("option gagnante"), default=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",
|
2024-07-10 13:51:24 +02:00
|
|
|
through="elections.Vote",
|
2021-03-18 14:49:48 +01:00
|
|
|
blank=True,
|
2020-11-20 17:45:15 +01:00
|
|
|
)
|
2020-12-19 18:26:25 +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
|
|
|
|
2021-09-16 16:43:27 +02:00
|
|
|
serializable_fields = ["text_fr", "text_en", "abbreviation"]
|
|
|
|
|
2021-04-17 02:00:34 +02:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
# On enlève les espaces et on passe tout en majuscules
|
|
|
|
self.abbreviation = "".join(self.abbreviation.upper().split())
|
|
|
|
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
def get_abbr(self, default: str) -> str:
|
2021-06-28 21:33:07 +02:00
|
|
|
return self.abbreviation or default
|
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
def __str__(self) -> str:
|
2021-04-17 02:00:34 +02:00
|
|
|
if self.abbreviation:
|
2024-07-10 13:51:24 +02:00
|
|
|
return f"{self.abbreviation} - {self.text}"
|
|
|
|
return str(self.text)
|
2021-03-29 20:35:34 +02:00
|
|
|
|
2020-12-19 23:48:18 +01:00
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
2020-12-20 18:50:38 +01:00
|
|
|
|
|
|
|
|
2021-03-18 14:45:06 +01:00
|
|
|
class Vote(models.Model):
|
|
|
|
option = models.ForeignKey(Option, on_delete=models.CASCADE)
|
2024-07-11 14:54:56 +02:00
|
|
|
user = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True
|
|
|
|
)
|
|
|
|
pseudonymous_user = models.CharField(max_length=16, blank=True)
|
2021-03-18 14:45:06 +01:00
|
|
|
|
2021-03-29 18:04:54 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ["option"]
|
|
|
|
|
2021-03-18 14:45:06 +01:00
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
class RankedVote(Vote):
|
|
|
|
rank: "Rank"
|
|
|
|
|
2024-07-11 14:54:56 +02:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
|
2021-03-29 12:42:34 +02:00
|
|
|
class Rank(models.Model):
|
|
|
|
vote = models.OneToOneField(Vote, on_delete=models.CASCADE)
|
|
|
|
rank = models.PositiveSmallIntegerField(_("rang de l'option"))
|
|
|
|
|
2021-03-29 18:04:54 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ["vote"]
|
|
|
|
|
2021-03-29 12:42:34 +02:00
|
|
|
|
2021-03-29 18:04:54 +02:00
|
|
|
class Duel(models.Model):
|
|
|
|
question = models.ForeignKey(
|
|
|
|
Question, related_name="duels", on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
winner = models.ForeignKey(
|
2021-03-29 12:42:34 +02:00
|
|
|
Option, related_name="contests_won", on_delete=models.CASCADE
|
|
|
|
)
|
2021-03-29 18:04:54 +02:00
|
|
|
loser = models.ForeignKey(
|
2021-03-29 12:42:34 +02:00
|
|
|
Option, related_name="contests_lost", on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
amount = models.PositiveSmallIntegerField(_("votes supplémentaires"))
|
|
|
|
|
|
|
|
|
2020-12-20 18:50:38 +01:00
|
|
|
# #############################################################################
|
|
|
|
# Modification of the base User Model
|
|
|
|
# #############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
class User(AbstractUser):
|
2024-07-10 13:51:24 +02:00
|
|
|
cast_elections: "ManyRelatedManager[Election]"
|
|
|
|
cast_questions: "ManyRelatedManager[Question]"
|
|
|
|
votes: "ManyRelatedManager[Vote]"
|
|
|
|
|
2020-12-20 18:50:38 +01:00
|
|
|
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)
|
2021-08-20 00:45:33 +02:00
|
|
|
has_valid_email = models.BooleanField(_("email valide"), null=True, default=None)
|
2020-12-20 18:50:38 +01:00
|
|
|
|
2021-04-26 17:54:07 +02:00
|
|
|
@property
|
2024-07-10 13:51:24 +02:00
|
|
|
def base_username(self) -> str:
|
2021-01-26 15:35:10 +01:00
|
|
|
return "__".join(self.username.split("__")[1:])
|
2020-12-21 00:07:07 +01:00
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
def can_vote(self, request: HttpRequest, election: Election) -> bool:
|
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:
|
2021-01-27 20:54:23 +01:00
|
|
|
# If the user is connected via CAS, request.session["CASCONNECTED"] is set
|
|
|
|
# to True by authens
|
2024-07-10 13:51:24 +02:00
|
|
|
return not election.restricted and request.session.get(
|
|
|
|
"CASCONNECTED", False
|
|
|
|
)
|
2021-01-27 20:54:23 +01:00
|
|
|
|
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
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
def is_admin(self, election: Election) -> bool:
|
2022-04-02 21:24:23 +02:00
|
|
|
return election.created_by == self or self.is_staff
|
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
def get_prefix(self) -> str:
|
2021-01-27 20:54:23 +01:00
|
|
|
return self.username.split("__")[0]
|
|
|
|
|
2021-03-21 10:01:50 +01:00
|
|
|
@property
|
2024-07-10 13:51:24 +02:00
|
|
|
def connection_method(self) -> "_StrPromise":
|
2021-01-27 14:55:28 +01:00
|
|
|
method = self.username.split("__")[0]
|
|
|
|
return CONNECTION_METHODS.get(method, _("identifiants spécifiques"))
|
2021-03-20 20:07:12 +01:00
|
|
|
|
|
|
|
class Meta:
|
2021-03-31 13:16:10 +02:00
|
|
|
ordering = ["username"]
|