Normalise broadcast content before validating length

This changes the content length validation of the internal API to match
the validation of the public broadcast API[1].

This removes the length check from JSONSchema, which isn’t sophisticated
enough to deal with things like normalising newlines or handling
different encodings.

The admin app should catch these errors before they’re raised here, but
it’s best to be belt and braces.

1.7ab0403ae7/app/v2/broadcast/post_broadcast.py (L53-L63)
This commit is contained in:
Chris Hill-Scott
2021-04-22 14:42:54 +01:00
parent 7ab0403ae7
commit 7c6ae40034
3 changed files with 49 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ from datetime import datetime
import iso8601
from flask import Blueprint, current_app, jsonify, request
from notifications_utils.template import BroadcastMessageTemplate
from app.broadcast_message.broadcast_message_schema import (
create_broadcast_message_schema,
@@ -116,6 +117,19 @@ def create_broadcast_message(service_id):
reference = None
else:
template, content, reference = None, data['content'], data['reference']
temporary_template = BroadcastMessageTemplate.from_content(content)
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,
)
broadcast_message = BroadcastMessage(
service_id=service.id,