diff --git a/aidants_connect/constants.py b/aidants_connect/constants.py index e65ef97cc..093b71f74 100644 --- a/aidants_connect/constants.py +++ b/aidants_connect/constants.py @@ -2,7 +2,7 @@ from enum import unique, Enum from django.conf import settings -from django.db.models import TextChoices +from django.db.models import TextChoices, IntegerChoices from django.utils.timezone import now @@ -129,9 +129,6 @@ class AuthorizationDurationChoices(TextChoices): ) -OTHER_ORGANISATION_TYPE = "Autre" - - class ChoiceEnum(Enum): @classmethod def choices(cls): @@ -139,22 +136,25 @@ def choices(cls): @unique -class RequestOriginConstants(ChoiceEnum): - FRANCE_SERVICE = "France Services/MSAP" - CCAS = "CCAS" - CENTRES_SOCIAUX = "Centres sociaux" - SECRETARIATS_MAIRIE = "Sécrétariats de mairie" - MEDIATHEQUE = "Médiathèque" +class RequestOriginConstants(IntegerChoices): + FRANCE_SERVICE = (1, "France Services/MSAP") + CCAS = (2, "CCAS") + CENTRES_SOCIAUX = (3, "Centres sociaux") + SECRETARIATS_MAIRIE = (4, "Sécrétariats de mairie") + MAISONS_SOLIDARITE = (5, "Maisons départementales des solidarités") + MEDIATHEQUE = (6, "Médiathèque") + GUICHET_AUTRE = (7, "Autre guichet d’accueil de service public de proximité") GUICHET_OPERATEUR = ( - "Guichet d’accueil d’opérateur de service public (CAF, Pôle Emploi, etc.)" + 8, + "Guichet d’accueil d’opérateur de service public (CAF, Pôle Emploi, etc.)", ) - GUICHET_AUTRE = "Autre guichet d’accueil de service public de proximité" AUTRES_ASSOS = ( - "Autres associations d’accompagnement des publics ou de médiation numérique" + 9, + "Autres associations d’accompagnement des publics ou de médiation numérique", ) - SMS = "Structure médico-sociale (CSAPA, CHU, CMS)" - INDEP = "Indépendant" - OTHER = "Autre" + SMS = (10, "Structure médico-sociale (CSAPA, CHU, CMS)") + INDEP = (11, "Indépendant") + OTHER = (12, "Autre") @unique diff --git a/aidants_connect_habilitation/migrations/0001_initial.py b/aidants_connect_habilitation/migrations/0001_initial.py index 93ccd47bd..79fffffa6 100644 --- a/aidants_connect_habilitation/migrations/0001_initial.py +++ b/aidants_connect_habilitation/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.1.13 on 2022-01-06 14:43 +# Generated by Django 3.1.13 on 2022-01-10 11:01 from django.db import migrations, models import django.db.models.deletion @@ -9,7 +9,9 @@ class Migration(migrations.Migration): initial = True - dependencies = [] + dependencies = [ + ("aidants_connect_web", "0050_auto_20210321_2154"), + ] operations = [ migrations.CreateModel( @@ -67,35 +69,6 @@ class Migration(migrations.Migration): verbose_name="État", ), ), - ( - "type", - models.CharField( - choices=[ - ("FRANCE_SERVICE", "France Services/MSAP"), - ("CCAS", "CCAS"), - ("CENTRES_SOCIAUX", "Centres sociaux"), - ("SECRETARIATS_MAIRIE", "Sécrétariats de mairie"), - ("MEDIATHEQUE", "Médiathèque"), - ( - "GUICHET_OPERATEUR", - "Guichet d’accueil d’opérateur de service public (CAF, Pôle Emploi, etc.)", - ), - ( - "GUICHET_AUTRE", - "Autre guichet d’accueil de service public de proximité", - ), - ( - "AUTRES_ASSOS", - "Autres associations d’accompagnement des publics ou de médiation numérique", - ), - ("SMS", "Structure médico-sociale (CSAPA, CHU, CMS)"), - ("INDEP", "Indépendant"), - ("OTHER", "Autre"), - ], - max_length=150, - verbose_name="Type de structure", - ), - ), ( "type_other", models.CharField( @@ -207,6 +180,14 @@ class Migration(migrations.Migration): to="aidants_connect_habilitation.issuer", ), ), + ( + "type", + models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="aidants_connect_web.organisationtype", + ), + ), ], ), migrations.CreateModel( @@ -224,7 +205,7 @@ class Migration(migrations.Migration): ( "sender", models.CharField( - choices=[("AC", "Aidant Connect"), ("ISSUER", "Demandeur")], + choices=[("AC", "Aidants Connect"), ("ISSUER", "Demandeur")], max_length=20, verbose_name="Expéditeur", ), @@ -281,8 +262,11 @@ class Migration(migrations.Migration): model_name="organisationrequest", constraint=models.CheckConstraint( check=models.Q( - models.Q(_negated=True, type="OTHER"), - models.Q(("type", "OTHER"), ("type_other__isnull", False)), + models.Q( + models.Q(_negated=True, type_id=12), + ("type_other__isnull", True), + ), + models.Q(("type_id", 12), ("type_other__isnull", False)), _connector="OR", ), name="type_other_correctly_set", diff --git a/aidants_connect_habilitation/models.py b/aidants_connect_habilitation/models.py index 49339a3b7..474e0f061 100644 --- a/aidants_connect_habilitation/models.py +++ b/aidants_connect_habilitation/models.py @@ -1,5 +1,5 @@ from django.db import models -from django.db.models import Q +from django.db.models import Q, SET_NULL from phonenumber_field.modelfields import PhoneNumberField from aidants_connect.constants import ( @@ -7,6 +7,7 @@ MessageStakeholders, RequestOriginConstants, ) +from aidants_connect_web.models import OrganisationType class Issuer(models.Model): @@ -34,11 +35,7 @@ class OrganisationRequest(models.Model): choices=RequestStatusConstants.choices(), ) - type = models.CharField( - "Type de structure", - max_length=150, - choices=RequestOriginConstants.choices(), - ) + type = models.ForeignKey(OrganisationType, null=True, on_delete=SET_NULL) type_other = models.CharField( "Type de structure si autre", @@ -98,9 +95,12 @@ class Meta: constraints = [ models.CheckConstraint( check=( - ~Q(type=RequestOriginConstants.OTHER.name) + ( + ~Q(type_id=RequestOriginConstants.OTHER.value) + & Q(type_other__isnull=True) + ) | ( - Q(type=RequestOriginConstants.OTHER.name) + Q(type_id=RequestOriginConstants.OTHER.value) & Q(type_other__isnull=False) ) ), diff --git a/aidants_connect_habilitation/tests/factories.py b/aidants_connect_habilitation/tests/factories.py index b6a1cd4f6..3fb0ffb66 100644 --- a/aidants_connect_habilitation/tests/factories.py +++ b/aidants_connect_habilitation/tests/factories.py @@ -18,8 +18,8 @@ class Meta: class OrganisationRequestFactory(factory.DjangoModelFactory): issuer = factory.SubFactory(IssuerFactory) - type = fuzzy.FuzzyChoice( - [x.name for x in RequestOriginConstants if x != RequestOriginConstants.OTHER] + type_id = fuzzy.FuzzyChoice( + [x.value for x in RequestOriginConstants if x != RequestOriginConstants.OTHER] ) name = factory.Faker("company") address = factory.Faker("street_address") diff --git a/aidants_connect_habilitation/tests/test_models.py b/aidants_connect_habilitation/tests/test_models.py index ec42f83c7..7d99c568e 100644 --- a/aidants_connect_habilitation/tests/test_models.py +++ b/aidants_connect_habilitation/tests/test_models.py @@ -9,12 +9,12 @@ class OrganisationRequestTests(TestCase): def test_type_other_correctly_set_constraint(self): OrganisationRequestFactory( - type=RequestOriginConstants.CCAS.name, type_other=None + type_id=RequestOriginConstants.CCAS.value, type_other=None ) with self.assertRaises(IntegrityError) as cm: OrganisationRequestFactory( - type=RequestOriginConstants.OTHER.name, type_other=None + type_id=RequestOriginConstants.OTHER.value, type_other=None ) self.assertIn("type_other_correctly_set", str(cm.exception)) diff --git a/aidants_connect_web/migrations/0050_auto_20210321_2154.py b/aidants_connect_web/migrations/0050_auto_20210321_2154.py index ddcc5e2a3..9e80a1353 100644 --- a/aidants_connect_web/migrations/0050_auto_20210321_2154.py +++ b/aidants_connect_web/migrations/0050_auto_20210321_2154.py @@ -1,29 +1,12 @@ -# Generated by Django 3.1.1 on 2021-03-21 20:54 - from django.db import migrations -from aidants_connect.constants import OTHER_ORGANISATION_TYPE - -orga_types = [ - "France Services/MSAP", - "CCAS", - "Centres sociaux", - "Sécrétariats de mairie", - "Maisons départementales des solidarités", - "Médiathèque", - "Autre guichet d’accueil de service public de proximité", - "Guichet d’accueil d’opérateur de service public (CAF, Pôle Emploi, etc.)", - "Autres associations d’accompagnement des publics ou de médiation numérique", - "Structure médico-sociale (CSAPA, CHU, CMS)", - "Indépendant", - OTHER_ORGANISATION_TYPE, -] +from aidants_connect.constants import RequestOriginConstants def add_organisation_types(apps, schema_editor): OrganisationType = apps.get_model("aidants_connect_web", "OrganisationType") - for one_type in orga_types: - OrganisationType.objects.get_or_create(name=one_type) + for type in RequestOriginConstants: + OrganisationType.objects.get_or_create(pk=type.value, name=type.label) class Migration(migrations.Migration):