Webapp2 Sessions in Google app engine

2020-04-09 19:07发布

I just figured out how to implement the webapp2 sessions in my Google app engine project using python. The code is below. What I would like to know is what is the best approach to go about it? What I have done is created a python file and dropped the BaseHandler code in it then I simply import it but I have to constantly duplicate the config key in each python app/file.

The code for the BaseHandler as I got from the website:

class BaseHandler(webapp2.RequestHandler):
    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()

To set variables in a session I simply import the BaseHandler into the app and call the following just as in the examples:

self.session['name'] = name

To get variables is just as in the example:

name = self.session.get('name')

The part I have to copy in each file is the following:

 config = {}
 config['webapp2_extras.sessions'] = {'secret_key': 'some-secret-key-to-use',}

app = webapp2.WSGIApplication([('/hello.*', MainHandler),
                            ], config=config, debug=True)

Is this the best way to go about it or are there better approaches? Thanks

1条回答
不美不萌又怎样
2楼-- · 2020-04-09 19:46

The way I handled sessions was to create a BaseHandler class deriving from webapp2.RequestHandler. I equipped BaseHandler class with all session initialization and default session values and reused this class to create each handler instead of directly deriving from webapp2.RequestHandler.

The obvious advantage of this is that you don't have to repeat session initialization each time you have to use a session in a file.

class BaseHandler(webapp2.RequestHandler):
    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.
        sess = self.session_store.get_session()
        #add some default values:
        if not sess.get('theme'):
            sess['theme']='cosmo'#'slate'
        return sess


class MainPage(BaseHandler):
    def get(self):
        template = JINJA_ENVIRONMENT.get_template('stereo.html')
        if self.request.get('theme'):
            theme=self.request.get('theme')
            self.session['theme']=theme
        else:
            theme=self.session['theme']

Refer my article for more details.

查看更多
登录 后发表回答