- Add validation methods for post notification.

- Use these validation methods in post_sms_notification and the version 1 of post_notification.
- Create a v2 error handlers.
- InvalidRequest has a to_dict method for private and v1 error responses and a to_dict_v2 method to create the v2 of the error responses.
- Each validation method has extensive unit tests, so the unit test for the endpoint do not need to check every error case, but check that the error handle formats the message correctly.
- The format of the error messages is still a work on progress.
- This version of the api could be deployed without causing a problem to the application.
- The new endpoing is still a work in progress and is not being used yet.
This commit is contained in:
Rebecca Law
2016-10-27 11:46:37 +01:00
parent 23a4f00e56
commit c2eecdae36
12 changed files with 328 additions and 101 deletions

View File

@@ -1,3 +1,32 @@
from notifications_utils.renderers import PassThrough
from notifications_utils.template import Template
from app.models import SMS_TYPE
from app.notifications.validators import check_sms_content_char_count
from app.v2.errors import BadRequestError
def create_content_for_notification(template, personalisation):
template_object = Template(
template.__dict__,
personalisation,
renderer=PassThrough()
)
if template_object.missing_data:
message = 'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
errors = {'template': [message]}
raise BadRequestError(errors)
if template_object.additional_data:
message = 'Personalisation not needed for template: {}'.format(", ".join(template_object.additional_data))
errors = {'template': [message]}
raise BadRequestError(fields=errors)
if template_object.template_type == SMS_TYPE:
check_sms_content_char_count(template_object.replaced_content_count)
return template_object
def persist_notification():
'''
persist the notification