Skip to content

Commit

Permalink
added lookup for "article-4-direction" and "permitted-development-rig…
Browse files Browse the repository at this point in the history
…hts"
  • Loading branch information
GeorgeGoodall committed Jan 16, 2024
1 parent e5e7488 commit 5735104
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
26 changes: 25 additions & 1 deletion application/core/filters.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion application/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions application/core/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion application/templates/components/entity-value/macro.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
<a class ="govuk-link" href="{{ '/prefix/statistical-geography/reference/' + value }}">{{ value }}</a>
{%- elif field in ["combined-authority","local-authority"] %}
<a class ="govuk-link" href="{{ '/prefix/local-authority/reference/' + value }}">{{ value }}</a>
{%- elif field in ["organisation-entity", "flood-risk-type", "flood-risk-level"] %}
{%- elif field in ["organisation-entity"] %}
<a class ="govuk-link" href="{{ '/entity/' + value }}">{{ value|get_entity_name }}</a>
{%- elif field in ["article-4-direction", "permitted-development-rights"] %}
<a class ="govuk-link" href="/entity/{{ value | lookup_entity_custom(field) }}">{{ value }}</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

0 comments on commit 5735104

Please sign in to comment.