mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 14:31:57 -05:00
Merge pull request #2741 from alphagov/make-sms-char-count-consistent
Update validators to use is_message_too_long()
This commit is contained in:
@@ -125,10 +125,12 @@ def requires_auth():
|
||||
g.service_id = api_key.service_id
|
||||
_request_ctx_stack.top.authenticated_service = service
|
||||
_request_ctx_stack.top.api_user = api_key
|
||||
current_app.logger.info('API authorised for service {} with api key {}, using issuer {}'.format(
|
||||
|
||||
current_app.logger.info('API authorised for service {} with api key {}, using issuer {} for URL: {}'.format(
|
||||
service.id,
|
||||
api_key.id,
|
||||
request.headers.get('User-Agent')
|
||||
request.headers.get('User-Agent'),
|
||||
request.base_url
|
||||
))
|
||||
return
|
||||
else:
|
||||
|
||||
@@ -8,7 +8,6 @@ from flask import (
|
||||
from app import api_user, authenticated_service
|
||||
from app.config import QueueNames
|
||||
from app.dao import (
|
||||
templates_dao,
|
||||
notifications_dao
|
||||
)
|
||||
from app.errors import (
|
||||
@@ -25,10 +24,9 @@ from app.notifications.process_notifications import (
|
||||
simulated_recipient
|
||||
)
|
||||
from app.notifications.validators import (
|
||||
check_template_is_for_notification_type,
|
||||
check_template_is_active,
|
||||
check_rate_limiting,
|
||||
service_has_permission,
|
||||
validate_template
|
||||
)
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
@@ -102,14 +100,12 @@ def send_notification(notification_type):
|
||||
|
||||
check_rate_limiting(authenticated_service, api_user)
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template, template_with_content = validate_template(
|
||||
template_id=notification_form['template'],
|
||||
service_id=authenticated_service.id)
|
||||
|
||||
check_template_is_for_notification_type(notification_type, template.template_type)
|
||||
check_template_is_active(template)
|
||||
|
||||
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
|
||||
personalisation=notification_form.get('personalisation', {}),
|
||||
service=authenticated_service,
|
||||
notification_type=notification_type
|
||||
)
|
||||
|
||||
_service_allowed_to_send_to(notification_form, authenticated_service)
|
||||
if not service_has_permission(notification_type, authenticated_service.permissions):
|
||||
@@ -147,7 +143,7 @@ def send_notification(notification_type):
|
||||
data=get_notification_return_data(
|
||||
notification_model.id,
|
||||
notification_form,
|
||||
template_object)
|
||||
template_with_content)
|
||||
), 201
|
||||
|
||||
|
||||
|
||||
@@ -124,9 +124,10 @@ def validate_and_format_recipient(send_to, key_type, service, notification_type,
|
||||
return validate_and_format_email_address(email_address=send_to)
|
||||
|
||||
|
||||
def check_sms_content_char_count(content_count):
|
||||
if content_count > SMS_CHAR_COUNT_LIMIT:
|
||||
message = 'Content for template has a character count greater than the limit of {}'.format(SMS_CHAR_COUNT_LIMIT)
|
||||
def check_content_char_count(template_with_content):
|
||||
if template_with_content.is_message_too_long():
|
||||
message = f"Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. " \
|
||||
f"Your message is {template_with_content.content_count_without_prefix} characters"
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
|
||||
@@ -151,9 +152,10 @@ def validate_template(template_id, personalisation, service, notification_type):
|
||||
check_template_is_active(template)
|
||||
|
||||
template_with_content = create_content_for_notification(template, personalisation)
|
||||
|
||||
check_notification_content_is_not_empty(template_with_content)
|
||||
if template.template_type == SMS_TYPE:
|
||||
check_sms_content_char_count(template_with_content.content_count)
|
||||
|
||||
check_content_char_count(template_with_content)
|
||||
|
||||
return template, template_with_content
|
||||
|
||||
|
||||
Reference in New Issue
Block a user