mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-11 09:28:27 -04:00
Merge pull request #3609 from alphagov/send-polygons-to-api
Send simple polygons to API along with areas
This commit is contained in:
@@ -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 we’ve 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 we’ve 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(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
import uuid
|
||||
from collections import namedtuple
|
||||
from functools import partial
|
||||
|
||||
import pytest
|
||||
@@ -507,8 +508,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,
|
||||
@@ -522,7 +529,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
|
||||
},
|
||||
)
|
||||
|
||||
@@ -555,8 +562,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,
|
||||
@@ -569,6 +582,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',
|
||||
@@ -584,8 +598,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,
|
||||
@@ -602,6 +622,7 @@ def test_remove_broadcast_area_page(
|
||||
service_id=SERVICE_ONE_ID,
|
||||
broadcast_message_id=fake_uuid,
|
||||
data={
|
||||
'simple_polygons': coordinates,
|
||||
'areas': ['ctry19-S92000003']
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user