From d629575cd7cbe1aac07a6556548a6e0d3db71c0a Mon Sep 17 00:00:00 2001 From: Zarino Zappia Date: Wed, 27 Mar 2024 13:54:09 +0000 Subject: [PATCH] Add a fake dataset for the constituency URL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Handy when you’re downloading constituency data as a CSV, and want to be able to quickly link back to the Area’s webpage. --- hub/mixins.py | 10 ++++++++-- hub/tests/test_views.py | 4 ++-- hub/views/explore.py | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/hub/mixins.py b/hub/mixins.py index 39aa1264a..f60ad0d6c 100644 --- a/hub/mixins.py +++ b/hub/mixins.py @@ -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 @@ -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": diff --git a/hub/tests/test_views.py b/hub/tests/test_views.py index 96d93c9a0..fedfa4018 100644 --- a/hub/tests/test_views.py +++ b/hub/tests/test_views.py @@ -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" diff --git a/hub/views/explore.py b/hub/views/explore.py index 8d8ff2fb9..4494c2eaa 100644 --- a/hub/views/explore.py +++ b/hub/views/explore.py @@ -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)