2011-12-17 71 views
1

是否合法作出这样的导入:导入是否合法或不推荐?

from webapp2_extras.appengine.auth.models import User as webapp2.User 

我想指的是对象webapp2.User即使它在技术上是没有的。这是因为还有另一个名为User的对象,所以我可以用这个模型命名用户,例如webapp2_user与fbuser(Facebook用户通过“login with facebook”)和谷歌用户截然不同。这似乎是一个很好的类,因为它承认这一模型连接您的谷歌或Facebook的其他帐户:

class User(object): 

    def get_id(self): 
     """Returns this user's unique ID, which can be an integer or string.""" 

    @classmethod 
    def get_by_auth_token(cls, user_id, token): 
     """Returns a user object based on a user ID and token. 

     :param user_id: 
      The user_id of the requesting user. 
     :param token: 
      The token string to be verified. 
     :returns: 
      A tuple ``(User, timestamp)``, with a user object and 
      the token timestamp, or ``(None, None)`` if both were not found. 
     """ 

    @classmethod 
    def get_by_auth_password(cls, auth_id, password): 
     """Returns a user object, validating password. 

     :param auth_id: 
      Authentication id. 
     :param password: 
      Password to be checked. 
     :returns: 
      A user object, if found and password matches. 
     :raises: 
      ``auth.InvalidAuthIdError`` or ``auth.InvalidPasswordError``. 
     """ 

    @classmethod 
    def create_auth_token(cls, user_id): 
     """Creates a new authorization token for a given user ID. 

     :param user_id: 
      User unique ID. 
     :returns: 
      A string with the authorization token. 
     """ 

    @classmethod 
    def delete_auth_token(cls, user_id, token): 
     """Deletes a given authorization token. 

     :param user_id: 
      User unique ID. 
     :param token: 
      A string with the authorization token. 
     """ 

感谢您对这个

回答

1

这是非法的,不推荐。

如果您希望User基类在不同的应用程序中使用,您应该继承它并覆盖THAT应用程序中所需的部分。不要这样进口。

+0

谢谢你回答比我问的更多。基类来自webapp2 API,我正在学习如何使用它,因为它似乎具有内置哈希和盐的密码,这是一个很大的优势,不必实现密码的哈希和腌制。 – 2011-12-18 05:56:30

2
from webapp2_extras.appengine.auth.models import User as webapp2.User 

任何回答或评论是无效的语法,Python不允许您在as webapp2.User中有.,您应该使用as webapp2_User代替。

相关问题