Skip to content

Commit

Permalink
Add a fake dataset for the constituency URL
Browse files Browse the repository at this point in the history
Handy when you’re downloading constituency data as a CSV,
and want to be able to quickly link back to the Area’s webpage.
  • Loading branch information
zarino authored and struan committed Apr 1, 2024
1 parent 2c461e1 commit d629575
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
10 changes: 8 additions & 2 deletions hub/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def columns(self, mp_name=False):
if mp_name:
col_names.append("mp_name")

col_label_map = {"mp_name": "MP Name", "gss": "GSS"}
col_label_map = {"mp_name": "MP Name", "gss": "GSS", "url": "URL"}

area_type = self.area_type()

for col in col_names:
if col in ["mp_name", "gss"]:
if col in ["mp_name", "gss", "url"]:
columns.append({"name": col, "label": col_label_map[col]})
continue

Expand Down Expand Up @@ -213,6 +213,12 @@ def data(self, as_dict=False, mp_name=False):
for row in self.query():
area_data[row.name]["GSS"].append(row.gss)
continue
elif col["name"] == "url":
for row in self.query():
area_data[row.name]["URL"].append(
f"{self.request.build_absolute_uri(row.get_absolute_url())}"
)
continue

dataset = col["dataset"]
if dataset.table == "areadata":
Expand Down
4 changes: 2 additions & 2 deletions hub/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def test_explore_datasets_json_page(self):
self.assertEqual(response.status_code, 200)

datasets = response.json()
self.assertEqual(11, len(datasets))
self.assertEqual(12, len(datasets))

self.client.logout()
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
datasets = response.json()
self.assertEqual(4, len(datasets))
self.assertEqual(5, len(datasets))

def test_explore_view_with_many_to_one(self):
url = f"{reverse('explore_csv')}?mp_appg_membership__exact=MadeUpAPPG"
Expand Down
16 changes: 16 additions & 0 deletions hub/views/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ def render_to_response(self, context, **response_kwargs):
}
)

datasets.append(
{
"scope": "public",
"is_featured": False,
"is_favourite": False,
"is_filterable": False,
"is_shadable": False,
"category": "place",
"name": "url",
"title": "Constituency URL",
"description": "A link to this constituency on the Local Intelligence Hub.",
"source_label": "Data from mySociety.",
"areas_available": ["WMC", "WMC23"],
}
)

return JsonResponse(list(datasets), safe=False)


Expand Down

0 comments on commit d629575

Please sign in to comment.