From 5a0cf58d8a75c605c57ad15685fe24e98a3b1706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 8 May 2020 16:34:35 +0200 Subject: [PATCH] Events: more validation & uniqueness constraints --- events/models.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/events/models.py b/events/models.py index 9b166599..99e97a97 100644 --- a/events/models.py +++ b/events/models.py @@ -29,6 +29,7 @@ option in this case). """ from django.contrib.auth import get_user_model +from django.core.exceptions import ValidationError from django.db import models from django.utils.translation import gettext_lazy as _ @@ -73,6 +74,7 @@ class Option(models.Model): class Meta: verbose_name = _("option d'événement") verbose_name_plural = _("options d'événement") + unique_together = [["event", "name"]] def __str__(self): return self.name @@ -87,6 +89,7 @@ class OptionChoice(models.Model): class Meta: verbose_name = _("choix d'option d'événement") verbose_name_plural = _("choix d'option d'événement") + unique_together = [["option", "choice"]] def __str__(self): return self.choice @@ -114,6 +117,9 @@ class ExtraField(models.Model): name = models.CharField(_("champ d'événement supplémentaire"), max_length=200) field_type = models.CharField(_("type de champ"), max_length=9, choices=FIELD_TYPE) + class Meta: + unique_together = [["event", "name"]] + class ExtraFieldContent(models.Model): """Value entered in an extra field.""" @@ -124,9 +130,16 @@ class ExtraFieldContent(models.Model): ) content = models.TextField(_("contenu du champ")) + def clean(self): + if self.registration.event != self.field.event: + raise ValidationError( + _("Inscription et champ texte incohérents pour ce commentaire") + ) + class Meta: verbose_name = _("contenu d'un champ événement supplémentaire") verbose_name_plural = _("contenus d'un champ événement supplémentaire") + unique_together = [["field", "registration"]] def __str__(self): max_length = 50 @@ -146,6 +159,7 @@ class Registration(models.Model): class Meta: verbose_name = _("inscription à un événement") verbose_name_plural = _("inscriptions à un événement") + unique_together = [["event", "user"]] def __str__(self): return "inscription de {} à {}".format(self.user, self.event)