I’ve made this decorator, which results in an infinite redirect loop.
The problem is this:
args[0].redirect(users.create_login_url(args[0].request.path))
It appears to be a perfectly valid URL. So why wouldn’t it properly redirect?
def admin_only(handler, *args): def redirect_to_login(*args, **kwargs): return args[0].redirect(users.create_login_url(args[0].request.path)) user = users.get_current_user() if user: if authorized(user): return handler(args[0]) else: logging.warning('An unauthorized user has attempted to enter an authorized page') return redirect_to_login else: return redirect_to_login
It seems that you aren’t defining your decorator properly.
A decorator is called only once every time you wrap a function with it; from then on the function that the decorator returned will be called. It seems that you (mistakenly) believe that the decorator function itself will be called every time.
Try something like this instead:
Note that in the above code, the decorator just defines a new function and returns it, and this new function itself does the relevant checks.