Domanda

to ensure that all session data is saved on each request, we just use dispatch method in our base handler class, this an example:

class BaseHandler(webapp2.RequestHandler):
  @webapp2.cached_property
  def auth(self):

   return auth.get_auth()

  @webapp2.cached_property
  def user_info(self):
        return self.auth.get_user_by_session()

  @webapp2.cached_property
  def user(self):

     u = self.user_info
     return self.user_model.get_by_id(u['user_id']) if u else None

  @webapp2.cached_property
  def user_model(self):
       return self.auth.store.user_model

  @webapp2.cached_property
  def session(self):

     return self.session_store.get_session(backend="datastore")

  def render_template(self, view_filename, params={}):
    user = self.user_info
    params['user'] = user
    self.response.out.write(template.render(path, params))

 def display_message(self, message):

   params = {
    'message': message
   }
   self.render_template('message.html', params)

 # this is needed for webapp2 sessions to work
 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)

according to this tutorial http://blog.abahgat.com/2013/01/07/user-authentication-with-webapp2-on-google-app-engine/#comment-4858 actually i don't understand how the framework use dispatch method, i just declare it i never use it ! is that right ?

È stato utile?

Soluzione

Dispatch dispatches the (your) webapp2 requesthandlers.

Besides implementing your own dispatch algorithm, you can do things before and after the handler has started or finished, like:

  • loading your session data before you call the handler and
  • storing your data in the session after your handler has finished.

If you do not use the dispatch override, you have to load and store the session data in all of your handlers yourself.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top