Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Correspondance schéma d·i - profils et besoins pour thématiques logement-hebergement #393

Merged
9 changes: 8 additions & 1 deletion dora/data_inclusion/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import requests
from django.conf import settings

from .constants import THEMATIQUES_MAPPING_DORA_TO_DI

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -120,7 +122,12 @@ def search_services(
url.args["code_insee"] = code_insee

if thematiques is not None:
url.args["thematiques"] = thematiques
enriched_thematiques = []
for thematique in thematiques:
enriched_thematiques += THEMATIQUES_MAPPING_DORA_TO_DI.get(
thematique, [thematique]
)
url.args["thematiques"] = enriched_thematiques

if types is not None:
url.args["types"] = types
Expand Down
15 changes: 15 additions & 0 deletions dora/data_inclusion/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from collections import defaultdict

# À une thématique DI correspond une thématique Dora
THEMATIQUES_MAPPING_DI_TO_DORA = {
"logement-hebergement--etre-accompagne-dans-son-projet-accession": "logement-hebergement--etre-accompagne-pour-se-loger",
"logement-hebergement--etre-accompagne-en cas-de-difficultes-financieres": "logement-hebergement--gerer-son-budget",
"logement-hebergement--financer-son-projet-travaux": "logement-hebergement--autre",
}

# Inversion du dictionnaire
# À une thématique Dora correspond une liste de thématiques DI
THEMATIQUES_MAPPING_DORA_TO_DI = defaultdict(list)
for key, value in THEMATIQUES_MAPPING_DI_TO_DORA.items():
if not value.endswith("--autre"):
THEMATIQUES_MAPPING_DORA_TO_DI[value].append(key)
14 changes: 8 additions & 6 deletions dora/data_inclusion/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
get_update_status,
)

from .constants import THEMATIQUES_MAPPING_DI_TO_DORA

DI_TO_DORA_DIFFUSION_ZONE_TYPE_MAPPING = {
"commune": "city",
"epci": "epci",
Expand Down Expand Up @@ -114,12 +116,12 @@ def map_service(service_data: dict, is_authenticated: bool) -> dict:
categories = None
subcategories = None
if service_data["thematiques"] is not None:
categories = ServiceCategory.objects.filter(
value__in=service_data["thematiques"]
)
subcategories = ServiceSubCategory.objects.filter(
value__in=service_data["thematiques"]
)
thematiques = [
THEMATIQUES_MAPPING_DI_TO_DORA.get(thematique, thematique)
for thematique in service_data["thematiques"]
]
categories = ServiceCategory.objects.filter(value__in=thematiques)
subcategories = ServiceSubCategory.objects.filter(value__in=thematiques)

location_kinds = None
if service_data["modes_accueil"] is not None:
Expand Down
9 changes: 8 additions & 1 deletion dora/data_inclusion/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Optional
from uuid import uuid4

from .constants import THEMATIQUES_MAPPING_DORA_TO_DI


def make_di_service_data(**kwargs) -> dict:
return {
Expand Down Expand Up @@ -99,13 +101,18 @@ def search_services(
services = [r for r in services if r["source"] in sources]

if thematiques is not None:
enriched_thematiques = []
for thematique in thematiques:
enriched_thematiques += THEMATIQUES_MAPPING_DORA_TO_DI.get(
thematique, [thematique]
)
services = [
r
for r in services
if any(
t.startswith(requested_thematique)
for t in r["thematiques"]
for requested_thematique in thematiques
for requested_thematique in enriched_thematiques
)
]

Expand Down
36 changes: 36 additions & 0 deletions dora/data_inclusion/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from .constants import THEMATIQUES_MAPPING_DI_TO_DORA, THEMATIQUES_MAPPING_DORA_TO_DI
from .mappings import map_service
from .test_utils import FakeDataInclusionClient, make_di_service_data


def test_map_service_thematiques_mapping():
input_thematiques = [
"logement-hebergement",
"logement-hebergement--connaissance-de-ses-droits-et-interlocuteurs",
"logement-hebergement--besoin-dadapter-mon-logement",
] + list(THEMATIQUES_MAPPING_DI_TO_DORA.keys())

expected_categories = ["logement-hebergement"]
expected_subcategories = [
"logement-hebergement--connaissance-de-ses-droits-et-interlocuteurs",
"logement-hebergement--besoin-dadapter-mon-logement",
] + list(THEMATIQUES_MAPPING_DI_TO_DORA.values())

di_service_data = make_di_service_data(thematiques=input_thematiques)
service = map_service(di_service_data, False)

assert sorted(service["categories"]) == sorted(expected_categories)
assert sorted(service["subcategories"]) == sorted(expected_subcategories)


def test_di_client_search_thematiques_mapping():
input_thematique = list(THEMATIQUES_MAPPING_DORA_TO_DI.keys())[0]
output_thematique = list(THEMATIQUES_MAPPING_DORA_TO_DI.values())[0][0]

di_client = FakeDataInclusionClient()
di_service_data = make_di_service_data(thematiques=[output_thematique])
di_client.services.append(di_service_data)

results = di_client.search_services(thematiques=[input_thematique])

assert len(results) == 1