First commit

This commit is contained in:
Ken Tsang
2017-03-28 10:41:25 +01:00
parent f5da3574b5
commit 89e244ccd2
10 changed files with 361 additions and 102 deletions

View File

@@ -1,8 +1,5 @@
import uuid
from flask import jsonify, request
from flask import jsonify
from jsonschema.exceptions import ValidationError
from werkzeug.exceptions import abort
from app import api_user
from app.dao import templates_dao

View File

@@ -0,0 +1,7 @@
from flask import Blueprint
from app.v2.errors import register_errors
v2_templates_blueprint = Blueprint("v2_templates", __name__, url_prefix='/v2/templates')
register_errors(v2_templates_blueprint)

View File

@@ -0,0 +1,38 @@
import json
from flask import jsonify, request, current_app, url_for
from jsonschema.exceptions import ValidationError
from app import api_user
from app.dao import templates_dao
from app.schema_validation import validate
from app.v2.templates import v2_templates_blueprint
from app.v2.templates.templates_schemas import get_all_template_request
@v2_templates_blueprint.route("/", methods=['GET'])
def get_templates():
_data = request.args.to_dict()
data = validate(_data, get_all_template_request)
templates = templates_dao.dao_get_all_templates_for_service(
api_user.service_id,
older_than=data.get('older_than'),
page_size=current_app.config.get('API_PAGE_SIZE'))
def _build_links(templates):
_links = {
'current': url_for(".get_templates", _external=True, **data),
}
if len(templates):
next_query_params = dict(data, older_than=templates[-1].id)
_links['next'] = url_for(".get_templates", _external=True, **next_query_params)
return _links
return jsonify(
templates=[template.serialize() for template in templates],
links=_build_links(templates)
), 200

View File

@@ -0,0 +1,49 @@
from app.models import TEMPLATE_TYPES
from app.schema_validation.definitions import uuid
from app.v2.template_schema import template
get_all_template_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "request schema for parameters allowed when getting all templates",
"type": "object",
"properties": {
"type": {"enum": TEMPLATE_TYPES},
"older_than": uuid
},
"additionalProperties": False,
}
get_all_template_response = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "GET response schema when getting all templates",
"type": "object",
"properties": {
"links": {
"type": "object",
"properties": {
"current": {
"type": "string",
"format": "uri"
},
"next": {
"type": "string",
"format": "uri"
}
},
"additionalProperties": False,
"required": ["current"],
},
"templates": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/definitions/template"
}
}
},
"required": ["links", "templates"],
"definitions": {
"template": template
}
}