I'm trying to get sessions to work with Webapp2 with GAE, but I'm stuck retrieving the stored parameters. Maybe I'm overlooking the obvious.
A dressed down version of with main.py where I store a value in the webapp2 session:
import webapp2
from webapp2_extras import sessions
other imports...
class BaseHandler(webapp2.RequestHandler): # Copied from Google's doc
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()
class MainPage(webapp2.RequestHandler): # My main page proper
def get(self):
self.session['foo'] = 'bar' # Store somehing in the session
template_values = {
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
application = webapp2.WSGIApplication([
('/', MainPage, ),], debug = True)
My problem is how to access the value stored in the session from another module. In test.py I do the following:
from webapp2_extras import sessions
import main
other imports ...
class Test(main.BaseHandler):
def post(self):
foo1 = self.session.get('foo')
return foo1
class ProductsPage(webapp2.RequestHandler):
def get(self):
foo2 = Test.post()
...
But I get the following error: TypeError: unbound method post() must be called with Test instance as first argument (got nothing instead)
I just can't find the proper way to use the Test class and retrieve the value stored in the session. I must be overlooking something when using the class, but I'm stuck and can't find the way out.
Maybe somebody looking from outside will spot it easily?