Add a ‘preview template’ endpoint

There’s a need for users of the API to be able to take advantage of
Notify’s template rendering.

For example, there’s a service that’s building a case management system.
Their users are sending emails on a case-by-case basis. Before they
send an email, it’s ressuring to double check that the right data is
being inserted, that the right template is being used, etc.

This commit:
- adds a separate endpoint for previewing a template, with
  personalisation taken from the get parameters of the request
- beefs up the tests around getting a template

Not part of this pull request:
- making this enpoint publicly accessible
This commit is contained in:
Chris Hill-Scott
2016-06-16 13:51:20 +01:00
parent f7ceaf9009
commit cf91ce57fc
2 changed files with 103 additions and 0 deletions

View File

@@ -86,6 +86,31 @@ def get_template_by_id_and_service_id(service_id, template_id):
return jsonify(data=data)
@template.route('/<uuid:template_id>/preview', methods=['GET'])
def preview_template_by_id_and_service_id(service_id, template_id):
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
data = template_schema.dump(fetched_template).data
template_object = Template(data, values=request.args.to_dict())
if template_object.missing_data:
raise InvalidRequest(
{'template': [
'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
]}, status_code=400
)
if template_object.additional_data:
raise InvalidRequest(
{'template': [
'Personalisation not needed for template: {}'.format(", ".join(template_object.additional_data))
]}, status_code=400
)
data['subject'], data['content'] = template_object.replaced_subject, template_object.replaced
return jsonify(data=data)
@template.route('/<uuid:template_id>/version/<int:version>')
def get_template_version(service_id, template_id, version):
data = template_history_schema.dump(