Skip to content

Commit 4848865

Browse files
committed
[Scorecards] only suggest similar councils of same type on council page
Sometimes the list of similar councils was including other types of councils and when you clicked on those you got an error. This adds something to get_related_councils which filters by council group to remove these.
1 parent 44b0bda commit 4848865

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

caps/models.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,9 @@ def get_emissions_cluster(self):
377377
assignments__council=self, type__slug="emissions"
378378
)
379379

380-
def get_related_councils(self, cut_off: int = 10) -> List[dict]:
380+
def get_related_councils(
381+
self, cut_off: int = 10, scoring_group: str = None
382+
) -> List[dict]:
381383
"""
382384
get all related councils
383385
"""
@@ -397,6 +399,13 @@ def get_related_councils(self, cut_off: int = 10) -> List[dict]:
397399
declared_emergency=Min("emergencydeclaration__date_declared"),
398400
)
399401

402+
if scoring_group is not None:
403+
group = self.SCORING_GROUPS[scoring_group]
404+
councils = councils.filter(
405+
authority_type__in=group["types"],
406+
country__in=group["countries"],
407+
)
408+
400409
council_lookup = {council.id: council for council in councils}
401410
council_ids = list(council_lookup.keys()) + [self.id]
402411

@@ -413,7 +422,14 @@ def get_related_councils(self, cut_off: int = 10) -> List[dict]:
413422
labels[comparison.label.type_id][comparison.council_id] = comparison
414423

415424
processed_councils = []
416-
for d in self.distances.all().order_by("type_id", "-match_score"):
425+
distances = self.distances.all().order_by("type_id", "-match_score")
426+
if scoring_group is not None:
427+
group = self.SCORING_GROUPS[scoring_group]
428+
distances = distances.filter(
429+
council_b__authority_type__in=group["types"],
430+
council_b__country__in=group["countries"],
431+
)
432+
for d in distances:
417433
# deepcopy to make sure the same council in multiple comparison types
418434
# are seperate objects with seperate links to Distance objects
419435
nc = deepcopy(council_lookup[d.council_b_id])

scoring/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def get_context_data(self, **kwargs):
407407
)
408408
context["comparisons"] = comparisons
409409

410-
for group in council.get_related_councils(5):
410+
for group in council.get_related_councils(5, group["slug"]):
411411
if group["type"].slug == "composite":
412412
if comparisons:
413413
# Filter out any related councils that are already being compared

0 commit comments

Comments
 (0)