Skip to content

Commit

Permalink
Merge pull request #236 from digital-land/entityRowValuesToLinks
Browse files Browse the repository at this point in the history
Entity row values to links
  • Loading branch information
GeorgeGoodall authored Jan 24, 2024
2 parents 6ec1a7a + b275bae commit 0f06598
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 9 deletions.
4 changes: 1 addition & 3 deletions application/data_access/digital_land_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def get_datasets_with_data_by_typology(
DatasetOrm,
func.count(func.distinct(EntityOrm.entity).label(("entity_count"))),
)
query = query.filter(
DatasetOrm.typology == typology, DatasetOrm.dataset == EntityOrm.dataset
)
query = query.filter(DatasetOrm.typology == typology)
query = query.group_by(DatasetOrm.dataset)
datasets = query.all()
return [DatasetModel.from_orm(ds.DatasetOrm) for ds in datasets]
Expand Down
24 changes: 24 additions & 0 deletions application/data_access/entity_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ def get_entity_search(session: Session, parameters: dict):
return {"params": params, "count": count, "entities": entities}


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.
"""
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):
# exclude any params that match an entity field name but need special handling
excluded = set(["geometry"])
Expand Down
1 change: 0 additions & 1 deletion application/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def home(request: Request):

@app.get("/health", response_class=JSONResponse, include_in_schema=False)
def health(session: Session = Depends(get_session)):

from sqlalchemy.sql import select

try:
Expand Down
26 changes: 25 additions & 1 deletion application/routers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
get_dataset_query,
get_typology_names,
)
from application.data_access.entity_queries import get_entity_query, get_entity_search
from application.data_access.entity_queries import (
get_entity_query,
get_entity_search,
lookup_entity_link,
)
from application.data_access.dataset_queries import get_dataset_names

from application.search.enum import SuffixEntity
Expand Down Expand Up @@ -136,11 +140,31 @@ def get_entity(

dataset = get_dataset_query(session, e.dataset)
organisation_entity, _, _ = get_entity_query(session, e.organisation_entity)

entityLinkFields = [
"article-4-direction",
"permitted-development-rights",
"tree-preservation-order",
]

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
4 changes: 3 additions & 1 deletion 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,6 +66,8 @@
{{ value }}
{% endif %}
</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
41 changes: 41 additions & 0 deletions tests/integration/test_entity_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from application.data_access.entity_queries import lookup_entity_link
from application.db.models import EntityOrm


def test__lookup_entity_link_returns_nothing_when_the_entity_isnt_found(db_session):
linked_entity = lookup_entity_link(
db_session, "a-reference", "article-4-direction", 123
)
assert linked_entity is None


def test__lookup_entity_link_returns_the_looked_up_entity_when_the_link_exists(
db_session,
):
lookup_entity = {
"entity": 106,
"name": "A space",
"entry_date": "2019-01-07",
"start_date": "2019-01-05",
"end_date": "2020-01-07",
"dataset": "article-4-direction",
"json": None,
"organisation_entity": 123,
"prefix": "greenspace",
"reference": "a-reference",
"typology": "geography",
"geometry": "MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))", # noqa: E501
"point": "POINT (-0.33737897872924805 53.74541799747043)",
}

db_session.add(EntityOrm(**lookup_entity))

linked_entity = lookup_entity_link(
db_session, "a-reference", "article-4-direction", 123
)
assert linked_entity["entity"] == lookup_entity["entity"]
assert linked_entity["reference"] == lookup_entity["reference"]
assert linked_entity["dataset"] == lookup_entity["dataset"]
assert linked_entity["typology"] == lookup_entity["typology"]
assert linked_entity["name"] == lookup_entity["name"]
assert linked_entity["reference"] == lookup_entity["reference"]
2 changes: 0 additions & 2 deletions tests/integration/test_entity_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ def test_search_geometry_entity_returns_entities_that_intersect_with_entity(


def test_search_entity_by_curie(test_data, params, db_session):

expected_entity = [
e
for e in test_data["entities"]
Expand All @@ -520,7 +519,6 @@ def test_search_entity_by_curie(test_data, params, db_session):


def test_search_entity_by_organisation_curie(test_data, params, db_session):

expected_entity = [
e
for e in test_data["entities"]
Expand Down

0 comments on commit 0f06598

Please sign in to comment.