From 84c87c1b4bb965fa8500e9dfd3b3172bea1b3448 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Thu, 30 Jun 2022 10:38:25 +0200 Subject: [PATCH] Replace unique_together by UniqueConstraint --- bda/migrations/0020_auto_20220630_1035.py | 23 +++++++ bda/models.py | 6 +- events/migrations/0006_auto_20220630_1035.py | 63 +++++++++++++++++++ events/models.py | 28 +++++++-- .../migrations/0020_auto_20220630_1036.py | 43 +++++++++++++ gestioncof/models.py | 12 +++- petitscours/models.py | 6 +- 7 files changed, 172 insertions(+), 9 deletions(-) create mode 100644 bda/migrations/0020_auto_20220630_1035.py create mode 100644 events/migrations/0006_auto_20220630_1035.py create mode 100644 gestioncof/migrations/0020_auto_20220630_1036.py diff --git a/bda/migrations/0020_auto_20220630_1035.py b/bda/migrations/0020_auto_20220630_1035.py new file mode 100644 index 00000000..43075bfe --- /dev/null +++ b/bda/migrations/0020_auto_20220630_1035.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.13 on 2022-06-30 08:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("bda", "0019_auto_20220628_1621"), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="choixspectacle", + unique_together=set(), + ), + migrations.AddConstraint( + model_name="choixspectacle", + constraint=models.UniqueConstraint( + fields=("participant", "spectacle"), name="unique_participation" + ), + ), + ] diff --git a/bda/models.py b/bda/models.py index 578f235c..9bc2ce3c 100644 --- a/bda/models.py +++ b/bda/models.py @@ -253,7 +253,11 @@ class ChoixSpectacle(models.Model): class Meta: ordering = ("priority",) - unique_together = (("participant", "spectacle"),) + constraints = [ + models.UniqueConstraint( + fields=["participant", "spectacle"], name="unique_participation" + ) + ] verbose_name = "voeu" verbose_name_plural = "voeux" diff --git a/events/migrations/0006_auto_20220630_1035.py b/events/migrations/0006_auto_20220630_1035.py new file mode 100644 index 00000000..f6cffcac --- /dev/null +++ b/events/migrations/0006_auto_20220630_1035.py @@ -0,0 +1,63 @@ +# Generated by Django 3.2.13 on 2022-06-30 08:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("events", "0005_auto_20220628_1621"), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="extrafield", + unique_together=set(), + ), + migrations.AlterUniqueTogether( + name="extrafieldcontent", + unique_together=set(), + ), + migrations.AlterUniqueTogether( + name="option", + unique_together=set(), + ), + migrations.AlterUniqueTogether( + name="optionchoice", + unique_together=set(), + ), + migrations.AlterUniqueTogether( + name="registration", + unique_together=set(), + ), + migrations.AddConstraint( + model_name="extrafield", + constraint=models.UniqueConstraint( + fields=("event", "name"), name="unique_extra_field" + ), + ), + migrations.AddConstraint( + model_name="extrafieldcontent", + constraint=models.UniqueConstraint( + fields=("field", "registration"), name="unique_extra_field_content" + ), + ), + migrations.AddConstraint( + model_name="option", + constraint=models.UniqueConstraint( + fields=("event", "name"), name="unique_event_option" + ), + ), + migrations.AddConstraint( + model_name="optionchoice", + constraint=models.UniqueConstraint( + fields=("option", "choice"), name="unique_option_choice" + ), + ), + migrations.AddConstraint( + model_name="registration", + constraint=models.UniqueConstraint( + fields=("event", "user"), name="unique_registration" + ), + ), + ] diff --git a/events/models.py b/events/models.py index 7b536c86..a421e8a3 100644 --- a/events/models.py +++ b/events/models.py @@ -72,9 +72,13 @@ class Option(models.Model): multi_choices = models.BooleanField(_("choix multiples"), default=False) class Meta: + constraints = [ + models.UniqueConstraint( + fields=["event", "name"], name="unique_event_option" + ) + ] verbose_name = _("option d'événement") verbose_name_plural = _("options d'événement") - unique_together = [["event", "name"]] def __str__(self): return self.name @@ -87,9 +91,13 @@ class OptionChoice(models.Model): choice = models.CharField(_("choix"), max_length=200) class Meta: + constraints = [ + models.UniqueConstraint( + fields=["option", "choice"], name="unique_option_choice" + ) + ] 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 @@ -118,7 +126,9 @@ class ExtraField(models.Model): field_type = models.CharField(_("type de champ"), max_length=9, choices=FIELD_TYPE) class Meta: - unique_together = [["event", "name"]] + constraints = [ + models.UniqueConstraint(fields=["event", "name"], name="unique_extra_field") + ] class ExtraFieldContent(models.Model): @@ -137,9 +147,13 @@ class ExtraFieldContent(models.Model): ) class Meta: + constraints = [ + models.UniqueConstraint( + fields=["field", "registration"], name="unique_extra_field_content" + ) + ] 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 @@ -163,9 +177,13 @@ class Registration(models.Model): ) class Meta: + constraints = [ + models.UniqueConstraint( + fields=["event", "user"], name="unique_registration" + ) + ] 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) diff --git a/gestioncof/migrations/0020_auto_20220630_1036.py b/gestioncof/migrations/0020_auto_20220630_1036.py new file mode 100644 index 00000000..2085f329 --- /dev/null +++ b/gestioncof/migrations/0020_auto_20220630_1036.py @@ -0,0 +1,43 @@ +# Generated by Django 3.2.13 on 2022-06-30 08:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("gestioncof", "0019_auto_20220628_1621"), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="eventregistration", + unique_together=set(), + ), + migrations.AlterUniqueTogether( + name="petitcoursability", + unique_together=set(), + ), + migrations.AlterUniqueTogether( + name="surveyanswer", + unique_together=set(), + ), + migrations.AddConstraint( + model_name="eventregistration", + constraint=models.UniqueConstraint( + fields=("user", "event"), name="unique_event_registration" + ), + ), + migrations.AddConstraint( + model_name="petitcoursability", + constraint=models.UniqueConstraint( + fields=("user", "niveau", "matiere"), name="unique_competence_level" + ), + ), + migrations.AddConstraint( + model_name="surveyanswer", + constraint=models.UniqueConstraint( + fields=("user", "survey"), name="unique_survey_answer" + ), + ), + ] diff --git a/gestioncof/models.py b/gestioncof/models.py index f4a0964c..c6d32efc 100644 --- a/gestioncof/models.py +++ b/gestioncof/models.py @@ -194,8 +194,12 @@ class EventRegistration(models.Model): paid = models.BooleanField("A payé", default=False) class Meta: + constraints = [ + models.UniqueConstraint( + fields=["user", "event"], name="unique_event_registration" + ) + ] verbose_name = "Inscription" - unique_together = ("user", "event") def __str__(self): return "Inscription de {} à {}".format(self.user, self.event.title) @@ -247,8 +251,12 @@ class SurveyAnswer(models.Model): answers = models.ManyToManyField(SurveyQuestionAnswer, related_name="selected_by") class Meta: + constraints = [ + models.UniqueConstraint( + fields=["user", "survey"], name="unique_survey_answer" + ) + ] verbose_name = "Réponses" - unique_together = ("user", "survey") def __str__(self): return "Réponse de %s sondage %s" % ( diff --git a/petitscours/models.py b/petitscours/models.py index e5ee749a..0be81449 100644 --- a/petitscours/models.py +++ b/petitscours/models.py @@ -44,9 +44,13 @@ class PetitCoursAbility(models.Model): class Meta: app_label = "gestioncof" + constraints = [ + models.UniqueConstraint( + fields=["user", "niveau", "matiere"], name="unique_competence_level" + ) + ] verbose_name = "Compétence petits cours" verbose_name_plural = "Compétences des petits cours" - unique_together = ("user", "niveau", "matiere") def __str__(self): return "{:s} - {!s} - {:s}".format(