Basically what I am trying to achieve is check if user is logged in and if he's not he shall be redirected to the /somepage. I am trying to make it by creating a class that extends webapp.RequestHandler and later in code just extend that class. (something like beforeFilter in cakephp) If something isn't clear I'll explain it :).
5 Answers
You can override the dispatch()
method in webapp requests to put this check before the super()
call:
http://webapp-improved.appspot.com/guide/handlers.html#overriding-dispatch
You could also use a decorator, as suggested by other answers, but that would require putting it at the head of each handler (which you may or may not want). Overriding dispatch()
as above allows you to do it once for all pages.
If you are using the python2.5 runtime, you need to install webapp2 in your project and use it in your code.

- 16,852
- 8
- 31
- 42
-
Also I forget to mention that I am using python 2.5. – Vizualni Apr 12 '12 at 18:24
One way would be to implement a Python decorator similar to the ones used in Django for user authentication. The login_required decorator for instance. This thread on stackoverflow is also related.

- 1
- 1

- 14,760
- 10
- 76
- 102
use decorators.
here the documentation: http://www.python.org/dev/peps/pep-0318/
here an example to remove slashes on the request path:
def removeslash(method):
"""Use this decorator to remove trailing slashes from the request path.
For example, a request to '/foo/' would redirect to '/foo' with this
decorator. Your request handler mapping should use a regular expression
like r'/foo/*' in conjunction with using the decorator.
"""
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
if self.request.path.endswith("/"):
if self.request.method == "GET":
uri = self.request.path.rstrip("/")
if self.request.query: uri += "?" + self.request.query
self.redirect(uri)
return
return self.status(404)
return method(self, *args, **kwargs)
return wrapper
and you use it like:
class MyHandler(RequestHandler):
@removeslash
def get(self):
# your code

- 7,038
- 2
- 47
- 66
What I've done is create a class hierarchy that lets me use the Template Method design pattern, something like:
class Page(webapp.RequestHandler):
def do_get(self):
''' override in derived classes to actually do something '''
pass
def get(self):
''' do NOT override this in derived classes '''
# do whatever you need to do to verify that the get() should succeed.
# Maybe redirect, show an error, whatever
self.do_get()
...the logic surrounding the call to the do_get()
template method function can be as complex as you need it to be, and as long as your handler classes are all derived from this class, they will get the correct behavior automatically.

- 35,114
- 8
- 59
- 65
I have not tested this but this should work. As long as you use BasicHandler instead of webapp2 directly any request where a user is not logged in will redirect.
class BasicHandler(webapp2.RequestHandler):
def __init__(self, request=None, response=None):
self.initialize(request, response)
self.user = users.get_current_user()
if not self.user:
self.redirect('/somepage')
class Page(BasicHandler):
def get(self):
# Do your work/render...
*edit Put the BasicHandler in its own .py file and import that

- 308
- 2
- 8