diff --git a/application/core/filters.py b/application/core/filters.py index 7c41049d..337b0325 100644 --- a/application/core/filters.py +++ b/application/core/filters.py @@ -1,4 +1,4 @@ -from application.data_access.entity_queries import get_entity_query +from application.data_access.entity_queries import get_entity_query, get_entity_search from application.data_access.digital_land_queries import get_dataset_query from application.core.utils import NoneToEmptyStringEncoder from jinja2 import pass_eval_context @@ -176,6 +176,30 @@ def get_entity_name_filter(eval_ctx, id): return id +@pass_eval_context +def lookup_entity_filter(eval_ctx, value, dataset): + "lookup an entity by its name or reference" + search_parameters = { + "reference": [value], + "dataset": [dataset], + } + params, count, entities = get_entity_search(search_parameters).values() + + if len(entities) == 1: + return entities[0].entity + + +@pass_eval_context +def lookup_entity_custom_filter(eval_ctx, value, dataset): + + datasetMapping = {"permitted-development-rights": "permitted-development-right"} + + if dataset in datasetMapping: + dataset = datasetMapping[dataset] + + return lookup_entity_filter(eval_ctx, value, dataset) + + def get_entity_name(entity): if entity.name: return entity.name diff --git a/application/core/models.py b/application/core/models.py index 216d109a..f631ca21 100644 --- a/application/core/models.py +++ b/application/core/models.py @@ -126,7 +126,7 @@ class DatasetPublicationCountModel(DigitalLandBaseModel): def entity_factory(entity_orm: EntityOrm): e = EntityModel.from_orm(entity_orm) - if entity_orm.json is not None: + if hasattr(entity_orm, "json") and entity_orm.json is not None: for key, val in entity_orm.json.items(): setattr(e, key, val) return e diff --git a/application/core/templates.py b/application/core/templates.py index cb7cb7b7..004de104 100644 --- a/application/core/templates.py +++ b/application/core/templates.py @@ -11,6 +11,8 @@ render_markdown, entity_name_filter, get_entity_name_filter, + lookup_entity_filter, + lookup_entity_custom_filter, debug, digital_land_to_json, uri_encode, @@ -73,6 +75,8 @@ def random_int(n=1): templates.env.filters["render_markdown"] = render_markdown templates.env.filters["entity_name"] = entity_name_filter templates.env.filters["get_entity_name"] = get_entity_name_filter +templates.env.filters["lookup_entity"] = lookup_entity_filter +templates.env.filters["lookup_entity_custom"] = lookup_entity_custom_filter templates.env.filters["debug"] = debug templates.env.filters["digital_land_to_json"] = digital_land_to_json templates.env.filters["uri_encode"] = uri_encode diff --git a/application/templates/components/entity-value/macro.jinja b/application/templates/components/entity-value/macro.jinja index b049f462..521f1ba3 100644 --- a/application/templates/components/entity-value/macro.jinja +++ b/application/templates/components/entity-value/macro.jinja @@ -52,8 +52,10 @@ {{ value }} {%- elif field in ["combined-authority","local-authority"] %} {{ value }} - {%- elif field in ["organisation-entity", "flood-risk-type", "flood-risk-level"] %} + {%- elif field in ["organisation-entity"] %} {{ value|get_entity_name }} + {%- elif field in ["article-4-direction", "permitted-development-rights"] %} + {{ value }} {%- elif field in ["parliament-thesaurus"] %} {{ value }} {%- elif field in ["statistical-geography"] %}