john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

google app engine webapp2 extras session

# -*- coding: utf-8 -*-
from uuid import uuid4
import webapp2
from webapp2_extras import sessions
from webapp2_extras import auth
from webapp2_extras import *


class BaseHandler( webapp2.RequestHandler ):
    def dispatch( self ):
        self.session_store = sessions.get_store( request = self.request )

        try:
            webapp2.RequestHandler.dispatch( self )
        finally:
            self.session_store.save_sessions( self.response )

    @webapp2.cached_property
    def session( self ):
        return self.session_store.get_session()


class MainHandler( BaseHandler ):

    def get( self ):
        if self.session.get( 'foo' ):
            foo = self.session.get( 'foo' )
            self.response.out.write( 'session variable foo = %s ' % ( foo ) )
#            self.auth.unset_session()        # google has no answer as to why the auth unset errors out

            if foo == 'bar':
                self.session['foo'] = 'foo'
            else:
                self.session['foo'] = 'bar'

        else:
            self.response.out.write( 'session counter not found, setting foo to bar' )
            self.session['foo'] = 'bar'



entry_points =  [  ( '/', MainHandler ) ]       # list of tuples, or use webapp2.Route for advanced functionality

config = dict()
config[ 'webapp2_extras.sessions' ] = {'secret_key': str( uuid4() ), 'session_max_age': 3600  }  # random string seed for cookie generation, cookies expire at 3600 seconds
app = webapp2.WSGIApplication ( entry_points , config=config, debug=True )    # for pure uwsgi change it to application=

  • « hash table implementation
  • index 2013 directory listing multi sort search find »

Published

Jul 7, 2013

Category

python-appengine

~135 words

Tags

  • appengine 18
  • engine 12
  • extras 1
  • google 18
  • python 180
  • session 3
  • webapp2 4