mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-09 13:29:48 -04:00
Merge pull request #4002 from alphagov/refactor-area-methods-178986763
Initial refactors to support area aggregation
This commit is contained in:
@@ -124,23 +124,21 @@ class BroadcastArea(BaseBroadcastArea, SortableMixin):
|
|||||||
return self._count_of_phones or 0
|
return self._count_of_phones or 0
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def parents(self):
|
def ancestors(self):
|
||||||
return list(filter(None, self._parents_iterator))
|
return list(self._ancestors_iterator)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _parents_iterator(self):
|
def _ancestors_iterator(self):
|
||||||
id = self.id
|
id = self.id
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
parent = BroadcastAreasRepository().get_parent_for_area(id)
|
parent = BroadcastAreasRepository().get_parent_for_area(id)
|
||||||
|
|
||||||
if not parent:
|
if not parent:
|
||||||
return None
|
return
|
||||||
|
|
||||||
parent_broadcast_area = BroadcastArea(parent)
|
parent_broadcast_area = BroadcastArea(parent)
|
||||||
|
|
||||||
yield parent_broadcast_area
|
yield parent_broadcast_area
|
||||||
|
|
||||||
id = parent_broadcast_area.id
|
id = parent_broadcast_area.id
|
||||||
|
|
||||||
|
|
||||||
@@ -165,10 +163,11 @@ class CustomBroadcastArea(BaseBroadcastArea):
|
|||||||
simple_polygons = polygons
|
simple_polygons = polygons
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def overlapping_areas(self):
|
def nearby_electoral_wards(self):
|
||||||
if not self.polygons:
|
if not self.polygons:
|
||||||
return []
|
return []
|
||||||
return broadcast_area_libraries.get_areas_with_simple_polygons([
|
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(
|
overlap.data for overlap in rtree_index.query(
|
||||||
Rect(*self.polygons.bounds)
|
Rect(*self.polygons.bounds)
|
||||||
)
|
)
|
||||||
@@ -178,7 +177,7 @@ class CustomBroadcastArea(BaseBroadcastArea):
|
|||||||
def count_of_phones(self):
|
def count_of_phones(self):
|
||||||
return sum(
|
return sum(
|
||||||
area.simple_polygons.ratio_of_intersection_with(self.polygons) * area.count_of_phones
|
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
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -104,14 +104,14 @@ class BroadcastMessage(JSONModel):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parent_areas(self):
|
def ancestor_areas(self):
|
||||||
return sorted(set(self._parent_areas_iterator))
|
return sorted(set(self._ancestor_areas_iterator))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _parent_areas_iterator(self):
|
def _ancestor_areas_iterator(self):
|
||||||
for area in self.areas:
|
for area in self.areas:
|
||||||
for parent in area.parents:
|
for ancestor in area.ancestors:
|
||||||
yield parent
|
yield ancestor
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def polygons(self):
|
def polygons(self):
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
{{ page_header("Choose where to send this alert") }}
|
{{ 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>
|
<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 %}
|
{% if loop.last %}
|
||||||
<div class="keyline-block"></div>
|
<div class="keyline-block"></div>
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ fido2==0.9.1
|
|||||||
awscli-cwlogs>=1.4,<1.5
|
awscli-cwlogs>=1.4,<1.5
|
||||||
itsdangerous==1.1.0 # pyup: <2
|
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
|
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
|
# cryptography 3.4+ incorporates Rust code, which isn't supported on PaaS
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ mistune==0.8.4
|
|||||||
# via notifications-utils
|
# via notifications-utils
|
||||||
notifications-python-client==6.2.1
|
notifications-python-client==6.2.1
|
||||||
# via -r requirements.in
|
# 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
|
# via -r requirements.in
|
||||||
openpyxl==3.0.7
|
openpyxl==3.0.7
|
||||||
# via pyexcel-xlsx
|
# via pyexcel-xlsx
|
||||||
|
|||||||
@@ -386,7 +386,7 @@ def test_count_of_phones_for_custom_area(
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert sorted(
|
assert sorted(
|
||||||
overlap.name for overlap in area.overlapping_areas
|
overlap.name for overlap in area.nearby_electoral_wards
|
||||||
) == expected_possible_overlaps
|
) == expected_possible_overlaps
|
||||||
|
|
||||||
assert close_enough(area.count_of_phones, expected_count_of_phones)
|
assert close_enough(area.count_of_phones, expected_count_of_phones)
|
||||||
|
|||||||
Reference in New Issue
Block a user