Updates to template endpoints:

- moved into templates rest class
- updated dao
- removed delete methods
- constraint on subject line
This commit is contained in:
Martyn Inglis
2016-02-22 12:55:18 +00:00
parent 443691402f
commit 9bb95a53ec
7 changed files with 540 additions and 383 deletions

View File

@@ -1,36 +1,84 @@
from flask import (
Blueprint,
jsonify
jsonify,
request,
current_app
)
from sqlalchemy.exc import IntegrityError
from app.dao.templates_dao import (
dao_update_template,
dao_create_template,
dao_get_template_by_id_and_service_id,
dao_get_all_templates_for_service
)
from app.dao.services_dao import (
dao_fetch_service_by_id
)
from app.schemas import (
template_schema,
templates_schema,
)
from sqlalchemy.exc import DataError
from sqlalchemy.orm.exc import NoResultFound
from app.dao.templates_dao import get_model_templates
from app.schemas import (template_schema, templates_schema)
template = Blueprint('template', __name__)
template = Blueprint('template', __name__, url_prefix='/service/<service_id>/template')
from app.errors import register_errors
register_errors(template)
# I am going to keep these for admin like operations
# Permissions should restrict who can access this endpoint
# TODO auth to be added.
@template.route('/<int:template_id>', methods=['GET'])
@template.route('', methods=['GET'])
def get_template(template_id=None):
try:
templates = get_model_templates(template_id=template_id)
except DataError:
return jsonify(result="error", message="Invalid template id"), 400
except NoResultFound:
return jsonify(result="error", message="Template not found"), 404
if isinstance(templates, list):
data, errors = templates_schema.dump(templates)
else:
data, errors = template_schema.dump(templates)
@template.route('', methods=['POST'])
def create_template(service_id):
fetched_service = dao_fetch_service_by_id(service_id=service_id)
if not fetched_service:
return jsonify(result="error", message="Service not found"), 404
new_template, errors = template_schema.load(request.get_json())
if errors:
return jsonify(result="error", message=str(errors))
return jsonify(result="error", message=errors), 400
new_template.service = fetched_service
try:
dao_create_template(new_template)
except IntegrityError as ex:
current_app.logger.debug(ex)
message = "Failed to create template"
if "templates_subject_key" in str(ex):
message = 'Duplicate template subject'
return jsonify(result="error", message=message), 400
return jsonify(result="error", message=message), 500
return jsonify(data=template_schema.dump(new_template).data), 201
@template.route('/<int:template_id>', methods=['POST'])
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)
if not fetched_template:
return jsonify(result="error", message="Template not found"), 404
current_data = dict(template_schema.dump(fetched_template).data.items())
current_data.update(request.get_json())
update_dict, errors = template_schema.load(current_data)
if errors:
return jsonify(result="error", message=errors), 400
dao_update_template(update_dict)
return jsonify(data=template_schema.dump(update_dict).data), 200
@template.route('', methods=['GET'])
def get_all_templates_for_service(service_id):
templates = dao_get_all_templates_for_service(service_id=service_id)
data, errors = templates_schema.dump(templates)
return jsonify(data=data)
@template.route('/<int:template_id>', methods=['GET'])
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)
if fetched_template:
data, errors = template_schema.dump(fetched_template)
return jsonify(data=data)
else:
return jsonify(result="error", message="Template not found"), 404