将参数添加到Flask装饰器

Nic*_*s L 0 python flask

我使用Flask的片段创建了一个用于处理HTTP Basic Auth的装饰器:

def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            return authenticate()
        return f(*args, **kwargs)
    return decorated
Run Code Online (Sandbox Code Playgroud)

此装饰器不允许传递参数.我想改变一下,我怎么能这样做?我一直在检查有关装饰器的其他SO问题,但我仍然无法使用它.

kin*_*all 5

使用装饰器时,在函数(或类)定义之前的行上指定的对象必须是一个函数(或其他可调用对象),它接受一个函数(或类)并返回一个函数(或类).

这意味着如果要将参数传递给装饰器,则表示以下行:

@requires_auth("admin")
Run Code Online (Sandbox Code Playgroud)

必须表示函数(或其他可调用对象).换句话说,requires_auth("admin")执行,并且此调用的结果用于修饰后面的函数(或类).

也就是说,接受参数的函数必须返回一个实际进行装饰的函数!

这样的话,然后:

def requires_auth(user):

    def decorator(f):
        # the actual decorator, which may use the variable "user"
        # (basically everything you've written, including the wrapper)

    return decorator
Run Code Online (Sandbox Code Playgroud)

您也可以以类的形式编写它.类的__init__方法将接受参数,其__call__方法将接受要装饰的函数/类.也就是说,requires_auth("admin")创建一个对象的实例,然后实例传递正在装饰的函数并完成工作.

class requires_auth:

    def __init__(self, user):
         self.user = user

    def __call__(self, f):
         # your decorator as above, referring to "self.user" for the arg
Run Code Online (Sandbox Code Playgroud)