"""Handler Utils. ============= These are general purpose functions for use with all route handlers. (Taken from https://github.com/theorchard/ows-project-manager/) """ from functools import wraps from flask import g from flask import request from territories import response from territories.validation import json_schema def validate_header(validator): """Validate HTTP request headers. Args: validator (Draft3Validator): the JSON Draft3 Schema to validate against. Returns: callable: the wrapped function. """ def decorator(function): @wraps(function) def _validate_header(*args, **kwargs): """Validate the request headers against a validator. Args: validator (Draft3Validator): the JSON Draft3 Schema to validate against. Returns: Response: error HTTP response, or continue to the calling function if successful. """ validation_response = json_schema.validate( dict(request.headers), validator) if not validation_response: g.log.info(validation_response.message) return response.flaskify(validation_response) return function(*args, **kwargs) return _validate_header return decorator def add_pagination(records, page_offset=0, page_limit=1000): """Wrap pagination information around records. Args: records (list): list of report dictionaries. page_offset (int): page offset. page_limit (int): total number of records per page. Return: dict: dictionary containing a list of records and pagination information. """ return { 'items': records[ page_offset * page_limit: page_offset * page_limit + page_limit ], 'pagination': { 'type': 'standard', 'offset': page_offset, 'limit': page_limit, 'total_records': len(records) } }