Merge pull request #361 from alphagov/do-not-create-new-tempalte-version

Check for an update before saving template
This commit is contained in:
Rebecca Law
2016-06-02 10:11:55 +01:00
3 changed files with 37 additions and 30 deletions

View File

@@ -5,7 +5,6 @@ from flask import (
current_app
)
import bleach
from sqlalchemy.exc import IntegrityError
from app.dao.templates_dao import (
dao_update_template,
@@ -59,14 +58,18 @@ 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())
current_data.update(request.get_json())
current_data['content'] = _strip_html(current_data['content'])
updated_template = dict(template_schema.dump(fetched_template).data.items())
updated_template.update(request.get_json())
updated_template['content'] = _strip_html(updated_template['content'])
# Check if there is a change to make.
if _template_has_not_changed(current_data, updated_template):
return jsonify(data=updated_template), 200
update_dict, errors = template_schema.load(current_data)
update_dict, errors = template_schema.load(updated_template)
if errors:
return jsonify(result="error", message=errors), 400
over_limit, json_resp = _content_count_greater_than_limit(
current_data['content'],
updated_template['content'],
fetched_template.template_type)
if over_limit:
return json_resp, 400
@@ -115,3 +118,10 @@ def get_template_versions(service_id, template_id):
def _strip_html(content):
return bleach.clean(content, tags=[], strip=True)
def _template_has_not_changed(current_data, updated_template):
return all(
current_data[key] == updated_template[key]
for key in ('name', 'content', 'subject', 'archived')
)