Send simple polygons to API along with areas

When creating broadcast message, or updating it.
We want to send simple polygons to API so we can
later relay them to the broadcast provider.

Test that update broadcast message sends polygons correctly
This commit is contained in:
Pea Tyczynska
2020-09-02 14:54:00 +01:00
parent 3bebb1fbfd
commit 3bfe3a2bf7
2 changed files with 47 additions and 16 deletions

View File

@@ -62,9 +62,7 @@ class BroadcastMessage(JSONModel):
@property
def areas(self):
return broadcast_area_libraries.get_areas(
*self._dict['areas']
)
return self.get_areas(areas=self._dict['areas'])
@property
def initial_area_names(self):
@@ -82,14 +80,7 @@ class BroadcastMessage(JSONModel):
@cached_property
def simple_polygons(self):
polygons = Polygons(
list(itertools.chain(*(
area.simple_polygons for area in self.areas
)))
)
# If weve added multiple areas then we need to re-simplify the
# combined shapes to keep the point count down
return polygons.smooth.simplify if len(self.areas) > 1 else polygons
return self.get_simple_polygons(areas=self.areas)
@property
def template(self):
@@ -122,16 +113,35 @@ class BroadcastMessage(JSONModel):
def cancelled_by(self):
return User.from_id(self.cancelled_by_id)
def get_areas(self, areas):
return broadcast_area_libraries.get_areas(
*areas
)
def get_simple_polygons(self, areas):
polygons = Polygons(
list(itertools.chain(*(
area.simple_polygons for area in areas
)))
)
# If weve added multiple areas then we need to re-simplify the
# combined shapes to keep the point count down
return polygons.smooth.simplify if len(areas) > 1 else polygons
def add_areas(self, *new_areas):
self._update(areas=list(OrderedSet(
areas = list(OrderedSet(
self._dict['areas'] + list(new_areas)
)))
))
simple_polygons = self.get_simple_polygons(areas=self.get_areas(areas=areas))
self._update(areas=areas, simple_polygons=simple_polygons.as_coordinate_pairs_lat_long)
def remove_area(self, area_to_remove):
self._update(areas=[
areas = [
area for area in self._dict['areas']
if area != area_to_remove
])
]
simple_polygons = self.get_simple_polygons(areas=self.get_areas(areas=areas))
self._update(areas=areas, 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

@@ -1,5 +1,6 @@
import json
import uuid
from collections import namedtuple
from functools import partial
import pytest
@@ -495,8 +496,14 @@ def test_add_broadcast_area(
mock_get_draft_broadcast_message,
mock_update_broadcast_message,
fake_uuid,
mocker
):
service_one['permissions'] += ['broadcast']
polygon_class = namedtuple("polygon_class", ["as_coordinate_pairs_lat_long"])
coordinates = [[50.1, 0.1], [50.2, 0.2], [50.3, 0.2]]
polygons = polygon_class(as_coordinate_pairs_lat_long=coordinates)
mocker.patch('app.models.broadcast_message.BroadcastMessage.get_simple_polygons', return_value=polygons)
client_request.post(
'.choose_broadcast_area',
service_id=SERVICE_ONE_ID,
@@ -510,7 +517,7 @@ def test_add_broadcast_area(
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
data={
'areas': ['ctry19-E92000001', 'ctry19-S92000003', 'ctry19-W92000004']
'areas': ['ctry19-E92000001', 'ctry19-S92000003', 'ctry19-W92000004'], 'simple_polygons': coordinates
},
)
@@ -543,8 +550,14 @@ def test_add_broadcast_sub_area(
fake_uuid,
post_data,
expected_selected,
mocker
):
service_one['permissions'] += ['broadcast']
polygon_class = namedtuple("polygon_class", ["as_coordinate_pairs_lat_long"])
coordinates = [[50.1, 0.1], [50.2, 0.2], [50.3, 0.2]]
polygons = polygon_class(as_coordinate_pairs_lat_long=coordinates)
mocker.patch('app.models.broadcast_message.BroadcastMessage.get_simple_polygons', return_value=polygons)
client_request.post(
'.choose_broadcast_sub_area',
service_id=SERVICE_ONE_ID,
@@ -557,6 +570,7 @@ def test_add_broadcast_sub_area(
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',
@@ -572,8 +586,14 @@ def test_remove_broadcast_area_page(
mock_get_draft_broadcast_message,
mock_update_broadcast_message,
fake_uuid,
mocker,
):
service_one['permissions'] += ['broadcast']
polygon_class = namedtuple("polygon_class", ["as_coordinate_pairs_lat_long"])
coordinates = [[50.1, 0.1], [50.2, 0.2], [50.3, 0.2]]
polygons = polygon_class(as_coordinate_pairs_lat_long=coordinates)
mocker.patch('app.models.broadcast_message.BroadcastMessage.get_simple_polygons', return_value=polygons)
client_request.get(
'.remove_broadcast_area',
service_id=SERVICE_ONE_ID,
@@ -590,6 +610,7 @@ def test_remove_broadcast_area_page(
service_id=SERVICE_ONE_ID,
broadcast_message_id=fake_uuid,
data={
'simple_polygons': coordinates,
'areas': ['ctry19-S92000003']
},
)