Merge pull request #865 from alphagov/add-v2-template-preview

Add v2 template preview endpoint and schema
This commit is contained in:
kentsanggds
2017-03-28 18:40:32 +01:00
committed by GitHub
9 changed files with 258 additions and 39 deletions

View File

@@ -2,6 +2,6 @@ from flask import Blueprint
from app.v2.errors import register_errors
template_blueprint = Blueprint("v2_template", __name__, url_prefix='/v2/template')
v2_template_blueprint = Blueprint("v2_template", __name__, url_prefix='/v2/template')
register_errors(template_blueprint)
register_errors(v2_template_blueprint)

View File

@@ -7,22 +7,18 @@ from werkzeug.exceptions import abort
from app import api_user
from app.dao import templates_dao
from app.schema_validation import validate
from app.v2.template import template_blueprint
from app.v2.template import v2_template_blueprint
from app.v2.template.template_schemas import get_template_by_id_request
@template_blueprint.route("/<template_id>", methods=['GET'])
@template_blueprint.route("/<template_id>/version/<int:version>", methods=['GET'])
@v2_template_blueprint.route("/<template_id>", methods=['GET'])
@v2_template_blueprint.route("/<template_id>/version/<int:version>", methods=['GET'])
def get_template_by_id(template_id, version=None):
try:
_data = {}
_data['id'] = template_id
if version:
_data['version'] = version
_data = {'id': template_id}
if version:
_data['version'] = version
data = validate(_data, get_template_by_id_request)
except ValueError or AttributeError:
abort(404)
data = validate(_data, get_template_by_id_request)
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id, api_user.service_id, data.get('version'))

View File

@@ -0,0 +1,41 @@
import uuid
from flask import jsonify, request
from jsonschema.exceptions import ValidationError
from werkzeug.exceptions import abort
from app import api_user
from app.dao import templates_dao
from app.schema_validation import validate
from app.utils import get_template_instance
from app.v2.errors import BadRequestError
from app.v2.template import v2_template_blueprint
from app.v2.template.template_schemas import post_template_preview_request, create_post_template_preview_response
@v2_template_blueprint.route("/<template_id>/preview", methods=['POST'])
def post_template_preview(template_id):
_data = request.get_json()
_data['id'] = template_id
data = validate(_data, post_template_preview_request)
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id, api_user.service_id)
template_object = get_template_instance(
template.__dict__, values=data.get('personalisation'))
check_placeholders(template_object)
resp = create_post_template_preview_response(template=template,
body=str(template_object),
url_root=request.url_root)
return jsonify(resp), 200
def check_placeholders(template_object):
if template_object.missing_data:
message = 'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
raise BadRequestError(message=message, fields=[{'template': message}])

View File

@@ -1,5 +1,5 @@
from app.models import TEMPLATE_TYPES
from app.schema_validation.definitions import uuid
from app.schema_validation.definitions import uuid, personalisation
get_template_by_id_request = {
@@ -39,3 +39,41 @@ get_template_by_id_response = {
},
"required": ["id", "type", "created_at", "updated_at", "version", "created_by", "body"]
}
post_template_preview_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST template schema",
"type": "object",
"title": "POST v2/template/{id}/preview",
"properties": {
"id": uuid,
"personalisation": personalisation
},
"required": ["id", "personalisation"]
}
post_template_preview_response = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST template preview schema response",
"type": "object",
"title": "reponse v2/template/{id}/preview",
"properties": {
"id": uuid,
"type": {"enum": TEMPLATE_TYPES},
"version": {"type": "integer"},
"body": {"type": "string"},
"subject": {"type": ["string", "null"]}
},
"required": ["id", "type", "version", "body"]
}
def create_post_template_preview_response(template, body, url_root):
return {
"id": template.id,
"type": template.template_type,
"version": template.version,
"body": body,
"subject": template.subject,
"uri": "{}v2/template/{}/preview".format(url_root, template.id)
}