0

I have a flask application and I have created a custom decorator that checks some header information for permission on controllers.py.

app.py

app = Flask(__name__)

if __name__ == "__main__":
   app.run(host='0.0.0.0', port=80)

controllers.py

@route('/')
@custom_decorator
def get():
   pass


@route('/id')
def get_id():
   pass

On this case, see that I forgot to use my decorator on the second method. There is any way I can guarantee that If I forget my decorator a exception would be throw? In the case of a new dev in the team forget to use the decorator or something like that.
Is there any way I can do something with the request before it hit the controllers?

RonanFelipe
  • 590
  • 1
  • 9
  • 23

1 Answers1

1

You could merge the decorators and check if the name of some function is passed as an argument.

def composed(*decs):
    def deco(f):
        is_decorated = False
        for dec in decs:
            if 'custom_decorator' in dec.__qualname__.split('.'):
                is_decorated = True
            f = dec(f)
        if not is_decorated:
            raise SomeError
        return f
    return deco


@composed(blueprint.route('/'), custom_decorator)
def get():
   pass

For further reading you can check this question about how to merge decorators in python.

Robin Uphoff
  • 625
  • 1
  • 9
  • 19