mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 06:21:50 -05:00
First commit
This commit is contained in:
38
app/v2/templates/get_templates.py
Normal file
38
app/v2/templates/get_templates.py
Normal 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
|
||||
Reference in New Issue
Block a user