Switch to using temporary "areas_2" API field

Depends on: https://github.com/alphagov/notifications-api/pull/3312

This is part of a multi-stage migration where we want to repurpose
the "areas" field in the existing API to something like "areas_2".
This commit is contained in:
Ben Thorner
2021-08-25 17:17:05 +01:00
parent de9d1f991b
commit 11cbee5843
3 changed files with 49 additions and 30 deletions

View File

@@ -88,19 +88,21 @@ class BroadcastMessage(JSONModel):
@property
def areas(self):
library_areas = self.get_areas(self._dict['areas'])
area_ids = self._dict['areas_2']['ids']
polygons = self._dict['areas_2']['simple_polygons']
library_areas = self.get_areas(area_ids)
if library_areas:
if len(library_areas) != len(self._dict['areas']):
if len(library_areas) != len(area_ids):
raise RuntimeError(
f'BroadcastMessage has {len(self._dict["areas"])} areas '
f'BroadcastMessage has {len(area_ids)} areas '
f'but {len(library_areas)} found in the library'
)
return library_areas
return CustomBroadcastAreas(
area_ids=self._dict['areas'],
polygons=self._dict['simple_polygons'],
area_ids=area_ids,
polygons=polygons,
)
@property
@@ -218,15 +220,23 @@ class BroadcastMessage(JSONModel):
def add_areas(self, *new_area_ids):
area_ids = list(OrderedSet(
self._dict['areas'] + list(new_area_ids)
self._dict['areas_2']['ids'] + list(new_area_ids)
))
simple_polygons = self.get_simple_polygons(areas=self.get_areas(area_ids))
self._update(areas=area_ids, simple_polygons=simple_polygons.as_coordinate_pairs_lat_long)
self._update(areas_2={
'ids': area_ids,
'simple_polygons': simple_polygons.as_coordinate_pairs_lat_long
})
def remove_area(self, area_id):
area_ids = list(set(self._dict['areas']) - {area_id})
area_ids = list(set(self._dict['areas_2']['ids']) - {area_id})
simple_polygons = self.get_simple_polygons(areas=self.get_areas(area_ids))
self._update(areas=area_ids, simple_polygons=simple_polygons.as_coordinate_pairs_lat_long)
self._update(areas_2={
'ids': area_ids,
'simple_polygons': simple_polygons.as_coordinate_pairs_lat_long
})
def _set_status_to(self, status):
broadcast_message_api_client.update_broadcast_message_status(

View File

@@ -696,10 +696,10 @@ def broadcast_message_json(
'reference': reference,
'personalisation': {},
'areas': area_ids or [
'ctry19-E92000001', 'ctry19-S92000003',
],
'simple_polygons': simple_polygons or [],
'areas_2': {
'ids': area_ids or ['ctry19-E92000001', 'ctry19-S92000003'],
'simple_polygons': simple_polygons or [],
},
'status': status,

View File

@@ -1395,7 +1395,10 @@ def test_add_broadcast_area(
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
data={
'areas': ['ctry19-E92000001', 'ctry19-S92000003', 'ctry19-W92000004'], 'simple_polygons': coordinates
'areas_2': {
'ids': ['ctry19-E92000001', 'ctry19-S92000003', 'ctry19-W92000004'],
'simple_polygons': coordinates
}
},
)
@@ -1450,12 +1453,14 @@ def test_add_broadcast_sub_area_district_view(
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
data={
'simple_polygons': coordinates,
'areas': [
# These two areas are on the broadcast already
'ctry19-E92000001',
'ctry19-S92000003',
] + expected_selected
'areas_2': {
'simple_polygons': coordinates,
'ids': [
# These two areas are on the broadcast already
'ctry19-E92000001',
'ctry19-S92000003',
] + expected_selected
}
},
)
@@ -1488,14 +1493,16 @@ def test_add_broadcast_sub_area_county_view(
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
data={
'simple_polygons': coordinates,
'areas': [
# These two areas are on the broadcast already
'ctry19-E92000001',
'ctry19-S92000003',
] + [
'ctyua19-E10000016'
]
'areas_2': {
'simple_polygons': coordinates,
'ids': [
# These two areas are on the broadcast already
'ctry19-E92000001',
'ctry19-S92000003',
] + [
'ctyua19-E10000016'
]
}
},
)
@@ -1532,8 +1539,10 @@ def test_remove_broadcast_area_page(
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
data={
'simple_polygons': coordinates,
'areas': ['ctry19-S92000003']
'areas_2': {
'simple_polygons': coordinates,
'ids': ['ctry19-S92000003']
},
},
)