1

I encountered a strange problem: The gaeutilities' session worked on the GAE SDK, but not on the actual Google App Engine platform. The followings are session creation and existence checking using Python, respectively.

Session creation:

self.session = sessions.Session()
self.session.delete_item('account')
self.session.delete_item('accountKey')
...
query = db.Query(model.Member)
query = query.filter('account =', account)  # 'account' is the user account
results = query.fetch(limit=1)
if results:  # Account exists
    member = results[0]
    self.session['account'] = account   
    self.session['accountKey'] = member.key()
...

Session existence checking:

self.session = sessions.Session()
if 'accountKey' in self.session:  # Session exists
    account = self.session['account']  # Could this be the problem?
...

The above program runs OK on the GAE SDK. But I uploaded the program to Google App Engine, and it didn't work. What might be the problem?

Randy Tang
  • 4,283
  • 12
  • 54
  • 112
  • what's the actual error? Also try checking the len of 'results' rather then it's existence as (I seem to recall) it will return an empty list instead of none. – Paul Collingwood Dec 28 '12 at 13:59
  • Checking the log (No idea that those means): Expires: Tue, 03 Jul 2001 06:00:00 GMTLast-Modified: Fri, 28 Dec 12 14:04:14 UTCCache-Control: no-store, no-cache, must-revalidate, max-age=0Cache-Control: post-check=0, pre-check=0Pragma: no-cache – Randy Tang Dec 28 '12 at 14:05

2 Answers2

0

I am not familiair with gaeutilities. But with self.session = sessions.Session() you create a new session. This will be empty. So your check if 'accountKey' in ... will not work. There must be another way to get the existing session.

voscausa
  • 11,253
  • 2
  • 39
  • 67
  • Can somebody shed some light on this? Thanks. – Randy Tang Dec 28 '12 at 14:44
  • I tried: if hasattr(self, 'session') and 'accountKey' in self.session: ... --> not working – Randy Tang Dec 28 '12 at 14:45
  • As far as I know, "self.session = sessions.Session()" either creates a new session, or gets the existent session. – Randy Tang Dec 28 '12 at 16:29
  • OK, so you could show what is in the session with : logging.info(self.session) But is there a reason for using gaeutilities? The preferred method for GAE with Python 27 uses webapp2, which has well documented session functionality. – voscausa Dec 28 '12 at 16:36
  • no particular reasons for using gaeutilities. I'll give webapp2's session a try. Thanks. – Randy Tang Dec 29 '12 at 01:00
  • I tried GAE webapp2 sessions but failed. I've edited another question about what the correct process of creating and checking sessions (http://stackoverflow.com/questions/14078054/gae-webapp2-session-the-correct-process-of-creating-and-checking-sessions). Would you care to take a look at it? Thanks. – Randy Tang Dec 29 '12 at 13:15
  • I have seen your new question. Did you follow the example, using dispatch and a webapp2 handler? Why would you like to delete the session? You can control the lifetime (maxage) of the cookie. If you like to delete the session, you can delete the cookie and skip the save session in the finally block, using a flag in the request registry. – voscausa Dec 29 '12 at 13:24
  • I am not familiar with sessions. My understanding is: when a user logs in, create a session; when a particular function is invoked, check if a session exists, and if not, ask for logging in; and finally, when the user logs out, delete the session. Maybe you can correct me if I am wrong. Furthermore , I don't quite understand the documentation of webapp2's session. For example, where should we call the BaseHandler class? How do I use dispatch() and session() methods provided by BaseHandler? The concepts there are so few and even vague. – Randy Tang Dec 29 '12 at 14:24
  • With webapp2 sessions, the session exists during the lifetime of the app. In your case. You can only use the app, if you login, and the login will be be registred in the session. Using a cookie with maxage, the session will end after some time. The basehandler class will be called by the handler, because this class overrides the dispatch method of the handler. So make an exact copy of this class, to use it. – voscausa Dec 29 '12 at 14:36
  • I guess I have to do more research. One last question, I kept getting the error message: '...' object has no attribute 'session' when using self.session('...') and self.session.get('...'). Why? – Randy Tang Dec 29 '12 at 14:46
  • I think the session store must be activated before your webapp2 request handler is dispatched. I will try to giive an example in your new question. – voscausa Dec 29 '12 at 15:07
0

I have found another approach to solve the problem using gae-sessions. Check here.

Community
  • 1
  • 1
Randy Tang
  • 4,283
  • 12
  • 54
  • 112