app/soc/logic/site/id_user.py
changeset 245 b14c2c4d3484
parent 230 cb2c7ae5424d
child 262 52a42831d9d6
equal deleted inserted replaced
244:da80c6519eea 245:b14c2c4d3484
   199   """
   199   """
   200   if getUserFromId(id):
   200   if getUserFromId(id):
   201     return True
   201     return True
   202   else:
   202   else:
   203     return False
   203     return False
   204     
   204 
       
   205 
   205 def isIdUser(id=None):
   206 def isIdUser(id=None):
   206   """Returns True if a Google Account has it's User entity in datastore.
   207   """Returns True if a Google Account has it's User entity in datastore.
   207 
   208 
   208   Args:
   209   Args:
   209     id: a Google Account (users.User) object; if id is not supplied,
   210     id: a Google Account (users.User) object; if id is not supplied,
   220   if not user:
   221   if not user:
   221     # no User entity for this Google Account
   222     # no User entity for this Google Account
   222     return False
   223     return False
   223   
   224   
   224   return True
   225   return True
       
   226 
   225 
   227 
   226 def isIdDeveloper(id=None):
   228 def isIdDeveloper(id=None):
   227   """Returns True if a Google Account is a Developer with special privileges.
   229   """Returns True if a Google Account is a Developer with special privileges.
   228   
   230   
   229   Since it only works on the current logged-in user, if id matches the
   231   Since it only works on the current logged-in user, if id matches the
   261     return False
   263     return False
   262   
   264   
   263   return user.is_developer
   265   return user.is_developer
   264 
   266 
   265 
   267 
   266 LINKNAME_REGEX = re.compile(key_name.LINKNAME_PATTERN)
       
   267 
       
   268 def isLinkNameFormatValid(link_name):
       
   269   """Returns True if link_name is in a valid format.
       
   270   
       
   271   Args:
       
   272     link_name: link name used in URLs to identify user
       
   273   """
       
   274   if LINKNAME_REGEX.match(link_name):
       
   275     return True
       
   276   return False
       
   277 
       
   278 
       
   279 def getUserFromLinkName(link_name):
   268 def getUserFromLinkName(link_name):
   280   """Returns User entity for link_name or None if not found.
   269   """Returns User entity for link_name or None if not found.
   281     
   270     
   282   Args:
   271   Args:
   283     link_name: link name used in URLs to identify user
   272     link_name: link name used in URLs to identify user
   284   """
   273   """
   285   return soc.models.user.User.gql('WHERE link_name = :1', link_name).get()
   274   return soc.models.user.User.gql('WHERE link_name = :1', link_name).get()
   286 
   275 
       
   276 
   287 def getUserFromKeyName(key_name):
   277 def getUserFromKeyName(key_name):
   288   """Returns User entity for key_name or None if not found.
   278   """Returns User entity for key_name or None if not found.
   289     
   279     
   290   Args:
   280   Args:
   291     key_name: key name of User entity
   281     key_name: key name of User entity
   292   """
   282   """
   293   return soc.models.user.User.get_by_key_name(key_name)
   283   return soc.models.user.User.get_by_key_name(key_name)
       
   284 
   294 
   285 
   295 def getUserIfLinkName(link_name):
   286 def getUserIfLinkName(link_name):
   296   """Returns User entity for supplied link_name if one exists.
   287   """Returns User entity for supplied link_name if one exists.
   297   
   288   
   298   Args:
   289   Args:
   411   # there is no way to be sure if get_or_insert() returned a new User or
   402   # there is no way to be sure if get_or_insert() returned a new User or
   412   # got an existing one due to a race, so update with user_properties anyway,
   403   # got an existing one due to a race, so update with user_properties anyway,
   413   # in a transaction
   404   # in a transaction
   414   return updateUserProperties(user, **user_properties)
   405   return updateUserProperties(user, **user_properties)
   415 
   406 
       
   407 
   416 def updateUserForKeyName(key_name, **user_properties):
   408 def updateUserForKeyName(key_name, **user_properties):
   417   """Update existing User entity for keyname with supplied properties.
   409   """Update existing User entity for keyname with supplied properties.
   418 
   410 
   419   Args:
   411   Args:
   420     key_name: key name of User entity
   412     key_name: key name of User entity
   435   # there is no way to be sure if get_or_insert() returned a new User or
   427   # there is no way to be sure if get_or_insert() returned a new User or
   436   # got an existing one due to a race, so update with user_properties anyway,
   428   # got an existing one due to a race, so update with user_properties anyway,
   437   # in a transaction
   429   # in a transaction
   438   return updateUserProperties(user, **user_properties)
   430   return updateUserProperties(user, **user_properties)
   439 
   431 
       
   432 
   440 def updateUserProperties(user, **user_properties):
   433 def updateUserProperties(user, **user_properties):
   441   """Update existing User entity using supplied User properties.
   434   """Update existing User entity using supplied User properties.
   442 
   435 
   443   Args:
   436   Args:
   444     user: a User entity
   437     user: a User entity
   451   def update():
   444   def update():
   452     return _unsafeUpdateUserProperties(user, **user_properties)
   445     return _unsafeUpdateUserProperties(user, **user_properties)
   453 
   446 
   454   return db.run_in_transaction(update)
   447   return db.run_in_transaction(update)
   455 
   448 
   456   
   449 
   457 def _unsafeUpdateUserProperties(user, **user_properties):
   450 def _unsafeUpdateUserProperties(user, **user_properties):
   458   """(see updateUserProperties)
   451   """(see updateUserProperties)
   459   
   452   
   460   Like updateUserProperties(), but not run within a transaction. 
   453   Like updateUserProperties(), but not run within a transaction. 
   461   """
   454   """