mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
@@ -200,7 +200,7 @@ def create_template_object_for_notification(template, personalisation):
|
||||
|
||||
if (
|
||||
template_object.template_type == SMS_TYPE and
|
||||
template_object.content_count > SMS_CHAR_COUNT_LIMIT
|
||||
template_object.is_message_too_long()
|
||||
):
|
||||
message = 'Content has a character count greater than the limit of {}'.format(SMS_CHAR_COUNT_LIMIT)
|
||||
errors = {'content': [message]}
|
||||
|
||||
@@ -7,6 +7,7 @@ from jsonschema import (Draft7Validator, ValidationError, FormatChecker)
|
||||
from notifications_utils.recipients import (validate_phone_number, validate_email_address, InvalidPhoneError,
|
||||
InvalidEmailError)
|
||||
|
||||
from app.v2.errors import BadRequestError
|
||||
|
||||
format_checker = FormatChecker()
|
||||
|
||||
@@ -56,6 +57,9 @@ def validate_schema_date_with_hour(instance):
|
||||
|
||||
|
||||
def validate(json_to_validate, schema):
|
||||
if json_to_validate is None:
|
||||
raise BadRequestError(message="Request body is empty.",
|
||||
status_code=400)
|
||||
validator = Draft7Validator(schema, format_checker=format_checker)
|
||||
errors = list(validator.iter_errors(json_to_validate))
|
||||
if errors.__len__() > 0:
|
||||
|
||||
@@ -49,7 +49,7 @@ def _content_count_greater_than_limit(content, template_type):
|
||||
if template_type != SMS_TYPE:
|
||||
return False
|
||||
template = SMSMessageTemplate({'content': content, 'template_type': template_type})
|
||||
return template.content_count > SMS_CHAR_COUNT_LIMIT
|
||||
return template.is_message_too_long()
|
||||
|
||||
|
||||
def validate_parent_folder(template_json):
|
||||
|
||||
@@ -62,10 +62,11 @@ from app.v2.notifications.notification_schemas import (
|
||||
|
||||
@v2_notification_blueprint.route('/{}'.format(LETTER_TYPE), methods=['POST'])
|
||||
def post_precompiled_letter_notification():
|
||||
if 'content' not in (request.get_json() or {}):
|
||||
request_json = get_valid_json()
|
||||
if 'content' not in (request_json or {}):
|
||||
return post_notification(LETTER_TYPE)
|
||||
|
||||
form = validate(request.get_json(), post_precompiled_letter_request)
|
||||
form = validate(request_json, post_precompiled_letter_request)
|
||||
|
||||
# Check permission to send letters
|
||||
check_service_has_permission(LETTER_TYPE, authenticated_service.permissions)
|
||||
@@ -99,11 +100,7 @@ def post_precompiled_letter_notification():
|
||||
|
||||
@v2_notification_blueprint.route('/<notification_type>', methods=['POST'])
|
||||
def post_notification(notification_type):
|
||||
try:
|
||||
request_json = request.get_json()
|
||||
except BadRequest as e:
|
||||
raise BadRequestError(message="Error decoding arguments: {}".format(e.description),
|
||||
status_code=400)
|
||||
request_json = get_valid_json()
|
||||
|
||||
if notification_type == EMAIL_TYPE:
|
||||
form = validate(request_json, post_email_request)
|
||||
@@ -176,6 +173,15 @@ def post_notification(notification_type):
|
||||
return jsonify(resp), 201
|
||||
|
||||
|
||||
def get_valid_json():
|
||||
try:
|
||||
request_json = request.get_json(force=True)
|
||||
except BadRequest:
|
||||
raise BadRequestError(message="Invalid JSON supplied in POST data",
|
||||
status_code=400)
|
||||
return request_json
|
||||
|
||||
|
||||
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service, reply_to_text=None):
|
||||
form_send_to = form['email_address'] if notification_type == EMAIL_TYPE else form['phone_number']
|
||||
|
||||
|
||||
@@ -11,6 +11,9 @@ from app.v2.template.template_schemas import post_template_preview_request, crea
|
||||
|
||||
@v2_template_blueprint.route("/<template_id>/preview", methods=['POST'])
|
||||
def post_template_preview(template_id):
|
||||
if not request.content_type or request.content_type != 'application/json':
|
||||
raise BadRequestError(message="Content-Type header is not set to application/json.",
|
||||
status_code=400)
|
||||
_data = request.get_json()
|
||||
if _data is None:
|
||||
_data = {}
|
||||
|
||||
Reference in New Issue
Block a user