Accept and validate personalisation

This commit allows the send notification endpoint to accept an extra parameter,
`personalisation`, the contents of which will be used (later) to replace the
placeholders in the template.

It does validation in the following places:
- at the schema level, to validate the type and (optional) presence of
  personalisation
- at the endpoint, to check whether the personalisation provided matches exactly
  the placeholders in the template

It does not do validation when processing CSV files, as these are assumed to
already have been validated by the admin app.

It explicitly does not persist either the names of the placeholders (these
should always be derived from the template contents unless it really becomes a
performance concern) or the values of the placeholders (because they might be
personal data).
This commit is contained in:
Chris Hill-Scott
2016-02-29 11:23:34 +00:00
parent 68f31c6f84
commit 68eaacaafb
4 changed files with 128 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ from flask import (
url_for
)
from utils.template import Template
from utils.template import Template, NeededByTemplateError, NoPlaceholderForDataError
from app import api_user, encryption, create_uuid
from app.authentication.auth import require_admin
@@ -135,10 +135,10 @@ def send_notification(notification_type):
if errors:
return jsonify(result="error", message=errors), 400
template = Template(templates_dao.dao_get_template_by_id_and_service_id(
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id=notification['template'],
service_id=service_id
))
)
if not template:
return jsonify(
result="error",
@@ -147,6 +147,26 @@ def send_notification(notification_type):
}
), 404
template_object = Template({'content': template.content}, notification.get('personalisation', {}))
if template_object.missing_data:
return jsonify(
result="error",
message={
'template': ['Missing personalisation: {}'.format(
", ".join(template_object.missing_data)
)]
}
), 400
if template_object.additional_data:
return jsonify(
result="error",
message={
'template': ['Personalisation not needed for template: {}'.format(
", ".join(template_object.additional_data)
)]
}
), 400
service = services_dao.dao_fetch_service_by_id(api_user['client'])
notification_id = create_uuid()