From 11cbee5843ee457d0100b2fe416ebf322c35b503 Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Wed, 25 Aug 2021 17:17:05 +0100 Subject: [PATCH] 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". --- app/models/broadcast_message.py | 28 +++++++++++------ tests/__init__.py | 8 ++--- tests/app/main/views/test_broadcast.py | 43 ++++++++++++++++---------- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/app/models/broadcast_message.py b/app/models/broadcast_message.py index 4397e9593..409d338cf 100644 --- a/app/models/broadcast_message.py +++ b/app/models/broadcast_message.py @@ -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( diff --git a/tests/__init__.py b/tests/__init__.py index 5a209e27e..e296d0c1f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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, diff --git a/tests/app/main/views/test_broadcast.py b/tests/app/main/views/test_broadcast.py index 31fc3f913..32f7c7315 100644 --- a/tests/app/main/views/test_broadcast.py +++ b/tests/app/main/views/test_broadcast.py @@ -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'] + }, }, )