I have login_required
decorator as follows:
def login_required(function):
""" Decorator to check Logged in users."""
def check_login(self, *args, **kwargs):
if not self.auth.get_user_by_session():
self.redirect('/_ah/login_required')
else:
return function(self, *args, **kwargs)
return check_login
Now I have a Page
(which is rendered by a seperate Handler
) where I have an option for users to upload image which can be viewed both by guests and users. As soon as the form is posted it is handled by another Handler
which uses the @login_required
decorator.
What I want to achieve is passing a continue_url
variable that I can use in the check_login
function while redirecting so that the user gets redirected back to the same page after logging in.
So basically, it sounds like you want to pass an argument to the decorator when you use it. Python does support this. The basic idea is that
@decorated(argument) def foo(...)
is equivalent todef foo(...); foo = decorated(argument)(foo)
.So you need to make
decorated
be something such thatdecorated(argument)
can decoratefoo
. There are several recipes for this. Here's one - makedecorated
a class with a__call__
method, so thatdecorated(argument)
is a callable object that storesargument
and uses it when called:This can also be achieved with a plain function (and an additional level of nesting), with tricks involving
functools.partial
etc.