烧瓶 - 自定义路线装饰

时间:2017-12-22 19:10:32

标签: python flask decorator python-decorators

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

目前我有:

@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

1 个答案:

答案 0 :(得分: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