Skip to content

Commit

Permalink
Adding json response for 410 when triggered with extention .json
Browse files Browse the repository at this point in the history
  • Loading branch information
ssadhu-sl committed Apr 24, 2024
1 parent e631662 commit 59f7211
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 50 deletions.
22 changes: 14 additions & 8 deletions application/routers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,19 @@ def get_entity(
e, old_entity_status, new_entity_id = get_entity_query(session, entity)

if old_entity_status == 410:
return templates.TemplateResponse(
"entity-gone.html",
{
"request": request,
"entity": str(entity),
},
)
if extension:
raise HTTPException(
detail=f"Entity {entity} has been removed", status_code=410
)
else:
return templates.TemplateResponse(
"entity-gone.html",
{
"request": request,
"entity": str(entity),
},
status_code=410,
)
elif old_entity_status == 301:
if extension:
return RedirectResponse(
Expand Down Expand Up @@ -278,7 +284,7 @@ def search_entities(
columns = ["dataset", "name", "plural", "typology", "themes", "paint_options"]
datasets = [dataset.dict(include=set(columns)) for dataset in response]

local_authorities = get_local_authorities(session, "local-authority")
local_authorities = get_local_authorities(session, "local-authority-eng")
local_authorities = [la.dict() for la in local_authorities]

if links.get("prev") is not None:
Expand Down
39 changes: 14 additions & 25 deletions application/templates/entity-gone.html
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
{% extends "layouts/layout.html" %}
{% set mainClasses = "govuk-main-wrapper--l" %}
{% set templateName = "dl-info/entity-gone.html" %}

{%- block content %}

{% block recordHead %}
{% block recordTitle -%}
<span class="govuk-caption-xl">Entity Removed</span>
<h1 class="govuk-heading-xl">Entity Removed</h1>
{%- endblock recordTitle %}
{% endblock recordHead %}
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l">Entity Removed</h1>

<p class="govuk-body">
This entity (#{{entity}}) has been removed.
</p>

{%- endblock content %}

{% block recordEnd %}{% endblock recordEnd %}

{% block footerStart %}
{%- from "components/feedback/macro.jinja" import dlFeedback %}
{% set subject = "Feedback on removed entity with number " + entity %}
{{ dlFeedback({
"text": "Spotted an issue? Let us know so we can improve the data.",
"action": {
"text": "There is something wrong with the data",
"href": "mailto:" + templateVar.email + "?subject=" + subject
},
"container": true
})
}}
{% endblock footerStart %}

{%- block pageScripts %}{%- endblock %}
{%- from "components/feedback/macro.jinja" import dlFeedback %}
{% set subject = "Feedback on removed entity with number " + entity %}
<div class="govuk-body">
Spotted an issue? You can
<a href="mailto:{{ templateVar.email }}?subject={{ subject }}" class="govuk-link"> contact the Planning Data team </a>
if you need to speak to someone about this page.
</div>
</div>
</div>
{%- endblock %}
2 changes: 1 addition & 1 deletion tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_old_entity_gone_shown(test_data_old_entities, client, exclude_middlewar
"""
old_entity = test_data_old_entities["old_entities"][410][0]
response = client.get(f"/entity/{old_entity.old_entity_id}", allow_redirects=False)
assert response.status_code == 200
assert response.status_code == 410
assert (
f"This entity (#{old_entity.old_entity_id}) has been removed." in response.text
)
Expand Down
22 changes: 6 additions & 16 deletions tests/unit/routers/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,11 @@ def test_get_entity_old_entity_gone_returned_json(mocker):
request = MagicMock()
extension = MagicMock()
extension.value = "json"
result = get_entity(request=request, entity="11000000", extension=extension)
try:
result.template.render(result.context)
get_entity(request=request, entity="11000000", extension=extension)
assert False, "Expected HTTPException to be raised"
except HTTPException:
assert True
except Exception:
if hasattr(result, "context"):
logging.warning(f"context:{result.context}")
else:
logging.warning("result has no context")
assert False, "template unable to render, missing variable(s) from context"


def test_get_entity_old_entity_gone_returned_geojson(mocker):
Expand All @@ -231,16 +226,11 @@ def test_get_entity_old_entity_gone_returned_geojson(mocker):
request = MagicMock()
extension = MagicMock()
extension.value = "geojson"
result = get_entity(request=request, entity="11000000", extension=extension)
try:
result.template.render(result.context)
get_entity(request=request, entity="11000000", extension=extension)
assert False, "Expected HTTPException to be raised"
except HTTPException:
assert True
except Exception:
if hasattr(result, "context"):
logging.warning(f"context:{result.context}")
else:
logging.warning("result has no context")
assert False, "template unable to render, missing variable(s) from context"


def test_get_entity_old_entity_redirect_returned_html(mocker):
Expand Down

0 comments on commit 59f7211

Please sign in to comment.