Move param_required into the decorators module

This commit is contained in:
Joseph Schorr 2017-07-20 11:41:19 -04:00
parent 98e2ccf74d
commit 5d69fc2aa3
3 changed files with 20 additions and 23 deletions

View file

@ -24,24 +24,6 @@ from _init import __version__
logger = logging.getLogger(__name__)
route_data = None
def truthy_param(param):
return param not in {False, 'false', 'False', '0', 'FALSE', '', 'null'}
def param_required(param_name, allow_body=False):
def wrapper(wrapped):
@wraps(wrapped)
def decorated(*args, **kwargs):
if param_name not in request.args:
if not allow_body or param_name not in request.values:
abort(make_response('Required param: %s' % param_name, 400))
return wrapped(*args, **kwargs)
return decorated
return wrapper
def common_login(db_user, permanent_session=True):
if login_user(LoginWrappedDBUser(db_user.uuid, db_user)):
@ -85,8 +67,6 @@ def _list_files(path, extension):
def render_page_template(name, route_data=None, **kwargs):
debugging = app.config.get('DEBUGGING', False)
library_styles = []
main_styles = []
library_scripts = []

View file

@ -1,7 +1,7 @@
""" Various decorators for endpoint and API handlers. """
from functools import wraps
from flask import abort
from flask import abort, request, make_response
import features
@ -35,6 +35,22 @@ def parse_repository_name(include_tag=False,
return inner
def param_required(param_name, allow_body=False):
""" Marks a route as requiring a parameter with the given name to exist in the request's arguments
or (if allow_body=True) in its body values. If the parameter is not present, the request will
fail with a 400.
"""
def wrapper(wrapped):
@wraps(wrapped)
def decorated(*args, **kwargs):
if param_name not in request.args:
if not allow_body or param_name not in request.values:
abort(make_response('Required param: %s' % param_name, 400))
return wrapped(*args, **kwargs)
return decorated
return wrapper
def anon_allowed(func):
""" Marks a method to allow anonymous access where it would otherwise be disallowed. """
func.__anon_allowed = True

View file

@ -27,9 +27,10 @@ from buildtrigger.triggerutil import TriggerProviderException
from data import model
from data.database import db
from endpoints.api.discovery import swagger_route_data
from endpoints.common import (common_login, render_page_template, param_required)
from endpoints.common import common_login, render_page_template
from endpoints.csrf import csrf_protect, generate_csrf_token, verify_csrf
from endpoints.decorators import anon_protect, anon_allowed, route_show_if, parse_repository_name
from endpoints.decorators import (anon_protect, anon_allowed, route_show_if, parse_repository_name,
param_required)
from health.healthcheck import get_healthchecker
from util.cache import no_cache
from util.headers import parse_basic_auth