3 回答

TA贡献1804条经验 获得超8个赞
只需添加另一个包装即可捕获json参数:
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
我已将您的原始requireAuthentication函数重命名为decorator(因为该函数所做的是它的修饰fn),并将旧函数重命名decorator为wrapper,这是通常的约定。
放在表达式之后的@任何内容,都首先求值以找到实际的装饰器函数。@helpers.requireAuthentication()表示您要调用requireAuthentication,它的返回值将用作该@行所应用功能的实际装饰器。
添加回答
举报