5

In Flask I'm using 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 prefer to have a declaration that groups all of them with a single decorator:

@nice_decorator("/first")
def first_page:
    ....

@nice_decorator("/second")
def second_page:
    ....

I tried to follow the answer at Can I combine two decorators into a single one in Python? but I cannot make it working:

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:
    ....

because of 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 possibility to specify the route parameter:

new_decorator2 = composed(app.route("/first"),
                          auth.login_required,
                          crossdomain(origin="*"),
                          nocache)

Is it possible to define a single decorator with parameters?

Alex Poca
  • 2,406
  • 4
  • 25
  • 47
  • 2
    `nice_decorator` does not returns nothing, so returns `None` and `None` is not callable. Add `return` before `composed(app.route...)` – Arount Oct 04 '17 at 12:10

1 Answers1

5

You're not defining the composition correctly. You need to change the definition of nice_decorator to something like this:

def nice_decorator(route):
    return composed(
        app.route(route),
        auth.login_required,
        crossdomain(origin="*"),
        nocache
    )

Your previous version never actually returned anything. Python isn't like Ruby or Lisp where the last expression is the return value.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
Edward Minnix
  • 2,889
  • 1
  • 13
  • 26