Skip to content

Commit

Permalink
link the entity and display on scree
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Jan 24, 2024
1 parent 8601d01 commit b275bae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
41 changes: 18 additions & 23 deletions application/data_access/entity_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 14 additions & 2 deletions application/routers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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": [],
Expand Down
6 changes: 3 additions & 3 deletions application/templates/components/entity-value/macro.jinja
Original file line number Diff line number Diff line change
@@ -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' %}
Expand Down Expand Up @@ -66,8 +66,8 @@
{{ value }}
{% endif %}
</a>
{%- elif field in ["article-4-direction", "permitted-development-rights", "tree-preservation-order"] %}
<a class ="govuk-link" href="/entity/{{ value.entity }}">{{ value.name }}</a>
{%- elif linked_entities[field] is defined %}
<a class ="govuk-link" href="/entity/{{ linked_entities[field].entity }}">{{ linked_entities[field].reference }}</a>
{%- elif field in ["parliament-thesaurus"] %}
<a class ="govuk-link" href="{{ 'https://lda.data.parliament.uk/terms/' + value }}">{{ value }}</a>
{%- elif field in ["statistical-geography"] %}
Expand Down
2 changes: 1 addition & 1 deletion application/templates/entity.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h1 class="govuk-heading-xl">{{ row_name }}</h1>
{{ entityField(field,fields,dataset_fields) }}
</th>
<td class="govuk-table__cell app-table__cell">
{{ entityValue(field,row[field],fields,dataset_fields,organisation_entity)}}
{{ entityValue(field,row[field],fields,dataset_fields,organisation_entity,linked_entities)}}
</td>
{% if field is not in['dataset','organisation-entity','start-date','end-date','typology'] %}
<td class="govuk-table__cell govuk-!-font-size-14 govuk-!-text-align-right">
Expand Down

0 comments on commit b275bae

Please sign in to comment.