diff --git a/application/data_access/entity_queries.py b/application/data_access/entity_queries.py index 485a08ec..53c60653 100644 --- a/application/data_access/entity_queries.py +++ b/application/data_access/entity_queries.py @@ -95,33 +95,28 @@ def get_entity_search(session: Session, parameters: dict): return {"params": params, "count": count, "entities": entities} -def apply_entity_links(session: Session, entity: dict, entityLinkFields: list): +def lookup_entity_link( + session: Session, reference: str, dataset: str, organisation_entity: int +): """ This function takes an entity and a list of fields that are entity links. any entity link fields are then replaced with the entity object. """ - for key in entityLinkFields: - if key in entity: - search_params = { - "reference": [entity[key]], - "dataset": [key], - "organisation-entity": [entity["organisation-entity"]], - } - found_entities = get_entity_search(session, search_params) - if found_entities["count"] == 1: - found_entity = found_entities["entities"][0] - e_dict = found_entity.dict(by_alias=True, exclude={"geojson"}) - entity[key] = e_dict - elif found_entities["count"] > 1: - # Log that multiple entities were found - # set the entity to -1 so the page not found page is shown - entity[key] = {"name": entity[key], "entity": -1} - elif found_entities["count"] == 0: - # Log that no entity was found - # set the entity to -1 so the page not found page is shown - entity[key] = {"name": entity[key], "entity": -1} - - return entity + search_params = { + "reference": [reference], + "dataset": [dataset], + "organisation-entity": [organisation_entity], + } + found_entities = get_entity_search(session, search_params) + if found_entities["count"] == 1: + found_entity = found_entities["entities"][0] + return found_entity.dict(by_alias=True, exclude={"geojson"}) + # elif found_entities["count"] > 1: + # Log that multiple entities were found + # set the entity to -1 so the page not found page is shown + # elif found_entities["count"] == 0: + # Log that no entity was found + # set the entity to -1 so the page not found page is shown def _apply_base_filters(query, params): diff --git a/application/routers/entity.py b/application/routers/entity.py index f3364706..231e0090 100644 --- a/application/routers/entity.py +++ b/application/routers/entity.py @@ -21,7 +21,7 @@ from application.data_access.entity_queries import ( get_entity_query, get_entity_search, - apply_entity_links, + lookup_entity_link, ) from application.data_access.dataset_queries import get_dataset_names @@ -146,13 +146,25 @@ def get_entity( "permitted-development-rights", "tree-preservation-order", ] - e_dict_sorted = apply_entity_links(session, e_dict_sorted, entityLinkFields) + + linked_entities = {} + + # for each entityLinkField, if that key exists in the entity dict, then + # lookup the entity and add it to the linked_entities dict + for field in entityLinkFields: + if field in e_dict_sorted: + linked_entity = lookup_entity_link( + session, e_dict_sorted[field], field, e_dict_sorted["dataset"] + ) + if linked_entity is not None: + linked_entities[field] = linked_entity return templates.TemplateResponse( "entity.html", { "request": request, "row": e_dict_sorted, + "linked_entities": linked_entities, "entity": e, "pipeline_name": e.dataset, "references": [], diff --git a/application/templates/components/entity-value/macro.jinja b/application/templates/components/entity-value/macro.jinja index b90e0d82..fdcdc7c4 100644 --- a/application/templates/components/entity-value/macro.jinja +++ b/application/templates/components/entity-value/macro.jinja @@ -1,4 +1,4 @@ -{% macro entityValue(field,value,field_spec,dataset_fields,organisation_entity) %} +{% macro entityValue(field,value,field_spec,dataset_fields,organisation_entity,linked_entities) %} {% if field_spec and field_spec[field] %} {% if field_spec[field]['typology'] == 'value' %} {% if field_spec[field]['datatype'] == 'url' %} @@ -66,8 +66,8 @@ {{ value }} {% endif %} - {%- elif field in ["article-4-direction", "permitted-development-rights", "tree-preservation-order"] %} - {{ value.name }} + {%- elif linked_entities[field] is defined %} + {{ linked_entities[field].reference }} {%- elif field in ["parliament-thesaurus"] %} {{ value }} {%- elif field in ["statistical-geography"] %} diff --git a/application/templates/entity.html b/application/templates/entity.html index 82d88cd0..d2c7147b 100644 --- a/application/templates/entity.html +++ b/application/templates/entity.html @@ -69,7 +69,7 @@