Port some suconfig/superuser endpoints, with data.model references
This commit is contained in:
parent
841053f878
commit
acf242f241
5 changed files with 379 additions and 13 deletions
|
@ -1,6 +1,6 @@
|
|||
import logging
|
||||
|
||||
from flask import Blueprint
|
||||
from flask import Blueprint, request
|
||||
from flask_restful import Resource, Api
|
||||
from flask_restful.utils.cors import crossdomain
|
||||
from email.utils import formatdate
|
||||
|
@ -9,7 +9,7 @@ from functools import partial, wraps
|
|||
from jsonschema import validate, ValidationError
|
||||
|
||||
from config_app.c_app import app
|
||||
from config_app.config_endpoints.exception import InvalidResponse
|
||||
from config_app.config_endpoints.exception import InvalidResponse, InvalidRequest
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
api_bp = Blueprint('api', __name__)
|
||||
|
@ -128,6 +128,26 @@ def define_json_response(schema_name):
|
|||
return wrapper
|
||||
|
||||
|
||||
def validate_json_request(schema_name, optional=False):
|
||||
def wrapper(func):
|
||||
@add_method_metadata('request_schema', schema_name)
|
||||
@wraps(func)
|
||||
def wrapped(self, *args, **kwargs):
|
||||
schema = self.schemas[schema_name]
|
||||
try:
|
||||
json_data = request.get_json()
|
||||
if json_data is None:
|
||||
if not optional:
|
||||
raise InvalidRequest('Missing JSON body')
|
||||
else:
|
||||
validate(json_data, schema)
|
||||
return func(self, *args, **kwargs)
|
||||
except ValidationError as ex:
|
||||
raise InvalidRequest(ex.message)
|
||||
return wrapped
|
||||
return wrapper
|
||||
|
||||
|
||||
nickname = partial(add_method_metadata, 'nickname')
|
||||
|
||||
|
||||
|
|
Reference in a new issue