Enforce clipper uniqueness

This commit is contained in:
Martin Pépin 2019-01-14 22:46:16 +01:00
parent 2140a59777
commit d85eeb5801
2 changed files with 48 additions and 1 deletions

View file

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-01-14 21:36
from __future__ import unicode_literals
from django.db import migrations, models
def empty_clippers_to_null(apps, schema_editor):
CofProfile = apps.get_model("gestioncof", "CofProfile")
CofProfile.objects.filter(login_clipper="").update(login_clipper=None)
def null_clippers_to_empty(apps, schema_editor):
CofProfile = apps.get_model("gestioncof", "CofProfile")
CofProfile.objects.filter(login_clipper__isnull=True).update(login_clipper="")
class Migration(migrations.Migration):
dependencies = [("gestioncof", "0015_psql_choices_niveaux")]
operations = [
# First, only set null to True (unique is still False)
migrations.AlterField(
model_name="cofprofile",
name="login_clipper",
field=models.CharField(
blank=True, max_length=32, null=True, verbose_name="Login clipper"
),
),
# Then, set all empty login_clippers to null
migrations.RunPython(empty_clippers_to_null),
# Finally set unique to True
migrations.AlterField(
model_name="cofprofile",
name="login_clipper",
field=models.CharField(
blank=True,
max_length=32,
null=True,
unique=True,
verbose_name="Login clipper",
),
),
]

View file

@ -46,7 +46,9 @@ class CofProfile(models.Model):
)
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
login_clipper = models.CharField("Login clipper", max_length=32, blank=True)
login_clipper = models.CharField(
"Login clipper", max_length=32, blank=True, unique=True, null=True
)
is_cof = models.BooleanField("Membre du COF", default=False)
phone = models.CharField("Téléphone", max_length=20, blank=True)
occupation = models.CharField(