Merge pull request #4002 from alphagov/refactor-area-methods-178986763

Initial refactors to support area aggregation
This commit is contained in:
Ben Thorner
2021-08-24 14:25:54 +01:00
committed by GitHub
6 changed files with 16 additions and 17 deletions

View File

@@ -124,23 +124,21 @@ class BroadcastArea(BaseBroadcastArea, SortableMixin):
return self._count_of_phones or 0
@cached_property
def parents(self):
return list(filter(None, self._parents_iterator))
def ancestors(self):
return list(self._ancestors_iterator)
@property
def _parents_iterator(self):
def _ancestors_iterator(self):
id = self.id
while True:
parent = BroadcastAreasRepository().get_parent_for_area(id)
if not parent:
return None
return
parent_broadcast_area = BroadcastArea(parent)
yield parent_broadcast_area
id = parent_broadcast_area.id
@@ -165,10 +163,11 @@ class CustomBroadcastArea(BaseBroadcastArea):
simple_polygons = polygons
@cached_property
def overlapping_areas(self):
def nearby_electoral_wards(self):
if not self.polygons:
return []
return broadcast_area_libraries.get_areas_with_simple_polygons([
# We only index electoral wards in the RTree
overlap.data for overlap in rtree_index.query(
Rect(*self.polygons.bounds)
)
@@ -178,7 +177,7 @@ class CustomBroadcastArea(BaseBroadcastArea):
def count_of_phones(self):
return sum(
area.simple_polygons.ratio_of_intersection_with(self.polygons) * area.count_of_phones
for area in self.overlapping_areas
for area in self.nearby_electoral_wards
)

View File

@@ -104,14 +104,14 @@ class BroadcastMessage(JSONModel):
)
@property
def parent_areas(self):
return sorted(set(self._parent_areas_iterator))
def ancestor_areas(self):
return sorted(set(self._ancestor_areas_iterator))
@property
def _parent_areas_iterator(self):
def _ancestor_areas_iterator(self):
for area in self.areas:
for parent in area.parents:
yield parent
for ancestor in area.ancestors:
yield ancestor
@cached_property
def polygons(self):

View File

@@ -14,7 +14,7 @@
{{ page_header("Choose where to send this alert") }}
{% for area in broadcast_message.parent_areas %}
{% for area in broadcast_message.ancestor_areas %}
<a class="govuk-heading-m govuk-link govuk-link--no-visited-state" href="{{ url_for('.choose_broadcast_sub_area', service_id=current_service.id, broadcast_message_id=broadcast_message.id, library_slug=area.library_id, area_slug=area.id) }}">{{ area.name }}</a>
{% if loop.last %}
<div class="keyline-block"></div>

View File

@@ -26,7 +26,7 @@ fido2==0.9.1
awscli-cwlogs>=1.4,<1.5
itsdangerous==1.1.0 # pyup: <2
git+https://github.com/alphagov/notifications-utils.git@44.4.3#egg=notifications-utils==44.4.3
git+https://github.com/alphagov/notifications-utils.git@44.5.0#egg=notifications-utils==44.5.0
git+https://github.com/alphagov/govuk-frontend-jinja.git@v0.5.8-alpha#egg=govuk-frontend-jinja==0.5.8-alpha
# cryptography 3.4+ incorporates Rust code, which isn't supported on PaaS

View File

@@ -118,7 +118,7 @@ mistune==0.8.4
# via notifications-utils
notifications-python-client==6.2.1
# via -r requirements.in
git+https://github.com/alphagov/notifications-utils.git@44.4.3#egg=notifications-utils==44.4.3
git+https://github.com/alphagov/notifications-utils.git@44.5.0#egg=notifications-utils==44.5.0
# via -r requirements.in
openpyxl==3.0.7
# via pyexcel-xlsx

View File

@@ -386,7 +386,7 @@ def test_count_of_phones_for_custom_area(
)
assert sorted(
overlap.name for overlap in area.overlapping_areas
overlap.name for overlap in area.nearby_electoral_wards
) == expected_possible_overlaps
assert close_enough(area.count_of_phones, expected_count_of_phones)