Files
notifications-api/app/template/rest.py

37 lines
1.1 KiB
Python
Raw Normal View History

from flask import (
Blueprint,
jsonify
)
2016-01-13 11:04:13 +00:00
from sqlalchemy.exc import DataError
from sqlalchemy.orm.exc import NoResultFound
2016-01-14 16:13:27 +00:00
2016-01-13 11:04:13 +00:00
from app.dao.templates_dao import get_model_templates
from app.schemas import (template_schema, templates_schema)
2016-01-14 16:13:27 +00:00
template = Blueprint('template', __name__)
2016-01-13 11:04:13 +00:00
from app.errors import register_errors
register_errors(template)
2016-01-13 11:04:13 +00:00
# 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'])
2016-01-13 11:04:13 +00:00
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)
if errors:
return jsonify(result="error", message=str(errors))
return jsonify(data=data)