Merge pull request #290 from alphagov/template_versions_api

Get all versions for a template endpoint added.
This commit is contained in:
NIcholas Staples
2016-05-09 16:05:20 +01:00
3 changed files with 45 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
import uuid import uuid
from app import db from app import db
from app.models import (Template, Service) from app.models import (Template, Service)
from sqlalchemy import asc from sqlalchemy import (asc, desc)
from app.dao.dao_utils import ( from app.dao.dao_utils import (
transactional, transactional,
@@ -45,3 +45,8 @@ def dao_get_all_templates_for_service(service_id):
).order_by( ).order_by(
asc(Template.updated_at), asc(Template.created_at) asc(Template.updated_at), asc(Template.created_at)
).all() ).all()
def dao_get_template_versions(service_id, template_id):
history_model = Template.get_history_model()
return history_model.query.filter_by(service_id=service_id, id=template_id).order_by(desc(history_model.version))

View File

@@ -11,7 +11,8 @@ from app.dao.templates_dao import (
dao_update_template, dao_update_template,
dao_create_template, dao_create_template,
dao_get_template_by_id_and_service_id, dao_get_template_by_id_and_service_id,
dao_get_all_templates_for_service dao_get_all_templates_for_service,
dao_get_template_versions
) )
from notifications_utils.template import Template from notifications_utils.template import Template
from app.dao.services_dao import dao_fetch_service_by_id from app.dao.services_dao import dao_fetch_service_by_id
@@ -105,7 +106,16 @@ def get_template_version(service_id, template_id, version):
) )
data, errors = template_history_schema.dump(fetched_template) data, errors = template_history_schema.dump(fetched_template)
if errors: if errors:
return json_resp(result=error, message=errors), 400 return json_resp(result='error', message=errors), 400
return jsonify(data=data)
@template.route('/<uuid:template_id>/version')
def get_template_versions(service_id, template_id):
fetched_templates = dao_get_template_versions(service_id, template_id)
data, errors = template_history_schema.dump(fetched_templates, many=True)
if errors:
return json_resp(result='error', message=errors), 400
return jsonify(data=data) return jsonify(data=data)

View File

@@ -62,3 +62,30 @@ def test_404_missing_template_version(notify_api, sample_template):
headers=[('Content-Type', 'application/json'), auth_header] headers=[('Content-Type', 'application/json'), auth_header]
) )
assert resp.status_code == 404 assert resp.status_code == 404
def test_all_versions_of_template(notify_api, sample_template):
with notify_api.test_request_context():
with notify_api.test_client() as client:
old_content = sample_template.content
newer_content = "Newer content"
newest_content = "Newest content"
sample_template.content = newer_content
dao_update_template(sample_template)
sample_template.content = newest_content
dao_update_template(sample_template)
auth_header = create_authorization_header()
endpoint = url_for(
'template.get_template_versions',
service_id=sample_template.service.id,
template_id=sample_template.id
)
resp = client.get(
endpoint,
headers=[('Content-Type', 'application/json'), auth_header]
)
json_resp = json.loads(resp.get_data(as_text=True))
assert len(json_resp['data']) == 3
assert json_resp['data'][0]['content'] == newest_content
assert json_resp['data'][1]['content'] == newer_content
assert json_resp['data'][2]['content'] == old_content