Just add another shell to capture the json parameter:
def requireAuthentication(json=False): def decorator(fn): def wrapper(**kwargs): # Is user logged on? if "user" in request.session: return fn(**kwargs) # No, return error if json: return { "exception": "NotAuthorized", "error" : "You are not authorized, please log on" } redirect('/login?url={0}{1}'.format(request.path, ("?" + request.query_string if request.query_string else ''))) return wrapper return decorator
I renamed your original requireAuthentication function to decorator (because this is what this function did, it decorated fn ) and renamed the old decorator to wrapper , the usual convention.
Regardless of what you put after @ , this is an expression that first evaluates the actual function of the decorator. @helpers.requireAuthentication() means you want to call requireAuthentication , and it returns as the actual decorator for the function to which the @ string applies.
source share