diff --git a/dora/data_inclusion/client.py b/dora/data_inclusion/client.py index 9903124e..3b4d1509 100644 --- a/dora/data_inclusion/client.py +++ b/dora/data_inclusion/client.py @@ -7,6 +7,8 @@ import requests from django.conf import settings +from .constants import THEMATIQUES_MAPPING_DORA_TO_DI + logger = logging.getLogger(__name__) @@ -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 diff --git a/dora/data_inclusion/constants.py b/dora/data_inclusion/constants.py new file mode 100644 index 00000000..79350df2 --- /dev/null +++ b/dora/data_inclusion/constants.py @@ -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) diff --git a/dora/data_inclusion/mappings.py b/dora/data_inclusion/mappings.py index 7c1018a9..1d6e943a 100644 --- a/dora/data_inclusion/mappings.py +++ b/dora/data_inclusion/mappings.py @@ -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", @@ -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: diff --git a/dora/data_inclusion/test_utils.py b/dora/data_inclusion/test_utils.py index 52842631..2a80694b 100644 --- a/dora/data_inclusion/test_utils.py +++ b/dora/data_inclusion/test_utils.py @@ -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 { @@ -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 ) ] diff --git a/dora/data_inclusion/tests.py b/dora/data_inclusion/tests.py new file mode 100644 index 00000000..1c8f7820 --- /dev/null +++ b/dora/data_inclusion/tests.py @@ -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