mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-24 01:11:38 -05:00
Refactor custom validation into own function
This sets a pattern for adding another in the next commits.
This commit is contained in:
@@ -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