thirdparty/google_appengine/google/appengine/api/users.py
changeset 2273 e4cb9c53db3e
parent 1278 a7766286a7be
child 2864 2e0b0af889be
--- a/thirdparty/google_appengine/google/appengine/api/users.py	Tue Apr 21 16:28:13 2009 +0000
+++ b/thirdparty/google_appengine/google/appengine/api/users.py	Fri Apr 24 14:16:00 2009 +0000
@@ -33,7 +33,6 @@
 import os
 from google.appengine.api import apiproxy_stub_map
 from google.appengine.api import user_service_pb
-from google.appengine.api import api_base_pb
 from google.appengine.runtime import apiproxy_errors
 
 
@@ -50,6 +49,7 @@
   """Raised by UserService calls if the generated redirect URL was too long.
   """
 
+
 class NotAllowedError(Error):
   """Raised by UserService calls if the requested redirect URL is not allowed.
   """
@@ -58,7 +58,7 @@
 class User(object):
   """A user.
 
-  We provide here the email address, nickname, and auth domain for a user.
+  We provide the email address, nickname, auth domain, and id for a user.
 
   A nickname is a human-readable string which uniquely identifies a Google
   user, akin to a username. It will be an email address for some users, but
@@ -66,12 +66,18 @@
   """
 
 
-  def __init__(self, email=None, _auth_domain=None):
+  __user_id = None
+
+  def __init__(self, email=None, _auth_domain=None, _user_id=None):
     """Constructor.
 
     Args:
-      # email is optional. it defaults to the current user.
-      email: string
+      email: An optional string of the user's email address. It defaults to
+          the current user's email address.
+
+    Raises:
+      UserNotFoundError: Raised if the user is not logged in and the email
+          argument is empty.
     """
     if _auth_domain is None:
       _auth_domain = os.environ.get('AUTH_DOMAIN')
@@ -83,12 +89,15 @@
     if email is None:
       assert 'USER_EMAIL' in os.environ
       email = os.environ['USER_EMAIL']
+      if _user_id is None and 'USER_ID' in os.environ:
+        _user_id = os.environ['USER_ID']
 
     if not email:
       raise UserNotFoundError
 
     self.__email = email
     self.__auth_domain = _auth_domain
+    self.__user_id = _user_id or None
 
   def nickname(self):
     """Return this user's nickname.
@@ -108,6 +117,13 @@
     """Return this user's email address."""
     return self.__email
 
+  def user_id(self):
+    """Return either a permanent unique identifying string or None.
+
+    If the email address was set explicity, this will return None.
+    """
+    return self.__user_id
+
   def auth_domain(self):
     """Return this user's auth domain."""
     return self.__auth_domain
@@ -119,7 +135,11 @@
     return str(self.nickname())
 
   def __repr__(self):
-    return "users.User(email='%s')" % self.email()
+    if self.__user_id:
+      return "users.User(email='%s',_user_id='%s')" % (self.email(),
+                                                       self.user_id())
+    else:
+      return "users.User(email='%s')" % self.email()
 
   def __hash__(self):
     return hash((self.__email, self.__auth_domain))
@@ -152,7 +172,7 @@
         user_service_pb.UserServiceError.REDIRECT_URL_TOO_LONG):
       raise RedirectTooLongError
     elif (e.application_error ==
-        user_service_pb.UserServiceError.NOT_ALLOWED):
+          user_service_pb.UserServiceError.NOT_ALLOWED):
       raise NotAllowedError
     else:
       raise e
@@ -205,6 +225,6 @@
   the User class, because admin status is not persisted in the datastore. It
   only exists for the user making this request right now.
   """
-  return (os.environ.get('USER_IS_ADMIN', '0')) == "1"
+  return (os.environ.get('USER_IS_ADMIN', '0')) == '1'
 
 IsCurrentUserAdmin = is_current_user_admin