Skip to content

Commit

Permalink
Switch aidants_connect_habilitation.models.OrganisationRequest.type f…
Browse files Browse the repository at this point in the history
…ield from CharField to fk onaidants_connect_web.models.OrganisationTyp
  • Loading branch information
christophehenry committed Jan 10, 2022
1 parent 0aecbd2 commit b07dbe6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 82 deletions.
32 changes: 16 additions & 16 deletions aidants_connect/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -129,32 +129,32 @@ class AuthorizationDurationChoices(TextChoices):
)


OTHER_ORGANISATION_TYPE = "Autre"


class ChoiceEnum(Enum):
@classmethod
def choices(cls):
return ((item.name, item.value) for item in 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
Expand Down
52 changes: 18 additions & 34 deletions aidants_connect_habilitation/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,7 +9,9 @@ class Migration(migrations.Migration):

initial = True

dependencies = []
dependencies = [
("aidants_connect_web", "0050_auto_20210321_2154"),
]

operations = [
migrations.CreateModel(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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",
),
Expand Down Expand Up @@ -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",
Expand Down
16 changes: 8 additions & 8 deletions aidants_connect_habilitation/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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 (
RequestStatusConstants,
MessageStakeholders,
RequestOriginConstants,
)
from aidants_connect_web.models import OrganisationType


class Issuer(models.Model):
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
)
),
Expand Down
4 changes: 2 additions & 2 deletions aidants_connect_habilitation/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions aidants_connect_habilitation/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
23 changes: 3 additions & 20 deletions aidants_connect_web/migrations/0050_auto_20210321_2154.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit b07dbe6

Please sign in to comment.