From de804805acbf9b5874fa00abd14cac633e85ed2c Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Tue, 24 Aug 2021 16:06:46 +0100 Subject: [PATCH] Refactor logic to identify types of areas I did consider whether to store this explicitly in the SQLite DB, but this is less effort for now and we can always switch to that more robust approach in future if we need to. --- app/broadcast_areas/models.py | 8 ++++++++ app/broadcast_areas/utils.py | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/broadcast_areas/models.py b/app/broadcast_areas/models.py index 271da834f..8d364e3c5 100644 --- a/app/broadcast_areas/models.py +++ b/app/broadcast_areas/models.py @@ -86,6 +86,14 @@ class BroadcastArea(BaseBroadcastArea, SortableMixin): def __init__(self, row): self.id, self.name, self._count_of_phones, self.library_id = row + @cached_property + def is_lower_tier_local_authority(self): + return self.id.startswith('lad20-') and self.parent + + @cached_property + def is_electoral_ward(self): + return self.id.startswith('wd20-') + @classmethod def from_row_with_simple_polygons(cls, row): instance = cls(row[:4]) diff --git a/app/broadcast_areas/utils.py b/app/broadcast_areas/utils.py index fdcc1f83a..217bf028e 100644 --- a/app/broadcast_areas/utils.py +++ b/app/broadcast_areas/utils.py @@ -24,7 +24,7 @@ def _convert_custom_areas_to_wards(areas): def _aggregate_wards_by_local_authority(areas): return { - area.parent if area.id.startswith('wd20-') + area.parent if area.is_electoral_ward else area for area in areas } @@ -56,7 +56,7 @@ def _cluster_lower_tier_authorities(areas): for area in areas: # group lower tier authorities by "county" - if area.id.startswith('lad20-') and area.parent: + if area.is_lower_tier_local_authority: result[area.parent] += [area] # leave countries, unitary authorities as-is else: