kadenios/elections/models.py

92 lines
3 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
# #############################################################################
# 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
)
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
)
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-20 17:45:15 +01:00
# TODO : cache tally or recompute it each time ?
2020-11-20 14:55:31 +01:00
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)
# 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-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",
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
# #############################################################################
# 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,
)
def can_vote(self, election):
# Si c'est un·e utilisateur·ice CAS, iel peut voter dans les élections
# ouvertes à tou·te·s
if self.election is None:
return not election.restricted
# Pour les élections restreintes, il faut y être associé
return election.restricted and (self.election == election)