mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-23 15:57:23 -04:00
Merge pull request #3780 from alphagov/show-areas-not-in-library
Display areas that aren’t in the library
This commit is contained in:
@@ -89,6 +89,41 @@ class BroadcastArea(SortableMixin):
|
|||||||
id = parent_broadcast_area.id
|
id = parent_broadcast_area.id
|
||||||
|
|
||||||
|
|
||||||
|
class CustomBroadcastArea:
|
||||||
|
|
||||||
|
# We don’t yet have a way to estimate the number of phones in a
|
||||||
|
# user-defined polygon
|
||||||
|
count_of_phones = 0
|
||||||
|
|
||||||
|
def __init__(self, *, name, polygons=None):
|
||||||
|
self.name = name
|
||||||
|
self._polygons = polygons or []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def polygons(self):
|
||||||
|
return Polygons(
|
||||||
|
# Polygons in the DB are stored with the coordinate pair
|
||||||
|
# order flipped – this flips them back again
|
||||||
|
Polygons(self._polygons).as_coordinate_pairs_lat_long
|
||||||
|
)
|
||||||
|
|
||||||
|
simple_polygons = polygons
|
||||||
|
|
||||||
|
|
||||||
|
class CustomBroadcastAreas(SerialisedModelCollection):
|
||||||
|
model = CustomBroadcastArea
|
||||||
|
|
||||||
|
def __init__(self, *, areas, polygons):
|
||||||
|
self.items = areas
|
||||||
|
self._polygons = polygons
|
||||||
|
|
||||||
|
def __getitem__(self, index):
|
||||||
|
return self.model(
|
||||||
|
name=self.items[index],
|
||||||
|
polygons=self._polygons if index == 0 else None,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class BroadcastAreaLibrary(SerialisedModelCollection, SortableMixin, GetItemByIdMixin):
|
class BroadcastAreaLibrary(SerialisedModelCollection, SortableMixin, GetItemByIdMixin):
|
||||||
|
|
||||||
model = BroadcastArea
|
model = BroadcastArea
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from notifications_utils.template import BroadcastPreviewTemplate
|
|||||||
from orderedset import OrderedSet
|
from orderedset import OrderedSet
|
||||||
from werkzeug.utils import cached_property
|
from werkzeug.utils import cached_property
|
||||||
|
|
||||||
from app.broadcast_areas import broadcast_area_libraries
|
from app.broadcast_areas import CustomBroadcastAreas, broadcast_area_libraries
|
||||||
from app.broadcast_areas.polygons import Polygons
|
from app.broadcast_areas.polygons import Polygons
|
||||||
from app.formatters import round_to_significant_figures
|
from app.formatters import round_to_significant_figures
|
||||||
from app.models import JSONModel, ModelList
|
from app.models import JSONModel, ModelList
|
||||||
@@ -45,6 +45,10 @@ class BroadcastMessage(JSONModel):
|
|||||||
return True
|
return True
|
||||||
if not self.starts_at and other.starts_at:
|
if not self.starts_at and other.starts_at:
|
||||||
return False
|
return False
|
||||||
|
if self.updated_at and not other.updated_at:
|
||||||
|
return self.updated_at < other.created_at
|
||||||
|
if not self.updated_at and other.updated_at:
|
||||||
|
return self.created_at < other.updated_at
|
||||||
return self.updated_at < other.updated_at
|
return self.updated_at < other.updated_at
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -74,7 +78,20 @@ class BroadcastMessage(JSONModel):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def areas(self):
|
def areas(self):
|
||||||
return self.get_areas(areas=self._dict['areas'])
|
library_areas = self.get_areas(areas=self._dict['areas'])
|
||||||
|
|
||||||
|
if library_areas:
|
||||||
|
if len(library_areas) != len(self._dict['areas']):
|
||||||
|
raise RuntimeError(
|
||||||
|
f'BroadcastMessage has {len(self._dict["areas"])} areas '
|
||||||
|
f'but {len(library_areas)} found in the library'
|
||||||
|
)
|
||||||
|
return library_areas
|
||||||
|
|
||||||
|
return CustomBroadcastAreas(
|
||||||
|
areas=self._dict['areas'],
|
||||||
|
polygons=self._dict['simple_polygons'],
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parent_areas(self):
|
def parent_areas(self):
|
||||||
@@ -124,7 +141,7 @@ class BroadcastMessage(JSONModel):
|
|||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def created_by(self):
|
def created_by(self):
|
||||||
return User.from_id(self.created_by_id)
|
return User.from_id(self.created_by_id) if self.created_by_id else None
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def approved_by(self):
|
def approved_by(self):
|
||||||
|
|||||||
@@ -20,7 +20,9 @@
|
|||||||
alert
|
alert
|
||||||
</li>
|
</li>
|
||||||
<li class="area-list-key area-list-key--phone-estimate">
|
<li class="area-list-key area-list-key--phone-estimate">
|
||||||
{% if broadcast_message.count_of_phones == broadcast_message.count_of_phones_likely %}
|
{% if broadcast_message.count_of_phones == 0 %}
|
||||||
|
Unknown number of phones
|
||||||
|
{% elif broadcast_message.count_of_phones == broadcast_message.count_of_phones_likely %}
|
||||||
{{ broadcast_message.count_of_phones|format_thousands }} phones estimated
|
{{ broadcast_message.count_of_phones|format_thousands }} phones estimated
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ broadcast_message.count_of_phones|format_thousands }} to {{ broadcast_message.count_of_phones_likely|format_thousands }} phones
|
{{ broadcast_message.count_of_phones|format_thousands }} to {{ broadcast_message.count_of_phones_likely|format_thousands }} phones
|
||||||
|
|||||||
@@ -19,10 +19,15 @@
|
|||||||
|
|
||||||
{% block service_page_title %}
|
{% block service_page_title %}
|
||||||
{% if broadcast_message.status == 'pending-approval' %}
|
{% if broadcast_message.status == 'pending-approval' %}
|
||||||
{% if broadcast_message.created_by == current_user and current_user.has_permissions('send_messages') %}
|
{% if broadcast_message.created_by and broadcast_message.created_by == current_user and current_user.has_permissions('send_messages') %}
|
||||||
{{ broadcast_message.template.name }} is waiting for approval
|
{{ broadcast_message.template.name }} is waiting for approval
|
||||||
{% elif current_user.has_permissions('send_messages') %}
|
{% elif current_user.has_permissions('send_messages') %}
|
||||||
{{ broadcast_message.created_by.name }} wants to broadcast
|
{% if broadcast_message.created_by %}
|
||||||
|
{{ broadcast_message.created_by.name }}
|
||||||
|
{% else %}
|
||||||
|
An API call
|
||||||
|
{% endif %}
|
||||||
|
wants to broadcast
|
||||||
{{ broadcast_message.template.name }}
|
{{ broadcast_message.template.name }}
|
||||||
{% else %}
|
{% else %}
|
||||||
This alert is waiting for approval
|
This alert is waiting for approval
|
||||||
@@ -37,7 +42,7 @@
|
|||||||
{{ govukBackLink({ "href": back_link }) }}
|
{{ govukBackLink({ "href": back_link }) }}
|
||||||
|
|
||||||
{% if broadcast_message.status == 'pending-approval' %}
|
{% if broadcast_message.status == 'pending-approval' %}
|
||||||
{% if broadcast_message.created_by == current_user and current_user.has_permissions('send_messages') %}
|
{% if broadcast_message.created_by and broadcast_message.created_by == current_user and current_user.has_permissions('send_messages') %}
|
||||||
<div class="banner govuk-!-margin-bottom-6">
|
<div class="banner govuk-!-margin-bottom-6">
|
||||||
<h1 class="govuk-heading-m govuk-!-margin-bottom-3">
|
<h1 class="govuk-heading-m govuk-!-margin-bottom-3">
|
||||||
{{ broadcast_message.template.name }} is waiting for approval
|
{{ broadcast_message.template.name }} is waiting for approval
|
||||||
@@ -79,7 +84,12 @@
|
|||||||
{% elif current_user.has_permissions('send_messages') %}
|
{% elif current_user.has_permissions('send_messages') %}
|
||||||
{% call form_wrapper(class="banner govuk-!-margin-bottom-6") %}
|
{% call form_wrapper(class="banner govuk-!-margin-bottom-6") %}
|
||||||
<h1 class="govuk-heading-m govuk-!-margin-top-0 govuk-!-margin-bottom-3">
|
<h1 class="govuk-heading-m govuk-!-margin-top-0 govuk-!-margin-bottom-3">
|
||||||
{{ broadcast_message.created_by.name }} wants to broadcast
|
{% if broadcast_message.created_by %}
|
||||||
|
{{ broadcast_message.created_by.name }}
|
||||||
|
{% else %}
|
||||||
|
An API call
|
||||||
|
{% endif %}
|
||||||
|
wants to broadcast
|
||||||
{{ broadcast_message.template.name }}
|
{{ broadcast_message.template.name }}
|
||||||
</h1>
|
</h1>
|
||||||
{{ page_footer(
|
{{ page_footer(
|
||||||
@@ -138,8 +148,12 @@
|
|||||||
|
|
||||||
{% if broadcast_message.status != 'pending-approval' %}
|
{% if broadcast_message.status != 'pending-approval' %}
|
||||||
<p class="govuk-body govuk-!-margin-bottom-3">
|
<p class="govuk-body govuk-!-margin-bottom-3">
|
||||||
Prepared by {{ broadcast_message.created_by.name }} and approved by
|
{% if broadcast_message.created_by %}
|
||||||
{{ broadcast_message.approved_by.name }}.
|
Prepared by {{ broadcast_message.created_by.name }}
|
||||||
|
{% else %}
|
||||||
|
Created from an API call
|
||||||
|
{% endif %}
|
||||||
|
and approved by {{ broadcast_message.approved_by.name }}.
|
||||||
</p>
|
</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|||||||
@@ -657,6 +657,7 @@ def broadcast_message_json(
|
|||||||
approved_by_id=None,
|
approved_by_id=None,
|
||||||
cancelled_by_id=None,
|
cancelled_by_id=None,
|
||||||
areas=None,
|
areas=None,
|
||||||
|
simple_polygons=None,
|
||||||
content=None,
|
content=None,
|
||||||
reference=None,
|
reference=None,
|
||||||
template_name='Example template',
|
template_name='Example template',
|
||||||
@@ -676,6 +677,7 @@ def broadcast_message_json(
|
|||||||
'areas': areas or [
|
'areas': areas or [
|
||||||
'ctry19-E92000001', 'ctry19-S92000003',
|
'ctry19-E92000001', 'ctry19-S92000003',
|
||||||
],
|
],
|
||||||
|
'simple_polygons': simple_polygons or [],
|
||||||
|
|
||||||
'status': status,
|
'status': status,
|
||||||
|
|
||||||
|
|||||||
@@ -684,6 +684,53 @@ def test_preview_broadcast_areas_page(
|
|||||||
] == estimates
|
] == estimates
|
||||||
|
|
||||||
|
|
||||||
|
def test_preview_broadcast_areas_page_with_custom_polygons(
|
||||||
|
mocker,
|
||||||
|
client_request,
|
||||||
|
service_one,
|
||||||
|
fake_uuid,
|
||||||
|
):
|
||||||
|
service_one['permissions'] += ['broadcast']
|
||||||
|
mocker.patch(
|
||||||
|
'app.broadcast_message_api_client.get_broadcast_message',
|
||||||
|
return_value=broadcast_message_json(
|
||||||
|
id_=fake_uuid,
|
||||||
|
template_id=fake_uuid,
|
||||||
|
created_by_id=fake_uuid,
|
||||||
|
service_id=SERVICE_ONE_ID,
|
||||||
|
status='draft',
|
||||||
|
areas=['Area one', 'Area two', 'Area three'],
|
||||||
|
simple_polygons=[
|
||||||
|
[[1, 2], [3, 4], [5, 6]],
|
||||||
|
[[7, 8], [9, 10], [11, 12]],
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
page = client_request.get(
|
||||||
|
'.preview_broadcast_areas',
|
||||||
|
service_id=SERVICE_ONE_ID,
|
||||||
|
broadcast_message_id=fake_uuid,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert [
|
||||||
|
normalize_spaces(item.text)
|
||||||
|
for item in page.select('ul.area-list li.area-list-item')
|
||||||
|
] == [
|
||||||
|
'Area one remove', 'Area two remove', 'Area three remove',
|
||||||
|
]
|
||||||
|
|
||||||
|
assert len(page.select('#area-list-map')) == 1
|
||||||
|
|
||||||
|
assert [
|
||||||
|
normalize_spaces(item.text)
|
||||||
|
for item in page.select('ul li.area-list-key')
|
||||||
|
] == [
|
||||||
|
'An area of 722.3 square miles Will get the alert',
|
||||||
|
'An extra area of 1,402.5 square miles is Likely to get the alert',
|
||||||
|
'Unknown number of phones',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('areas, expected_list', (
|
@pytest.mark.parametrize('areas, expected_list', (
|
||||||
([], [
|
([], [
|
||||||
'Countries',
|
'Countries',
|
||||||
@@ -1260,8 +1307,8 @@ def test_start_broadcasting(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('endpoint, extra_fields, expected_paragraphs', (
|
@pytest.mark.parametrize('endpoint, created_by_api, extra_fields, expected_paragraphs', (
|
||||||
('.view_current_broadcast', {
|
('.view_current_broadcast', False, {
|
||||||
'status': 'broadcasting',
|
'status': 'broadcasting',
|
||||||
'finishes_at': '2020-02-23T23:23:23.000000',
|
'finishes_at': '2020-02-23T23:23:23.000000',
|
||||||
}, [
|
}, [
|
||||||
@@ -1269,7 +1316,15 @@ def test_start_broadcasting(
|
|||||||
'Prepared by Alice and approved by Bob.',
|
'Prepared by Alice and approved by Bob.',
|
||||||
'Broadcasting stops tomorrow at 11:23pm.'
|
'Broadcasting stops tomorrow at 11:23pm.'
|
||||||
]),
|
]),
|
||||||
('.view_previous_broadcast', {
|
('.view_current_broadcast', True, {
|
||||||
|
'status': 'broadcasting',
|
||||||
|
'finishes_at': '2020-02-23T23:23:23.000000',
|
||||||
|
}, [
|
||||||
|
'Live since 20 February at 8:20pm Stop broadcasting',
|
||||||
|
'Created from an API call and approved by Alice.',
|
||||||
|
'Broadcasting stops tomorrow at 11:23pm.'
|
||||||
|
]),
|
||||||
|
('.view_previous_broadcast', False, {
|
||||||
'status': 'broadcasting',
|
'status': 'broadcasting',
|
||||||
'finishes_at': '2020-02-22T22:20:20.000000', # 2 mins before now()
|
'finishes_at': '2020-02-22T22:20:20.000000', # 2 mins before now()
|
||||||
}, [
|
}, [
|
||||||
@@ -1277,7 +1332,15 @@ def test_start_broadcasting(
|
|||||||
'Prepared by Alice and approved by Bob.',
|
'Prepared by Alice and approved by Bob.',
|
||||||
'Finished broadcasting today at 10:20pm.'
|
'Finished broadcasting today at 10:20pm.'
|
||||||
]),
|
]),
|
||||||
('.view_previous_broadcast', {
|
('.view_previous_broadcast', True, {
|
||||||
|
'status': 'broadcasting',
|
||||||
|
'finishes_at': '2020-02-22T22:20:20.000000', # 2 mins before now()
|
||||||
|
}, [
|
||||||
|
'Broadcast on 20 February at 8:20pm.',
|
||||||
|
'Created from an API call and approved by Alice.',
|
||||||
|
'Finished broadcasting today at 10:20pm.'
|
||||||
|
]),
|
||||||
|
('.view_previous_broadcast', False, {
|
||||||
'status': 'completed',
|
'status': 'completed',
|
||||||
'finishes_at': '2020-02-21T21:21:21.000000',
|
'finishes_at': '2020-02-21T21:21:21.000000',
|
||||||
}, [
|
}, [
|
||||||
@@ -1285,7 +1348,7 @@ def test_start_broadcasting(
|
|||||||
'Prepared by Alice and approved by Bob.',
|
'Prepared by Alice and approved by Bob.',
|
||||||
'Finished broadcasting yesterday at 9:21pm.',
|
'Finished broadcasting yesterday at 9:21pm.',
|
||||||
]),
|
]),
|
||||||
('.view_previous_broadcast', {
|
('.view_previous_broadcast', False, {
|
||||||
'status': 'cancelled',
|
'status': 'cancelled',
|
||||||
'cancelled_by_id': sample_uuid,
|
'cancelled_by_id': sample_uuid,
|
||||||
'cancelled_at': '2020-02-21T21:21:21.000000',
|
'cancelled_at': '2020-02-21T21:21:21.000000',
|
||||||
@@ -1304,6 +1367,7 @@ def test_view_broadcast_message_page(
|
|||||||
mock_get_broadcast_template,
|
mock_get_broadcast_template,
|
||||||
fake_uuid,
|
fake_uuid,
|
||||||
endpoint,
|
endpoint,
|
||||||
|
created_by_api,
|
||||||
extra_fields,
|
extra_fields,
|
||||||
expected_paragraphs,
|
expected_paragraphs,
|
||||||
):
|
):
|
||||||
@@ -1313,7 +1377,7 @@ def test_view_broadcast_message_page(
|
|||||||
id_=fake_uuid,
|
id_=fake_uuid,
|
||||||
service_id=SERVICE_ONE_ID,
|
service_id=SERVICE_ONE_ID,
|
||||||
template_id=fake_uuid,
|
template_id=fake_uuid,
|
||||||
created_by_id=fake_uuid,
|
created_by_id=None if created_by_api else fake_uuid,
|
||||||
approved_by_id=fake_uuid,
|
approved_by_id=fake_uuid,
|
||||||
starts_at='2020-02-20T20:20:20.000000',
|
starts_at='2020-02-20T20:20:20.000000',
|
||||||
**extra_fields
|
**extra_fields
|
||||||
@@ -1517,6 +1581,49 @@ def test_view_pending_broadcast_without_template(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time('2020-02-22T22:22:22.000000')
|
||||||
|
def test_view_pending_broadcast_from_api_call(
|
||||||
|
mocker,
|
||||||
|
client_request,
|
||||||
|
service_one,
|
||||||
|
active_user_with_permissions,
|
||||||
|
fake_uuid,
|
||||||
|
):
|
||||||
|
mocker.patch(
|
||||||
|
'app.broadcast_message_api_client.get_broadcast_message',
|
||||||
|
return_value=broadcast_message_json(
|
||||||
|
id_=fake_uuid,
|
||||||
|
service_id=SERVICE_ONE_ID,
|
||||||
|
template_id=None,
|
||||||
|
created_by_id=None, # No user created this broadcast
|
||||||
|
finishes_at=None,
|
||||||
|
status='pending-approval',
|
||||||
|
reference='abc123',
|
||||||
|
content='Uh-oh',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
service_one['permissions'] += ['broadcast']
|
||||||
|
|
||||||
|
page = client_request.get(
|
||||||
|
'.view_current_broadcast',
|
||||||
|
service_id=SERVICE_ONE_ID,
|
||||||
|
broadcast_message_id=fake_uuid,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert (
|
||||||
|
normalize_spaces(page.select_one('.banner').text)
|
||||||
|
) == (
|
||||||
|
'An API call wants to broadcast abc123 '
|
||||||
|
'Start broadcasting now Reject this alert'
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
normalize_spaces(page.select_one('.broadcast-message-wrapper').text)
|
||||||
|
) == (
|
||||||
|
'Emergency alert '
|
||||||
|
'Uh-oh'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@freeze_time('2020-02-22T22:22:22.000000')
|
@freeze_time('2020-02-22T22:22:22.000000')
|
||||||
def test_cant_approve_own_broadcast(
|
def test_cant_approve_own_broadcast(
|
||||||
mocker,
|
mocker,
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
from app.models.broadcast_message import BroadcastMessage
|
from app.models.broadcast_message import BroadcastMessage
|
||||||
from tests import broadcast_message_json
|
from tests import broadcast_message_json
|
||||||
|
|
||||||
@@ -45,3 +47,24 @@ def test_content_comes_from_attribute_not_template(fake_uuid):
|
|||||||
created_by_id=fake_uuid,
|
created_by_id=fake_uuid,
|
||||||
))
|
))
|
||||||
assert broadcast_message.content == 'This is a test'
|
assert broadcast_message.content == 'This is a test'
|
||||||
|
|
||||||
|
|
||||||
|
def test_raises_for_missing_areas(fake_uuid):
|
||||||
|
broadcast_message = BroadcastMessage(broadcast_message_json(
|
||||||
|
id_=fake_uuid,
|
||||||
|
service_id=fake_uuid,
|
||||||
|
template_id=fake_uuid,
|
||||||
|
status='draft',
|
||||||
|
created_by_id=fake_uuid,
|
||||||
|
areas=[
|
||||||
|
'wd20-E05009372',
|
||||||
|
'something else',
|
||||||
|
],
|
||||||
|
))
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError) as exception:
|
||||||
|
broadcast_message.areas
|
||||||
|
|
||||||
|
assert str(exception.value) == (
|
||||||
|
'BroadcastMessage has 2 areas but 1 found in the library'
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user