Display broadcasts without a template

At the moment the admin app expects all broadcasts to have a template,
and expects the content of the alert to come from the template.

This commit makes it so those pages can still get a `Template` instance,
but populated with content straight from the `content` field in the
database.
This commit is contained in:
Chris Hill-Scott
2021-01-15 15:11:43 +00:00
parent 74243a88fb
commit db4d9f886c
7 changed files with 86 additions and 21 deletions

View File

@@ -138,12 +138,26 @@ def broadcast(service_id, template_id):
@user_has_permissions('send_messages')
@service_has_permission('broadcast')
def preview_broadcast_areas(service_id, broadcast_message_id):
broadcast_message = BroadcastMessage.from_id(
broadcast_message_id,
service_id=current_service.id,
)
if broadcast_message.template_id:
back_link = url_for(
'.view_template',
service_id=current_service.id,
template_id=broadcast_message.template_id,
)
else:
back_link = url_for(
'.write_new_broadcast',
service_id=current_service.id,
)
return render_template(
'views/broadcast/preview-areas.html',
broadcast_message=BroadcastMessage.from_id(
broadcast_message_id,
service_id=current_service.id,
),
broadcast_message=broadcast_message,
back_link=back_link,
)

View File

@@ -13,7 +13,6 @@ from app.models.user import User
from app.notify_client.broadcast_message_api_client import (
broadcast_message_api_client,
)
from app.notify_client.service_api_client import service_api_client
class BroadcastMessage(JSONModel):
@@ -22,8 +21,6 @@ class BroadcastMessage(JSONModel):
'id',
'service_id',
'template_id',
'template_name',
'template_version',
'content',
'service_id',
'created_by',
@@ -101,14 +98,19 @@ class BroadcastMessage(JSONModel):
def simple_polygons(self):
return self.get_simple_polygons(areas=self.areas)
@property
def reference(self):
if self.template_id:
return self._dict['template_name']
return self._dict['reference']
@property
def template(self):
response = service_api_client.get_service_template(
self.service_id,
self.template_id,
version=self.template_version,
)
return BroadcastPreviewTemplate(response['data'])
return BroadcastPreviewTemplate({
'template_type': BroadcastPreviewTemplate.template_type,
'name': self.reference,
'content': self.content,
})
@property
def status(self):

View File

@@ -5,7 +5,7 @@
<div class="keyline-block">
<div class="file-list govuk-!-margin-bottom-2">
<h2>
<a class="file-list-filename-large govuk-link govuk-link--no-visited-state" href="{{ url_for(view_broadcast_endpoint, service_id=current_service.id, broadcast_message_id=item.id) }}">{{ item.template_name }}</a>
<a class="file-list-filename-large govuk-link govuk-link--no-visited-state" href="{{ url_for(view_broadcast_endpoint, service_id=current_service.id, broadcast_message_id=item.id) }}">{{ item.template.name }}</a>
</h2>
<div class="govuk-grid-row">
<div class="govuk-grid-column-one-half">

View File

@@ -21,7 +21,7 @@
{{ page_header(
"Choose where to broadcast to",
back_link=url_for('.view_template', service_id=current_service.id, template_id=broadcast_message.template_id)
back_link=back_link
) }}
{% for area in broadcast_message.areas %}

View File

@@ -20,15 +20,15 @@
{% block service_page_title %}
{% if broadcast_message.status == 'pending-approval' %}
{% if 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') %}
{{ broadcast_message.created_by.name }} wants to broadcast
{{ broadcast_message.template_name }}
{{ broadcast_message.template.name }}
{% else %}
This alert is waiting for approval
{% endif %}
{% else %}
{{ broadcast_message.template_name }}
{{ broadcast_message.template.name }}
{% endif %}
{% endblock %}
@@ -40,7 +40,7 @@
{% if broadcast_message.created_by == current_user and current_user.has_permissions('send_messages') %}
<div class="banner govuk-!-margin-bottom-6">
<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
</h1>
{% if current_service.live %}
<p class="govuk-body">
@@ -80,7 +80,7 @@
{% call form_wrapper(class="banner govuk-!-margin-bottom-6") %}
<h1 class="govuk-heading-m govuk-!-margin-top-0 govuk-!-margin-bottom-3">
{{ broadcast_message.created_by.name }} wants to broadcast
{{ broadcast_message.template_name }}
{{ broadcast_message.template.name }}
</h1>
{{ page_footer(
"Start broadcasting now",
@@ -97,7 +97,7 @@
</div>
{% endif %}
{% else %}
{{ page_header(broadcast_message.template_name) }}
{{ page_header(broadcast_message.template.name) }}
{% if broadcast_message.status == 'broadcasting' %}
<p class="govuk-body govuk-!-margin-bottom-2 live-broadcast live-broadcast--left">

View File

@@ -656,6 +656,7 @@ def broadcast_message_json(
cancelled_by_id=None,
areas=None,
content=None,
reference=None,
template_name='Example template',
):
return {
@@ -667,6 +668,7 @@ def broadcast_message_json(
'template_version': 123,
'template_name': template_name,
'content': content or 'This is a test',
'reference': reference,
'personalisation': {},
'areas': areas or [

View File

@@ -1470,6 +1470,53 @@ def test_view_pending_broadcast(
)
@freeze_time('2020-02-22T22:22:22.000000')
def test_view_pending_broadcast_without_template(
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=fake_uuid,
finishes_at=None,
status='pending-approval',
reference='No template test',
content='Uh-oh',
),
)
mocker.patch('app.user_api_client.get_user', side_effect=[
active_user_with_permissions, # Current user
user_json(id_=uuid.uuid4()), # User who created broadcast
])
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)
) == (
'Test User wants to broadcast No template test '
'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')
def test_cant_approve_own_broadcast(
mocker,