使用Web2py Auth检索用户信息

时间:2014-04-07 06:36:11

标签: python google-app-engine authentication webapp2

我正在学习如何按照http://gosurob.com/post/20024043690/gaewebapp2accounts中的说明在GAE中实现用户登录。

下面是代码的一部分,它创建了我的其他处理程序应该继承的用户感知处理程序。这允许我通过self.user简单地返回用户信息。现在我的问题是如何在ndb类中检索用户信息,这显然继承了ndb.Model而不是BaseHandler。请指教。谢谢!

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)
        #super(BaseHandler,self).dispatch()
    finally:
        # Save all sessions.
        self.session_store.save_sessions(self.response)

   @webapp2.cached_property
   def auth(self):
     return auth.get_auth(request=self.request)

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

    @webapp2.cached_property
    def user_model(self):
       user_model, timestamp = self.auth.store.user_model.get_by_auth_token(
              self.user['user_id'], 
              self.user['token']) if self.user else (None, None)
       return user_model

class Global_Var(ndb.Model):   #<<< This is the ndb class from which I want to use the user info.
    current_user = ndb.StringProperty()

    @staticmethod
    def get_id():
        return user['email']   #<<< This, of course, errors out since user is not defined in this class

0 个答案:

没有答案