Merge pull request #3325 from alphagov/prevent-empty-areas-178986763

Add validation to prevent blank area names
This commit is contained in:
Ben Thorner
2021-09-17 15:20:15 +01:00
committed by GitHub
4 changed files with 45 additions and 17 deletions

View File

@@ -76,6 +76,7 @@ post_broadcast_schema = {
"properties": {
"name": {
"type": "string",
"pattern": "([a-zA-Z1-9]+ )*[a-zA-Z1-9]+",
},
"polygons": {
"type": "array",

View File

@@ -41,27 +41,12 @@ def create_broadcast():
broadcast_json = cap_xml_to_dict(cap_xml)
validate(broadcast_json, post_broadcast_schema)
_validate_template(broadcast_json)
polygons = Polygons(list(chain.from_iterable((
area['polygons'] for area in broadcast_json['areas']
))))
template = BroadcastMessageTemplate.from_content(
broadcast_json['content']
)
if template.content_too_long:
raise ValidationError(
message=(
f'description must be {template.max_content_count:,.0f} '
f'characters or fewer'
) + (
' (because it could not be GSM7 encoded)'
if template.non_gsm_characters else ''
),
status_code=400,
)
broadcast_message = BroadcastMessage(
service_id=authenticated_service.id,
content=broadcast_json['content'],
@@ -89,3 +74,21 @@ def create_broadcast():
)
return jsonify(broadcast_message.serialize()), 201
def _validate_template(broadcast_json):
template = BroadcastMessageTemplate.from_content(
broadcast_json['content']
)
if template.content_too_long:
raise ValidationError(
message=(
f'description must be {template.max_content_count:,.0f} '
f'characters or fewer'
) + (
' (because it could not be GSM7 encoded)'
if template.non_gsm_characters else ''
),
status_code=400,
)