From 9f13d45cc25693e01ce332a7742027e40bd934f7 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Thu, 9 Nov 2023 11:43:31 +0000 Subject: [PATCH] only filter by data sets that are available for the area type Otherwise you end up with no areas returned if you select a dataset that's not available for the area type Fixes #335 --- hub/fixtures/area_data.json | 3 +++ hub/fixtures/areas.json | 10 ++++++++++ hub/fixtures/elections.json | 3 +++ hub/fixtures/mp_memberships.json | 1 + hub/fixtures/mps.json | 3 ++- hub/mixins.py | 6 ++++-- 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/hub/fixtures/area_data.json b/hub/fixtures/area_data.json index 11f920aa3..3dec5769f 100644 --- a/hub/fixtures/area_data.json +++ b/hub/fixtures/area_data.json @@ -10,6 +10,7 @@ "is_range": "True", "source": "https://parliament.uk", "table": "areadata", + "areas_available": [1], "category": "place" } }, @@ -23,6 +24,7 @@ "last_update": "2022-10-10T13:20:30+00:00", "source": "https://parliament.uk", "table": "areadata", + "areas_available": [1], "category": "place" } }, @@ -36,6 +38,7 @@ "last_update": "2022-10-10T13:20:30+00:00", "source": "http://example.org/polling", "table": "areadata", + "areas_available": [1], "category": "opinion" } }, diff --git a/hub/fixtures/areas.json b/hub/fixtures/areas.json index 0ecb0603f..261dd53d2 100644 --- a/hub/fixtures/areas.json +++ b/hub/fixtures/areas.json @@ -9,6 +9,16 @@ "description": "Constituency" } }, + { + "model": "hub.areatype", + "pk": 2, + "fields": { + "name": "Constituency 2023", + "code": "WMC23", + "area_type": "Constituency 2023", + "description": "Constituency 2023" + } + }, { "model": "hub.area", "pk": 1, diff --git a/hub/fixtures/elections.json b/hub/fixtures/elections.json index 6170c8f15..6d604ca01 100644 --- a/hub/fixtures/elections.json +++ b/hub/fixtures/elections.json @@ -7,6 +7,7 @@ "data_type": "integer", "last_update": "2022-10-10T13:20:30+00:00", "source": "https://wikipedia.org", + "areas_available": [1], "table": "person__persondata" } }, @@ -18,6 +19,7 @@ "data_type": "date", "last_update": "2022-10-10T13:20:30+00:00", "source": "https://wikipedia.org", + "areas_available": [1], "table": "person__persondata" } }, @@ -29,6 +31,7 @@ "data_type": "date", "last_update": "2022-10-10T13:20:30+00:00", "source": "https://wikipedia.org", + "areas_available": [1], "table": "person__persondata" } }, diff --git a/hub/fixtures/mp_memberships.json b/hub/fixtures/mp_memberships.json index f9b22e807..6065910b5 100644 --- a/hub/fixtures/mp_memberships.json +++ b/hub/fixtures/mp_memberships.json @@ -7,6 +7,7 @@ "name": "mp_appg_membership", "label": "APPG membership", "last_update": "2022-10-10T13:20:30+00:00", + "areas_available": [1], "source": "Parliament" } }, diff --git a/hub/fixtures/mps.json b/hub/fixtures/mps.json index d709eed7b..6c42bcc66 100644 --- a/hub/fixtures/mps.json +++ b/hub/fixtures/mps.json @@ -51,7 +51,8 @@ "options": [ { "title": "blue party", "shader": "#0000ff" }, { "title": "red party", "shader": "#ff0000" } - ] + ], + "areas_available": [1] } }, { diff --git a/hub/mixins.py b/hub/mixins.py index 6028dc53d..ff4c4ad9b 100644 --- a/hub/mixins.py +++ b/hub/mixins.py @@ -23,6 +23,7 @@ class FilterMixin: @cache def filters(self): filters = [] + area_type = self.area_type() for param, value in self.request.GET.items(): # we don't filter on blank values so skip them if value == "": @@ -37,7 +38,7 @@ def filters(self): value = value.split(",") try: - dataset = DataSet.objects.get(name=name) + dataset = DataSet.objects.get(name=name, areas_available=area_type) filters.append( { "dataset": dataset, @@ -50,7 +51,7 @@ def filters(self): ) except DataSet.DoesNotExist: # pragma: nocover try: - datatype = DataType.objects.get(name=name) + datatype = DataType.objects.get(name=name, area_type=area_type) filters.append( { "dataset": datatype.data_set, @@ -108,6 +109,7 @@ def columns(self, mp_name=False): return columns + @cache def area_type(self): code = self.request.GET.get("area_type", "WMC") try: