From 7041fcf5ce51000f066ab115fb3bbd3f514fe201 Mon Sep 17 00:00:00 2001 From: Greg Barnes Date: Mon, 16 Dec 2024 17:50:26 +0000 Subject: [PATCH] Reformats and displays related legislation as titles rather than urls --- app/search/templates/document.html | 12 ++++-------- app/search/views.py | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/search/templates/document.html b/app/search/templates/document.html index 838a342..242d81c 100644 --- a/app/search/templates/document.html +++ b/app/search/templates/document.html @@ -71,17 +71,13 @@

Document details

- {% if 'nan' not in result.related_legislation %} + {% if result.related_legislation and 'nan' not in result.related_legislation %}

Related content on legislation.gov.uk

-
{% for legislation in result.related_legislation %} -

- {{ legislation }} -

+

+ {{ legislation.title }} +

{% endfor %}
{% endif %} diff --git a/app/search/views.py b/app/search/views.py index 2ab05ba..88da1e0 100644 --- a/app/search/views.py +++ b/app/search/views.py @@ -1,4 +1,5 @@ import csv +import json import logging from django.conf import settings @@ -38,9 +39,21 @@ def document(request: HttpRequest, id) -> HttpResponse: context["result"].regulatory_topics = context[ "result" ].regulatory_topics.split("\n") - context["result"].related_legislation = context[ - "result" - ].related_legislation.split("\n") + + # Parse the related_legislation field + related_legislation_str = context["result"].related_legislation + try: + related_legislation_double_quotes = ( + related_legislation_str.replace("'", '"') + ) + related_legislation_json = json.loads( + related_legislation_double_quotes + ) + context["result"].related_legislation = related_legislation_json + except json.JSONDecodeError as e: + logger.error(f"JSON decoding error: {e}") + context["result"].related_legislation = [] + except Exception as e: logger.error("error fetching details: %s", e) context["error"] = f"error fetching details: {e}"