Showing posts with label session. Show all posts
Showing posts with label session. Show all posts

Tuesday, February 10, 2015

Setting cookie 'expires' for Google App Engine webapp2_extras.sessions

I like Google App Engine for all the good reasons but it took me some time to
set 'expires' time for session cookies when using webapp2_extras.sessions .



webapp2_extras.sessions only provide control alter max_age of cookie based session and I wanted to control 'expires' time based 'remember me' check box.

Following hack did the trick for me.

class CustomerLogin(webapp2.RequestHandler):
    def dispatch(self):
        # Get a session store for this request.
       
        if bool(cgi.escape(self.request.get('remember'))):
            self.request.app.config['webapp2_extras.sessions']['cookie_args']['expires'] = (datetime.datetime.utcnow()  + datetime.timedelta(minutes=30))
            pass
        else:
            self.request.app.config['webapp2_extras.sessions']['cookie_args']['expires'] = None
               
        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)