I tried to implement GAE's webapp2 session, but there seems very little documentation about it. According to http://webapp-improved.appspot.com/api/webapp2_extras/sessions.html, my steps are as follows:
1.Configure and add config to the main application:
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'my_secret_key',
}
app = webapp2.WSGIApplication([...], config=config)
2.Create session in the login handler
# Delete existent session
--> not mention in the tutorial
# member is found
self.session_store = sessions.get_store(request=handler.request)
self.session['account'] = member.account
3.Check if a session exists at various locations in my program
if self.session['account']:
# Session exists
4.Delete session when user logs out
--> not mentioned in the tutorial
My questions:
I got error message " ... object has no attribute 'session'" during the session creation process (Step 2)
How do I delete a session in steps 2 and 4?
Is the overall session management process correct?
Thanks.
This may not be a direct answer to the question, but it is a solution I found using gaesessions instead of GAE's webapp2 session and I would like to share with everybody. Here we go:
Download gaesessions from https://github.com/dound/gae-sessions by clicking "Download ZIP" button. The downloaded file is "gae-sessions-master.zip".
Unzip the file (a directory "gae-sessions-master" will be created), and copy the directory "gaessions" to the root directory of your application (i.e., where "app.yaml" is)
Create a file called "appengine_config.py" in the root directory, with the following content (copied form https://github.com/dound/gae-sessions/tree/master/demo):
Create a session when a user logs in (variable account is the user's account):
Check if the user's session exists, if yes, return user's account:
Delete the session when the user logs out:
Finally, you may create a cron job to clean expired sessions periodically:
cron.yaml:
Function:
Hope this helps.
Here is an example of the handler and how to use webapp2 extra sessions
main.py with the BaseHandler and a MainHandler
And if you have a seperate login.py :
In your
RequestHandler
overridedispatch
: from webapp2_extras import sessionsand make a
webapp2.cached_property
calledsession
:When you want to access session values, you do
self.session[<key>]
When a user logs in, you can call either:
which will take care of getting rid of the old session and creating the new one for you, or:
As far as logging out, all you should need to call is: