2016-02-17 17:04:50 +00:00
|
|
|
from flask import (
|
|
|
|
|
Blueprint,
|
2016-02-22 12:55:18 +00:00
|
|
|
jsonify,
|
|
|
|
|
request,
|
|
|
|
|
current_app
|
2016-02-17 17:04:50 +00:00
|
|
|
)
|
|
|
|
|
|
2016-02-22 12:55:18 +00:00
|
|
|
from app.dao.templates_dao import (
|
|
|
|
|
dao_update_template,
|
|
|
|
|
dao_create_template,
|
|
|
|
|
dao_get_template_by_id_and_service_id,
|
2016-05-09 15:59:34 +01:00
|
|
|
dao_get_all_templates_for_service,
|
|
|
|
|
dao_get_template_versions
|
2016-02-22 12:55:18 +00:00
|
|
|
)
|
2016-12-09 15:56:25 +00:00
|
|
|
from notifications_utils.template import SMSMessageTemplate
|
2016-04-29 10:36:59 +01:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
2016-06-30 15:41:51 +01:00
|
|
|
from app.models import SMS_TYPE
|
2016-05-06 15:42:43 +01:00
|
|
|
from app.schemas import (template_schema, template_history_schema)
|
2016-01-13 11:04:13 +00:00
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
template_blueprint = Blueprint('template', __name__, url_prefix='/service/<uuid:service_id>/template')
|
2016-01-13 11:04:13 +00:00
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
from app.errors import (
|
|
|
|
|
register_errors,
|
|
|
|
|
InvalidRequest
|
|
|
|
|
)
|
2016-12-09 15:56:25 +00:00
|
|
|
from app.utils import get_template_instance
|
2016-02-22 12:55:18 +00:00
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
register_errors(template_blueprint)
|
2016-02-17 17:04:50 +00:00
|
|
|
|
2016-01-13 11:04:13 +00:00
|
|
|
|
2016-04-29 10:36:59 +01:00
|
|
|
def _content_count_greater_than_limit(content, template_type):
|
2016-12-09 15:56:25 +00:00
|
|
|
if template_type != SMS_TYPE:
|
|
|
|
|
return False
|
|
|
|
|
template = SMSMessageTemplate({'content': content, 'template_type': template_type})
|
|
|
|
|
return template.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
2016-04-29 10:36:59 +01:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('', methods=['POST'])
|
2016-02-22 12:55:18 +00:00
|
|
|
def create_template(service_id):
|
|
|
|
|
fetched_service = dao_fetch_service_by_id(service_id=service_id)
|
2016-06-14 15:07:23 +01:00
|
|
|
new_template = template_schema.load(request.get_json()).data
|
2016-02-22 12:55:18 +00:00
|
|
|
new_template.service = fetched_service
|
2016-06-14 15:07:23 +01:00
|
|
|
over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type)
|
2016-04-29 10:36:59 +01:00
|
|
|
if over_limit:
|
2016-06-14 15:07:23 +01:00
|
|
|
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
|
|
|
|
message = 'Content has a character count greater than the limit of {}'.format(char_count_limit)
|
|
|
|
|
errors = {'content': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
2016-05-18 10:00:09 +01:00
|
|
|
dao_create_template(new_template)
|
2016-02-22 12:55:18 +00:00
|
|
|
return jsonify(data=template_schema.dump(new_template).data), 201
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>', methods=['POST'])
|
2016-02-22 12:55:18 +00:00
|
|
|
def update_template(service_id, template_id):
|
|
|
|
|
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
|
|
|
|
|
|
|
|
|
|
current_data = dict(template_schema.dump(fetched_template).data.items())
|
2016-06-01 12:19:59 +01:00
|
|
|
updated_template = dict(template_schema.dump(fetched_template).data.items())
|
|
|
|
|
updated_template.update(request.get_json())
|
2016-06-01 10:53:03 +01:00
|
|
|
# Check if there is a change to make.
|
2016-06-01 12:19:59 +01:00
|
|
|
if _template_has_not_changed(current_data, updated_template):
|
|
|
|
|
return jsonify(data=updated_template), 200
|
2016-06-01 10:53:03 +01:00
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
update_dict = template_schema.load(updated_template).data
|
|
|
|
|
over_limit = _content_count_greater_than_limit(updated_template['content'], fetched_template.template_type)
|
2016-04-29 10:36:59 +01:00
|
|
|
if over_limit:
|
2016-06-14 15:07:23 +01:00
|
|
|
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
|
|
|
|
message = 'Content has a character count greater than the limit of {}'.format(char_count_limit)
|
|
|
|
|
errors = {'content': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-02-22 12:55:18 +00:00
|
|
|
dao_update_template(update_dict)
|
|
|
|
|
return jsonify(data=template_schema.dump(update_dict).data), 200
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('', methods=['GET'])
|
2016-02-22 12:55:18 +00:00
|
|
|
def get_all_templates_for_service(service_id):
|
|
|
|
|
templates = dao_get_all_templates_for_service(service_id=service_id)
|
2016-06-14 15:07:23 +01:00
|
|
|
data = template_schema.dump(templates, many=True).data
|
2016-01-13 11:04:13 +00:00
|
|
|
return jsonify(data=data)
|
2016-02-22 12:55:18 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>', methods=['GET'])
|
2016-02-22 12:55:18 +00:00
|
|
|
def get_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)
|
2016-06-14 15:07:23 +01:00
|
|
|
data = template_schema.dump(fetched_template).data
|
2016-03-11 12:39:55 +00:00
|
|
|
return jsonify(data=data)
|
2016-03-04 07:03:15 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>/preview', methods=['GET'])
|
2016-06-16 13:51:20 +01:00
|
|
|
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
|
2016-12-09 15:56:25 +00:00
|
|
|
|
|
|
|
|
template_object = get_template_instance(data, values=request.args.to_dict())
|
2016-06-16 13:51:20 +01:00
|
|
|
|
|
|
|
|
if template_object.missing_data:
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
{'template': [
|
|
|
|
|
'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
|
|
|
|
|
]}, status_code=400
|
|
|
|
|
)
|
|
|
|
|
|
2016-12-09 15:56:25 +00:00
|
|
|
data['subject'], data['content'] = template_object.subject, str(template_object)
|
2016-06-16 13:51:20 +01:00
|
|
|
|
2016-06-17 12:57:43 +01:00
|
|
|
return jsonify(data)
|
2016-06-16 13:51:20 +01:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>/version/<int:version>')
|
2016-05-06 15:42:43 +01:00
|
|
|
def get_template_version(service_id, template_id, version):
|
2016-06-14 15:07:23 +01:00
|
|
|
data = template_history_schema.dump(
|
2016-05-10 14:55:59 +01:00
|
|
|
dao_get_template_by_id_and_service_id(
|
|
|
|
|
template_id=template_id,
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
version=version
|
|
|
|
|
)
|
2016-06-14 15:07:23 +01:00
|
|
|
).data
|
2016-05-09 15:59:34 +01:00
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@template_blueprint.route('/<uuid:template_id>/versions')
|
2016-05-09 15:59:34 +01:00
|
|
|
def get_template_versions(service_id, template_id):
|
2016-06-14 15:07:23 +01:00
|
|
|
data = template_history_schema.dump(
|
2016-05-13 16:25:05 +01:00
|
|
|
dao_get_template_versions(service_id=service_id, template_id=template_id),
|
2016-05-10 14:55:59 +01:00
|
|
|
many=True
|
2016-06-14 15:07:23 +01:00
|
|
|
).data
|
2016-05-06 15:42:43 +01:00
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
2016-06-01 12:19:59 +01:00
|
|
|
def _template_has_not_changed(current_data, updated_template):
|
2016-06-01 13:55:04 +01:00
|
|
|
return all(
|
|
|
|
|
current_data[key] == updated_template[key]
|
2017-01-17 15:48:51 +00:00
|
|
|
for key in ('name', 'content', 'subject', 'archived', 'process_type')
|
2016-06-01 13:55:04 +01:00
|
|
|
)
|