kadenios/petitions/models.py

85 lines
2.5 KiB
Python

from datetime import date
from translated_fields import TranslatedFieldWithFallback
from django.contrib.auth import get_user_model
from django.db import models
from django.utils.translation import gettext_lazy as _
from shared.utils import choices_length, token_generator
from .staticdefs import DEPARTMENTS, ELECTED, STATUSES
User = get_user_model()
# #############################################################################
# Petition related models
# #############################################################################
class Petition(models.Model):
title = TranslatedFieldWithFallback(models.CharField(_("titre"), max_length=255))
text = TranslatedFieldWithFallback(models.TextField(_("texte"), blank=True))
letter = TranslatedFieldWithFallback(models.TextField(_("lettre"), blank=True))
created_by = models.ForeignKey(
User,
related_name="petitions_created",
on_delete=models.SET_NULL,
blank=True,
null=True,
)
launch_date = models.DateField(_("date d'ouverture"), default=date.today)
archived = models.BooleanField(_("archivée"), default=False)
class Meta:
permissions = [
("is_admin", _("Peut administrer des pétitions")),
]
ordering = ["-launch_date"]
class Signature(models.Model):
petition = models.ForeignKey(
Petition, related_name="signatures", on_delete=models.CASCADE
)
full_name = models.CharField(_("nom complet"), max_length=255)
email = models.EmailField(_("adresse mail"))
status = models.CharField(
_("statut"),
choices=STATUSES,
max_length=choices_length(STATUSES),
default="autre",
)
elected = models.CharField(
_("poste d'élu"),
choices=ELECTED,
max_length=choices_length(ELECTED),
default="",
blank=True,
)
department = models.CharField(
_("département"),
choices=DEPARTMENTS,
max_length=choices_length(DEPARTMENTS),
default="",
blank=True,
)
verified = models.BooleanField(_("adresse mail vérifiée"), default=False)
valid = models.BooleanField(_("signature vérifiée"), default=False)
token = models.SlugField(_("token"), editable=False, default=token_generator)
timestamp = models.DateTimeField(_("horodatage"), auto_now=True)
class Meta:
constraints = [
models.UniqueConstraint(
fields=["petition", "email"], name="unique_signature"
)
]