Reputation: 509
I wrote a login decorator, that should guarantee, that the user is properly logged in before executing another function. The problem is that, although the the decorator is working as expected, the function wrapped function is never executed. My structure is like the following:
#This is my decorator
login_required(func):
def func_wrapper(*args, **kwargs):
#do some operations
return True #or False
return func_wrapper
@login_required
def do_something(param1, param2, param3):
print("This print is not executing")
#continue the work that should be done after
#the user is authorized by the login decorator
I already tried to remove the return True/False inside the decorator, but it didn't change anything.
Upvotes: 3
Views: 5209
Reputation: 155438
Your wrapper function never calls func
. If you want func
to be called when the wrapper is invoked, call it, e.g.:
def login_required(func):
def func_wrapper(*args, **kwargs):
#do some operations to determine if user logged in and allowed to do op
if allowed:
return func(*args, **kwargs)
else:
# Raise exception, return "auth required" sentinel value, whatever
return func_wrapper
Your code is assuming that returning a boolean would somehow determine whether the wrapped func
gets invoked, but that's not how decorators work. They replace the original function with whatever the decorator returns; if you return a new function, that new function is responsible for invoking the original function (if desired), no one else will do it for you.
Upvotes: 8