烧瓶 - 自定义路线装饰[重复]

问题描述 投票:0回答:1

如何组合带参数的装饰器?

目前我有:

@app.route('/create/<id>', methods=['POST'])
@requiresAdmin
def create_room(id):
    pass

@app.route('/join/<id>')
@requiresAdmin
def join_room(id):
    pass

我想拥有:

@adminRoute('/create/<id>', methods=['POST'])
def create_room(id):
    pass

@adminRoute('/join/<id>')
def join_room(id):
    pass
python flask decorator python-decorators
1个回答
0
投票
def adminRoute(*route_args, **route_kwargs):

    def outer(action_function):
        @app.route(*route_args, **route_kwargs)
        @requiresAdmin
        @functools.wraps(action_function)
        def inner(*f_args, **f_kwargs):
            return action_function(f_args, f_kwargs)
        return inner

    return outer
© www.soinside.com 2019 - 2024. All rights reserved.