mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 02:41:14 -05:00
Merge pull request #3325 from alphagov/prevent-empty-areas-178986763
Add validation to prevent blank area names
This commit is contained in:
@@ -76,6 +76,7 @@ post_broadcast_schema = {
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"pattern": "([a-zA-Z1-9]+ )*[a-zA-Z1-9]+",
|
||||
},
|
||||
"polygons": {
|
||||
"type": "array",
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user