In Flask, I use a set of decorators for each route, but the code is ugly:
@app.route("/first")
@auth.login_required
@crossdomain(origin='*')
@nocache
def first_page:
....
@app.route("/second")
@auth.login_required
@crossdomain(origin='*')
@nocache
def second_page:
....
I would rather have an ad that groups all of them with a single decorator:
@nice_decorator("/first")
def first_page:
....
@nice_decorator("/second")
def second_page:
....
I tried to execute the answer to Can I combine two decorators into one in Python? but I can't get it to work:
def composed(*decs):
def deco(f):
for dec in reversed(decs):
f = dec(f)
return f
return deco
def nice_decorator(route):
composed(app.route(route),
auth.login_required,
crossdomain(origin="*"),
nocache)
@nice_decorator("/first")
def first_page:
....
due to this error that I don't understand:
@nice_decorator("/first")
TypeError: 'NoneType' object is not callable
Following one of the comments, I defined it with another form that works, but without the ability to specify a route parameter:
new_decorator2 = composed(app.route("/first"),
auth.login_required,
crossdomain(origin="*"),
nocache)
Is it possible to define one decorator with parameters?
source
share