Skip to content

Commit

Permalink
only filter by data sets that are available for the area type
Browse files Browse the repository at this point in the history
Otherwise you end up with no areas returned if you select a dataset
that's not available for the area type

Fixes #335
  • Loading branch information
struan committed Nov 21, 2023
1 parent 0fd6a91 commit 775b53d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions hub/fixtures/area_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"is_range": "True",
"source": "https://parliament.uk",
"table": "areadata",
"areas_available": [1],
"category": "place"
}
},
Expand All @@ -23,6 +24,7 @@
"last_update": "2022-10-10T13:20:30+00:00",
"source": "https://parliament.uk",
"table": "areadata",
"areas_available": [1],
"category": "place"
}
},
Expand All @@ -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"
}
},
Expand Down
10 changes: 10 additions & 0 deletions hub/fixtures/areas.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions hub/fixtures/elections.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
Expand All @@ -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"
}
},
Expand All @@ -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"
}
},
Expand Down
1 change: 1 addition & 0 deletions hub/fixtures/mp_memberships.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
Expand Down
6 changes: 4 additions & 2 deletions hub/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FilterMixin:
@cache
def filters(self):
filters = []
area_type = self.area_type()
for param, value in self.request.GET.items():
if "__" in param and param not in ["shader", "columns"]:
name, comparator = param.split("__", 1)
Expand All @@ -33,7 +34,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,
Expand All @@ -46,7 +47,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,
Expand Down Expand Up @@ -102,6 +103,7 @@ def columns(self, mp_name=False):

return columns

@cache
def area_type(self):
code = self.request.GET.get("area_type", "WMC")
try:
Expand Down

0 comments on commit 775b53d

Please sign in to comment.