Webapp2 Sessions in Google app engine

二次信任 提交于 2019-12-01 20:19:21

问题


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:


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.



来源:https://stackoverflow.com/questions/13997215/webapp2-sessions-in-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!