2020-07-07 11:42:38 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
import iso8601
|
2021-03-10 13:55:06 +00:00
|
|
|
from flask import Blueprint, current_app, jsonify, request
|
2021-04-22 14:42:54 +01:00
|
|
|
from notifications_utils.template import BroadcastMessageTemplate
|
2021-01-29 15:20:29 +00:00
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.broadcast_message.broadcast_message_schema import (
|
|
|
|
|
create_broadcast_message_schema,
|
|
|
|
|
update_broadcast_message_schema,
|
|
|
|
|
update_broadcast_message_status_schema,
|
|
|
|
|
)
|
|
|
|
|
from app.celery.broadcast_message_tasks import send_broadcast_event
|
2020-07-09 18:22:53 +01:00
|
|
|
from app.config import QueueNames
|
2020-07-07 11:42:38 +01:00
|
|
|
from app.dao.broadcast_message_dao import (
|
|
|
|
|
dao_get_broadcast_message_by_id_and_service_id,
|
|
|
|
|
dao_get_broadcast_messages_for_service,
|
|
|
|
|
)
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.dao.dao_utils import dao_save_object
|
2020-07-07 11:42:38 +01:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.dao.templates_dao import dao_get_template_by_id_and_service_id
|
|
|
|
|
from app.dao.users_dao import get_user_by_id
|
|
|
|
|
from app.errors import InvalidRequest, register_errors
|
|
|
|
|
from app.models import (
|
|
|
|
|
BroadcastEvent,
|
|
|
|
|
BroadcastEventMessageType,
|
|
|
|
|
BroadcastMessage,
|
|
|
|
|
BroadcastStatusType,
|
2020-07-07 11:42:38 +01:00
|
|
|
)
|
|
|
|
|
from app.schema_validation import validate
|
|
|
|
|
|
|
|
|
|
broadcast_message_blueprint = Blueprint(
|
|
|
|
|
'broadcast_message',
|
|
|
|
|
__name__,
|
|
|
|
|
url_prefix='/service/<uuid:service_id>/broadcast-message'
|
|
|
|
|
)
|
|
|
|
|
register_errors(broadcast_message_blueprint)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_nullable_datetime(dt):
|
|
|
|
|
if dt:
|
|
|
|
|
return iso8601.parse_date(dt).replace(tzinfo=None)
|
|
|
|
|
return dt
|
|
|
|
|
|
|
|
|
|
|
2020-07-14 16:36:41 +01:00
|
|
|
def _update_broadcast_message(broadcast_message, new_status, updating_user):
|
2020-07-16 12:07:55 +01:00
|
|
|
if updating_user not in broadcast_message.service.users:
|
2021-04-06 15:55:56 +01:00
|
|
|
# we allow platform admins to cancel broadcasts
|
|
|
|
|
if not (new_status == BroadcastStatusType.CANCELLED and updating_user.platform_admin):
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
f'User {updating_user.id} cannot update broadcast_message {broadcast_message.id} from other service',
|
|
|
|
|
status_code=400
|
|
|
|
|
)
|
2020-07-16 16:02:04 +01:00
|
|
|
|
|
|
|
|
if new_status not in BroadcastStatusType.ALLOWED_STATUS_TRANSITIONS[broadcast_message.status]:
|
2020-07-27 15:54:31 +01:00
|
|
|
raise InvalidRequest(
|
|
|
|
|
f'Cannot move broadcast_message {broadcast_message.id} from {broadcast_message.status} to {new_status}',
|
|
|
|
|
status_code=400
|
2020-07-16 12:07:55 +01:00
|
|
|
)
|
|
|
|
|
|
2020-07-14 16:36:41 +01:00
|
|
|
if new_status == BroadcastStatusType.BROADCASTING:
|
2021-01-25 11:09:49 +00:00
|
|
|
# training mode services can approve their own broadcasts
|
2021-01-22 16:56:05 +00:00
|
|
|
if updating_user == broadcast_message.created_by and not broadcast_message.service.restricted:
|
2020-07-27 15:54:31 +01:00
|
|
|
raise InvalidRequest(
|
|
|
|
|
f'User {updating_user.id} cannot approve their own broadcast_message {broadcast_message.id}',
|
|
|
|
|
status_code=400
|
2020-07-15 17:31:59 +01:00
|
|
|
)
|
2021-02-09 17:01:04 +00:00
|
|
|
elif len(broadcast_message.areas['simple_polygons']) == 0:
|
2020-09-02 18:10:56 +01:00
|
|
|
raise InvalidRequest(
|
|
|
|
|
f'broadcast_message {broadcast_message.id} has no selected areas and so cannot be broadcasted.',
|
|
|
|
|
status_code=400
|
|
|
|
|
)
|
2020-07-15 17:31:59 +01:00
|
|
|
else:
|
|
|
|
|
broadcast_message.approved_at = datetime.utcnow()
|
|
|
|
|
broadcast_message.approved_by = updating_user
|
2020-07-14 16:36:41 +01:00
|
|
|
|
|
|
|
|
if new_status == BroadcastStatusType.CANCELLED:
|
|
|
|
|
broadcast_message.cancelled_at = datetime.utcnow()
|
|
|
|
|
broadcast_message.cancelled_by = updating_user
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
f'broadcast_message {broadcast_message.id} moving from {broadcast_message.status} to {new_status}'
|
|
|
|
|
)
|
|
|
|
|
broadcast_message.status = new_status
|
|
|
|
|
|
|
|
|
|
|
2020-07-07 11:42:38 +01:00
|
|
|
@broadcast_message_blueprint.route('', methods=['GET'])
|
|
|
|
|
def get_broadcast_messages_for_service(service_id):
|
|
|
|
|
# TODO: should this return template content/data in some way? or can we rely on them being cached admin side.
|
|
|
|
|
# we might need stuff like template name for showing on the dashboard.
|
|
|
|
|
# TODO: should this paginate or filter on dates or anything?
|
|
|
|
|
broadcast_messages = [o.serialize() for o in dao_get_broadcast_messages_for_service(service_id)]
|
|
|
|
|
return jsonify(broadcast_messages=broadcast_messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@broadcast_message_blueprint.route('/<uuid:broadcast_message_id>', methods=['GET'])
|
|
|
|
|
def get_broadcast_message(service_id, broadcast_message_id):
|
|
|
|
|
return jsonify(dao_get_broadcast_message_by_id_and_service_id(broadcast_message_id, service_id).serialize())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@broadcast_message_blueprint.route('', methods=['POST'])
|
|
|
|
|
def create_broadcast_message(service_id):
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
|
|
|
|
|
validate(data, create_broadcast_message_schema)
|
|
|
|
|
service = dao_fetch_service_by_id(data['service_id'])
|
|
|
|
|
user = get_user_by_id(data['created_by'])
|
2021-01-08 16:38:51 +00:00
|
|
|
personalisation = data.get('personalisation', {})
|
2021-01-15 14:35:23 +00:00
|
|
|
template_id = data.get('template_id')
|
|
|
|
|
|
|
|
|
|
if template_id:
|
|
|
|
|
template = dao_get_template_by_id_and_service_id(
|
|
|
|
|
template_id, data['service_id']
|
|
|
|
|
)
|
2021-05-10 15:21:28 +01:00
|
|
|
content = str(template._as_utils_template_with_personalisation(
|
2021-01-15 14:35:23 +00:00
|
|
|
personalisation
|
2021-05-10 15:21:28 +01:00
|
|
|
))
|
2021-01-15 14:43:40 +00:00
|
|
|
reference = None
|
2021-01-15 14:35:23 +00:00
|
|
|
else:
|
2021-05-10 15:21:28 +01:00
|
|
|
temporary_template = BroadcastMessageTemplate.from_content(data['content'])
|
2021-04-22 14:42:54 +01:00
|
|
|
if temporary_template.content_too_long:
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
(
|
|
|
|
|
f'Content must be '
|
|
|
|
|
f'{temporary_template.max_content_count:,.0f} '
|
|
|
|
|
f'characters or fewer'
|
|
|
|
|
) + (
|
|
|
|
|
' (because it could not be GSM7 encoded)'
|
|
|
|
|
if temporary_template.non_gsm_characters else ''
|
|
|
|
|
),
|
|
|
|
|
status_code=400,
|
|
|
|
|
)
|
2021-05-10 15:21:28 +01:00
|
|
|
template = None
|
|
|
|
|
content = str(temporary_template)
|
|
|
|
|
reference = data['reference']
|
2021-01-15 14:35:23 +00:00
|
|
|
|
2020-07-07 11:42:38 +01:00
|
|
|
broadcast_message = BroadcastMessage(
|
|
|
|
|
service_id=service.id,
|
2021-01-15 14:35:23 +00:00
|
|
|
template_id=template_id,
|
|
|
|
|
template_version=template.version if template else None,
|
2021-01-08 16:38:51 +00:00
|
|
|
personalisation=personalisation,
|
2021-09-01 17:25:55 +01:00
|
|
|
areas=data.get("areas", {}),
|
2020-07-07 11:42:38 +01:00
|
|
|
status=BroadcastStatusType.DRAFT,
|
|
|
|
|
starts_at=_parse_nullable_datetime(data.get('starts_at')),
|
|
|
|
|
finishes_at=_parse_nullable_datetime(data.get('finishes_at')),
|
|
|
|
|
created_by_id=user.id,
|
2021-01-15 14:35:23 +00:00
|
|
|
content=content,
|
2021-01-15 14:43:40 +00:00
|
|
|
reference=reference,
|
2021-01-26 17:39:38 +00:00
|
|
|
stubbed=service.restricted
|
2020-07-07 11:42:38 +01:00
|
|
|
)
|
|
|
|
|
|
2020-08-14 17:37:16 +01:00
|
|
|
dao_save_object(broadcast_message)
|
2020-07-07 11:42:38 +01:00
|
|
|
|
|
|
|
|
return jsonify(broadcast_message.serialize()), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@broadcast_message_blueprint.route('/<uuid:broadcast_message_id>', methods=['POST'])
|
|
|
|
|
def update_broadcast_message(service_id, broadcast_message_id):
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
|
2021-08-26 12:39:16 +01:00
|
|
|
# TEMPORARY: while we migrate and backfill "areas"
|
|
|
|
|
force_override = data.pop("force_override", False)
|
|
|
|
|
|
2020-07-07 11:42:38 +01:00
|
|
|
validate(data, update_broadcast_message_schema)
|
|
|
|
|
|
|
|
|
|
broadcast_message = dao_get_broadcast_message_by_id_and_service_id(broadcast_message_id, service_id)
|
|
|
|
|
|
2021-08-26 12:39:16 +01:00
|
|
|
if not force_override and broadcast_message.status not in BroadcastStatusType.PRE_BROADCAST_STATUSES:
|
2020-07-27 15:54:31 +01:00
|
|
|
raise InvalidRequest(
|
|
|
|
|
f'Cannot update broadcast_message {broadcast_message.id} while it has status {broadcast_message.status}',
|
|
|
|
|
status_code=400
|
2020-07-10 15:30:25 +01:00
|
|
|
)
|
|
|
|
|
|
2021-09-01 17:25:55 +01:00
|
|
|
areas = data.get("areas", {})
|
2021-08-25 13:49:18 +01:00
|
|
|
|
2021-08-25 15:48:09 +01:00
|
|
|
if ('ids' in areas and 'simple_polygons' not in areas) or ('ids' not in areas and 'simple_polygons' in areas):
|
2020-09-07 15:10:04 +01:00
|
|
|
raise InvalidRequest(
|
2021-08-25 15:48:09 +01:00
|
|
|
f'Cannot update broadcast_message {broadcast_message.id}, area IDs or polygons are missing.',
|
2020-09-07 15:10:04 +01:00
|
|
|
status_code=400
|
|
|
|
|
)
|
|
|
|
|
|
2020-07-07 11:42:38 +01:00
|
|
|
if 'personalisation' in data:
|
|
|
|
|
broadcast_message.personalisation = data['personalisation']
|
|
|
|
|
if 'starts_at' in data:
|
|
|
|
|
broadcast_message.starts_at = _parse_nullable_datetime(data['starts_at'])
|
|
|
|
|
if 'finishes_at' in data:
|
2020-07-09 15:36:08 +01:00
|
|
|
broadcast_message.finishes_at = _parse_nullable_datetime(data['finishes_at'])
|
2021-08-27 13:13:04 +01:00
|
|
|
if 'ids' in areas and 'simple_polygons' in areas:
|
2021-08-25 13:49:18 +01:00
|
|
|
broadcast_message.areas = areas
|
2020-07-07 11:42:38 +01:00
|
|
|
|
2020-08-14 17:37:16 +01:00
|
|
|
dao_save_object(broadcast_message)
|
2020-07-07 11:42:38 +01:00
|
|
|
|
|
|
|
|
return jsonify(broadcast_message.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@broadcast_message_blueprint.route('/<uuid:broadcast_message_id>/status', methods=['POST'])
|
|
|
|
|
def update_broadcast_message_status(service_id, broadcast_message_id):
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
|
|
|
|
|
validate(data, update_broadcast_message_status_schema)
|
|
|
|
|
broadcast_message = dao_get_broadcast_message_by_id_and_service_id(broadcast_message_id, service_id)
|
|
|
|
|
|
2021-04-19 16:54:25 +01:00
|
|
|
if not broadcast_message.service.active:
|
|
|
|
|
raise InvalidRequest("Updating broadcast message is not allowed: service is inactive ", 403)
|
|
|
|
|
|
2020-07-07 11:42:38 +01:00
|
|
|
new_status = data['status']
|
2020-07-14 16:36:41 +01:00
|
|
|
updating_user = get_user_by_id(data['created_by'])
|
2020-07-07 11:42:38 +01:00
|
|
|
|
2020-07-14 16:36:41 +01:00
|
|
|
_update_broadcast_message(broadcast_message, new_status, updating_user)
|
2020-08-14 17:37:16 +01:00
|
|
|
dao_save_object(broadcast_message)
|
2020-07-07 11:42:38 +01:00
|
|
|
|
2020-08-04 19:00:10 +01:00
|
|
|
if new_status in {BroadcastStatusType.BROADCASTING, BroadcastStatusType.CANCELLED}:
|
|
|
|
|
_create_broadcast_event(broadcast_message)
|
2020-07-09 18:22:53 +01:00
|
|
|
|
2020-07-07 11:42:38 +01:00
|
|
|
return jsonify(broadcast_message.serialize()), 200
|
2020-08-04 19:00:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _create_broadcast_event(broadcast_message):
|
|
|
|
|
"""
|
2021-04-13 18:36:31 +01:00
|
|
|
If the service is live and the broadcast message is not stubbed, creates a broadcast event, stores it in the
|
|
|
|
|
database, and triggers the task to send the CAP XML off.
|
2020-08-04 19:00:10 +01:00
|
|
|
"""
|
2021-04-13 18:36:31 +01:00
|
|
|
service = broadcast_message.service
|
|
|
|
|
|
|
|
|
|
if not broadcast_message.stubbed and not service.restricted:
|
2021-04-13 16:50:26 +01:00
|
|
|
msg_types = {
|
|
|
|
|
BroadcastStatusType.BROADCASTING: BroadcastEventMessageType.ALERT,
|
|
|
|
|
BroadcastStatusType.CANCELLED: BroadcastEventMessageType.CANCEL,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event = BroadcastEvent(
|
2021-04-13 18:36:31 +01:00
|
|
|
service=service,
|
2021-04-13 16:50:26 +01:00
|
|
|
broadcast_message=broadcast_message,
|
|
|
|
|
message_type=msg_types[broadcast_message.status],
|
|
|
|
|
transmitted_content={"body": broadcast_message.content},
|
|
|
|
|
transmitted_areas=broadcast_message.areas,
|
|
|
|
|
# TODO: Probably move this somewhere more standalone too and imply that it shouldn't change. Should it
|
|
|
|
|
# include a service based identifier too? eg "flood-warnings@notifications.service.gov.uk" or similar
|
|
|
|
|
transmitted_sender='notifications.service.gov.uk',
|
|
|
|
|
|
|
|
|
|
# TODO: Should this be set to now? Or the original starts_at?
|
|
|
|
|
transmitted_starts_at=broadcast_message.starts_at,
|
|
|
|
|
transmitted_finishes_at=broadcast_message.finishes_at,
|
|
|
|
|
)
|
2020-08-04 19:00:10 +01:00
|
|
|
|
2021-04-13 16:50:26 +01:00
|
|
|
dao_save_object(event)
|
2020-08-04 19:00:10 +01:00
|
|
|
|
2021-01-26 17:50:56 +00:00
|
|
|
send_broadcast_event.apply_async(
|
|
|
|
|
kwargs={'broadcast_event_id': str(event.id)},
|
|
|
|
|
queue=QueueNames.BROADCASTS
|
|
|
|
|
)
|
2021-04-13 18:36:31 +01:00
|
|
|
elif broadcast_message.stubbed != service.restricted:
|
|
|
|
|
# It's possible for a service to create a broadcast in trial mode, and then approve it after the
|
|
|
|
|
# service is live (or vice versa). We don't think it's safe to send such broadcasts, as the service
|
|
|
|
|
# has changed since they were created. Log an error instead.
|
|
|
|
|
current_app.logger.error(
|
|
|
|
|
f'Broadcast event not created. Stubbed status of broadcast message was {broadcast_message.stubbed}'
|
|
|
|
|
f' but service was {"in trial mode" if service.restricted else "live"}'
|
|
|
|
|
)
|