diff --git a/app/broadcast_areas/models.py b/app/broadcast_areas/models.py index 8d364e3c5..ddd163a53 100644 --- a/app/broadcast_areas/models.py +++ b/app/broadcast_areas/models.py @@ -203,8 +203,8 @@ class CustomBroadcastArea(BaseBroadcastArea): class CustomBroadcastAreas(SerialisedModelCollection): model = CustomBroadcastArea - def __init__(self, *, area_ids, polygons): - self.items = area_ids + def __init__(self, *, names, polygons): + self.items = names self._polygons = polygons def __getitem__(self, index): diff --git a/app/models/broadcast_message.py b/app/models/broadcast_message.py index 5e1a5deb4..247edc478 100644 --- a/app/models/broadcast_message.py +++ b/app/models/broadcast_message.py @@ -89,10 +89,9 @@ class BroadcastMessage(JSONModel): @property def areas(self): - polygons = self._dict['areas']['simple_polygons'] - library_areas = self.get_areas(self.area_ids) + if 'ids' in self._dict['areas']: + library_areas = self.get_areas(self.area_ids) - if library_areas: if len(library_areas) != len(self.area_ids): raise RuntimeError( f'BroadcastMessage has {len(self.area_ids)} areas ' @@ -101,8 +100,8 @@ class BroadcastMessage(JSONModel): return library_areas return CustomBroadcastAreas( - area_ids=self.area_ids, - polygons=polygons, + names=self._dict['areas']['names'], + polygons=self._dict['areas']['simple_polygons'], ) @property diff --git a/tests/__init__.py b/tests/__init__.py index 95bd17233..8c9d5ad12 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -678,6 +678,7 @@ def broadcast_message_json( updated_at=None, approved_by_id=None, cancelled_by_id=None, + areas=None, area_ids=None, simple_polygons=None, content=None, @@ -696,7 +697,7 @@ def broadcast_message_json( 'reference': reference, 'personalisation': {}, - 'areas': { + 'areas': areas or { 'ids': area_ids or ['ctry19-E92000001', 'ctry19-S92000003'], 'simple_polygons': simple_polygons or [], }, diff --git a/tests/app/broadcast_areas/test_utils.py b/tests/app/broadcast_areas/test_utils.py index 8ed121995..c372aa2e4 100644 --- a/tests/app/broadcast_areas/test_utils.py +++ b/tests/app/broadcast_areas/test_utils.py @@ -175,8 +175,10 @@ def test_aggregate_areas_for_custom_polygons( ): broadcast_message = BroadcastMessage( broadcast_message_json( - area_ids=['derived from polygons'], - simple_polygons=simple_polygons + areas={ + 'names': [f'polygon {i}' for i, _ in enumerate(simple_polygons)], + 'simple_polygons': simple_polygons + } ) ) diff --git a/tests/app/main/views/test_broadcast.py b/tests/app/main/views/test_broadcast.py index e50b0a858..3c13b281c 100644 --- a/tests/app/main/views/test_broadcast.py +++ b/tests/app/main/views/test_broadcast.py @@ -974,8 +974,10 @@ def test_preview_broadcast_areas_page_with_custom_polygons( created_by_id=fake_uuid, service_id=SERVICE_ONE_ID, status='draft', - area_ids=['Area one', 'Area two', 'Area three'], - simple_polygons=polygons, + areas={ + 'names': ['Area one', 'Area two', 'Area three'], + 'simple_polygons': polygons, + } ), ) client_request.login(active_user_create_broadcasts_permission) diff --git a/tests/app/models/test_broadcast_message.py b/tests/app/models/test_broadcast_message.py index 9b2dd3862..423bf2db2 100644 --- a/tests/app/models/test_broadcast_message.py +++ b/tests/app/models/test_broadcast_message.py @@ -38,6 +38,22 @@ def test_content_comes_from_attribute_not_template(): assert broadcast_message.content == 'This is a test' +@pytest.mark.parametrize(('areas', 'expected_length'), [ + ({'ids': []}, 0), + ({'ids': ['wd20-E05009372']}, 1), + ({'names': ['somewhere'], 'simple_polygons': [[[3.5, 1.5]]]}, 1) +]) +def test_areas( + areas, + expected_length +): + broadcast_message = BroadcastMessage(broadcast_message_json( + areas=areas + )) + + assert len(list(broadcast_message.areas)) == expected_length + + def test_areas_raises_for_missing_areas(): broadcast_message = BroadcastMessage(broadcast_message_json( area_ids=[