Skip to content

Commit

Permalink
Ajout d'un contrainte d'unicité sur les emails des personnes #577
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehenry authored Mar 15, 2022
2 parents 6cbc9e9 + 45f2743 commit 2737ba6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
11 changes: 10 additions & 1 deletion aidants_connect_habilitation/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Tuple

from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
from django.forms import (
BaseModelFormSet,
BooleanField,
Expand Down Expand Up @@ -206,6 +206,15 @@ class AidantRequestForm(PatchedErrorListForm):
class Meta:
model = AidantRequest
exclude = ["organisation"]
error_messages = {
NON_FIELD_ERRORS: {
"unique_together": (
"Il y a déjà un aidant avec la même adresse e-mail dans "
"cette organisation. Chaque aidant doit avoir son propre "
"e-mail nominatif."
),
}
}


class BaseAidantRequestFormSet(BaseModelFormSet):
Expand Down
47 changes: 47 additions & 0 deletions aidants_connect_habilitation/migrations/0012_auto_20220314_1530.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated by Django 3.2.12 on 2022-03-14 14:30

from django.db import migrations

import aidants_connect_habilitation.models


class Migration(migrations.Migration):

dependencies = [
('aidants_connect_habilitation', '0011_alter_organisationrequest_status'),
]

operations = [
migrations.AlterModelOptions(
name='dataprivacyofficer',
options={'verbose_name': 'DPO', 'verbose_name_plural': 'DPOs'},
),
migrations.AlterModelOptions(
name='manager',
options={'verbose_name': 'Responsable structure', 'verbose_name_plural': 'Responsables structure'},
),
migrations.AlterField(
model_name='aidantrequest',
name='email',
field=aidants_connect_habilitation.models.PersonEmailField(max_length=150, verbose_name='Email nominatif'),
),
migrations.AlterField(
model_name='dataprivacyofficer',
name='email',
field=aidants_connect_habilitation.models.PersonEmailField(max_length=150, verbose_name='Email nominatif'),
),
migrations.AlterField(
model_name='issuer',
name='email',
field=aidants_connect_habilitation.models.PersonEmailField(max_length=150, unique=True, verbose_name='Email nominatif'),
),
migrations.AlterField(
model_name='manager',
name='email',
field=aidants_connect_habilitation.models.PersonEmailField(max_length=150, verbose_name='Email nominatif'),
),
migrations.AlterUniqueTogether(
name='aidantrequest',
unique_together={('email', 'organisation')},
),
]
11 changes: 10 additions & 1 deletion aidants_connect_habilitation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ def _new_uuid():
return uuid4()


class PersonEmailField(models.EmailField):
def __init__(self, **kwargs):
kwargs["max_length"] = 150
kwargs["verbose_name"] = "Email nominatif"
super().__init__(**kwargs)


class Person(models.Model):
first_name = models.CharField("Prénom", max_length=150)
last_name = models.CharField("Nom", max_length=150)
email = models.EmailField("Email nominatif", max_length=150)
email = PersonEmailField()
profession = models.CharField("Profession", blank=False, max_length=150)

def __str__(self):
Expand All @@ -60,6 +67,7 @@ class Meta:


class Issuer(PersonWithResponsibilities):
email = PersonEmailField(unique=True)
issuer_id = models.UUIDField(
"Identifiant de demandeur", default=_new_uuid, unique=True
)
Expand Down Expand Up @@ -332,6 +340,7 @@ def draft_id(self):
class Meta:
verbose_name = "aidant à habiliter"
verbose_name_plural = "aidants à habiliter"
unique_together = (("email", "organisation"),)


class RequestMessage(models.Model):
Expand Down
6 changes: 1 addition & 5 deletions aidants_connect_habilitation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,12 @@ def test_template(self):

def test_on_correct_issuer_id_post_updates_model(self):
new_name = Faker("first_name").evaluate(None, None, {"locale": DEFAULT_LOCALE})
form = IssuerForm(data={**model_to_dict(self.issuer), "first_name": new_name})

if not form.is_valid():
raise ValueError(str(form.errors))

self.assertNotEqual(self.issuer.first_name, new_name)

response = self.client.post(
reverse(self.pattern_name, kwargs={"issuer_id": self.issuer.issuer_id}),
form.clean(),
{**model_to_dict(self.issuer), "first_name": new_name},
)

self.assertRedirects(
Expand Down

0 comments on commit 2737ba6

Please sign in to comment.