I just figured out how to implement the webapp2 sessions in my Google app engine project using python. The code is below. What I would like to know is what is the best approach to go about it? What I have done is created a python file and dropped the BaseHandler code in it then I simply import it but I have to constantly duplicate the config key in each python app/file.
The code for the BaseHandler as I got from the website:
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def session(self):
# Returns a session using the default cookie key.
return self.session_store.get_session()
To set variables in a session I simply import the BaseHandler into the app and call the following just as in the examples:
self.session['name'] = name
To get variables is just as in the example:
name = self.session.get('name')
The part I have to copy in each file is the following:
config = {}
config['webapp2_extras.sessions'] = {'secret_key': 'some-secret-key-to-use',}
app = webapp2.WSGIApplication([('/hello.*', MainHandler),
], config=config, debug=True)
Is this the best way to go about it or are there better approaches? Thanks