这个问题在这里已有答案:
如何组合带参数的装饰器?
目前我有:
@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
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