Need to do some authentication checks before deciding what content to display. Ideally, I'd like to have a restrictive base handler, that would implement the "before" hook that could show an alternative content, instead of the requested content.
class Restrictive(webapp2.RequestHandler):
def __init__(self, *args, **kwargs):
if not self.is_logged_in():
self.response.write('not logged in')
self.abort(200)
def is_logged_in(self):
return False
But this won't output the response, only headers. What is the proper way of aborting the requested handler and showing alternative content?
Thanks.
I wrote this decorator once.
def authenticated(handler):
"""
Decorator for checking if there's a user associated with the current session.
Will also fail if there's no session present.
"""
def check_login(self, *args, **kwargs):
if not self.is_logged_in():
self.response.write('not logged in')
return self.error(403)
else:
return handler(self, *args, **kwargs)
return check_login
You can then decorate your methods if they need to be authenticated.
class MyHandler:
@authenticated
def get(self):
pass