(organization) add metadata field

This allows to store custom values which can be reused along the
organization lifetime.
This commit is contained in:
Quentin BEY
2025-03-10 15:59:01 +01:00
committed by BEY Quentin
parent 3aaddc0493
commit 7ce5b28af4
8 changed files with 228 additions and 13 deletions

View File

@@ -31,19 +31,27 @@ class NameFromSiretOrganizationPlugin(BaseOrganizationPlugin):
_api_url = "https://recherche-entreprises.api.gouv.fr/search?q={siret}"
@staticmethod
def get_organization_name_from_results(data, siret):
"""Return the organization name from the results of a SIRET search."""
def get_organization_name_and_metadata_from_results(data, siret):
"""Return the organization name and metadata from the results of a SIRET search."""
org_metadata = {}
for result in data["results"]:
for organization in result["matching_etablissements"]:
if organization.get("siret") == siret:
org_metadata["is_public_service"] = result.get(
"complements", {}
).get("est_service_public", False)
org_metadata["is_commune"] = (
str(result.get("nature_juridique", "")) == "7210"
)
store_signs = organization.get("liste_enseignes") or []
if store_signs:
return store_signs[0].title()
return store_signs[0].title(), org_metadata
if name := result.get("nom_raison_sociale"):
return name.title()
return name.title(), org_metadata
logger.warning("No organization name found for SIRET %s", siret)
return None
return None, org_metadata
def run_after_create(self, organization):
"""After creating an organization, update the organization name."""
@@ -67,15 +75,20 @@ class NameFromSiretOrganizationPlugin(BaseOrganizationPlugin):
response = s.get(self._api_url.format(siret=siret), timeout=10)
response.raise_for_status()
data = response.json()
name = self.get_organization_name_from_results(data, siret)
if not name:
return
except requests.RequestException as exc:
logger.exception("%s: Unable to fetch organization name from SIRET", exc)
return
name, metadata = self.get_organization_name_and_metadata_from_results(
data, siret
)
if not name: # don't consider metadata either
return
organization.name = name
organization.save(update_fields=["name", "updated_at"])
organization.metadata = (organization.metadata or {}) | metadata
organization.save(update_fields=["name", "metadata", "updated_at"])
logger.info("Organization %s name updated to %s", organization, name)
def run_after_grant_access(self, organization_access):

View File

@@ -3,7 +3,7 @@
import pytest
import responses
from core.models import Organization
from core.models import Organization, get_organization_metadata_schema
from core.plugins.loader import get_organization_plugins
pytestmark = pytest.mark.django_db
@@ -30,6 +30,12 @@ def organization_plugins_settings_fixture(settings):
get_organization_plugins.cache_clear()
get_organization_plugins() # call to populate the cache
settings.ORGANIZATION_METADATA_SCHEMA = "fr/organization_metadata.json"
# Reset the model validation cache
get_organization_metadata_schema.cache_clear()
get_organization_metadata_schema()
yield
# reset get_organization_plugins cache
@@ -37,9 +43,26 @@ def organization_plugins_settings_fixture(settings):
get_organization_plugins.cache_clear()
get_organization_plugins() # call to populate the cache
settings.ORGANIZATION_METADATA_SCHEMA = None
# Reset the model validation cache
get_organization_metadata_schema.cache_clear()
get_organization_metadata_schema()
@responses.activate
def test_organization_plugins_run_after_create(organization_plugins_settings):
@pytest.mark.parametrize(
"nature_juridique,is_commune,is_public_service",
[
("123", False, False),
("7210", True, False),
("123", False, True),
("7210", True, True),
],
)
def test_organization_plugins_run_after_create(
organization_plugins_settings, nature_juridique, is_commune, is_public_service
):
"""Test the run_after_create method of the organization plugins for nominal case."""
responses.add(
responses.GET,
@@ -54,7 +77,11 @@ def test_organization_plugins_run_after_create(organization_plugins_settings):
"liste_enseignes": ["AMAZING ORGANIZATION"],
"siret": "12345678901234",
}
]
],
"nature_juridique": nature_juridique,
"complements": {
"est_service_public": is_public_service,
},
}
],
"total_results": 1,
@@ -69,10 +96,14 @@ def test_organization_plugins_run_after_create(organization_plugins_settings):
name="12345678901234", registration_id_list=["12345678901234"]
)
assert organization.name == "Amazing Organization"
assert organization.metadata["is_commune"] == is_commune
assert organization.metadata["is_public_service"] == is_public_service
# Check that the organization has been updated in the database also
organization.refresh_from_db()
assert organization.name == "Amazing Organization"
assert organization.metadata["is_commune"] == is_commune
assert organization.metadata["is_public_service"] == is_public_service
@responses.activate
@@ -157,6 +188,10 @@ def test_organization_plugins_run_after_create_no_list_enseignes(
"siret": "12345678901234",
}
],
"nature_juridique": "123",
"complements": {
"est_service_public": True,
},
}
],
"total_results": 1,
@@ -171,7 +206,11 @@ def test_organization_plugins_run_after_create_no_list_enseignes(
name="12345678901234", registration_id_list=["12345678901234"]
)
assert organization.name == "Amazing Organization"
assert organization.metadata["is_commune"] is False
assert organization.metadata["is_public_service"] is True
# Check that the organization has been updated in the database also
organization.refresh_from_db()
assert organization.name == "Amazing Organization"
assert organization.metadata["is_commune"] is False
assert organization.metadata["is_public_service"] is True