app/soc/logic/site/id_user.py
changeset 229 a46c238be8db
parent 223 a18e93e21672
child 230 cb2c7ae5424d
equal deleted inserted replaced
228:2204287da374 229:a46c238be8db
    16 
    16 
    17 """Basic ID (Google Account) and User (Model) query functions.
    17 """Basic ID (Google Account) and User (Model) query functions.
    18 """
    18 """
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
       
    21   '"Chen Lunpeng" <forever.clp@gmail.com>',
    21   '"Todd Larsen" <tlarsen@google.com>',
    22   '"Todd Larsen" <tlarsen@google.com>',
    22   ]
    23   ]
    23 
    24 
    24 
    25 
    25 import re
    26 import re
   115     user = getUserFromId(id)
   116     user = getUserFromId(id)
   116     
   117     
   117   return user
   118   return user
   118 
   119 
   119 
   120 
       
   121 def getNearestUsers(id=None, link_name=None):
       
   122   """Get User entities just before and just after the specified User.
       
   123     
       
   124   Args:
       
   125     id: a Google Account (users.User) object; default is None (not supplied)
       
   126     link_name: link name string; default is None (not supplied)
       
   127 
       
   128   Returns:
       
   129     User entities being those just before and just after the (possibly
       
   130     non-existent) User for given id or link_name,
       
   131       OR
       
   132     possibly None if query had no results or neither id or link_name were
       
   133     supplied.
       
   134   """
       
   135   if id:
       
   136     query = db.GqlQuery("SELECT * FROM User WHERE id > :1", id)
       
   137     return query.fetch(1)
       
   138 
       
   139   #if id not supplied, try link name.
       
   140   if link_name:
       
   141     query = db.GqlQuery("SELECT * FROM User WHERE link_name > :1", link_name)
       
   142     return query.fetch(1)
       
   143 
       
   144   return None
       
   145 
       
   146 
       
   147 def findNearestUsersOffset(width, id=None, link_name=None):
       
   148   """Finds offset of beginning of a range of Users around the nearest User.
       
   149   
       
   150   Args:
       
   151     limit: the width of the "found" window around the nearest User found 
       
   152     id: a Google Account (users.User) object, or None
       
   153     link_name: link name input in the Lookup form or None if not supplied.
       
   154     
       
   155   Returns:
       
   156     an offset into the list of Users that is width/2 less than the
       
   157     offset of the first User returned by getNearestUsers(), or zero if
       
   158     that offset would be less than zero
       
   159       OR
       
   160     None if there are no nearest Users or the offset of the beginning of
       
   161     the range cannot be found for some reason 
       
   162   """
       
   163   # find User "nearest" to supplied id (Google Account) or link_name
       
   164   nearest_users = getNearestUsers(id=id, link_name=link_name)
       
   165   
       
   166   if not nearest_users:
       
   167     # no "nearest" User, so indicate that with None
       
   168     return None
       
   169 
       
   170   nearest_user = nearest_users[0]
       
   171 
       
   172   # start search for beginning of nearest Users range at offset zero
       
   173   offset = 0
       
   174   users = getUsersForOffsetAndLimit(offset=offset, limit=width)
       
   175   
       
   176   while True:
       
   177     for user in users:
       
   178       if nearest_user.id == user.id:
       
   179         # nearest User found in current search range, so return a range start
       
   180         return max(0, (offset - (width/2)))
       
   181 
       
   182       offset = offset + 1
       
   183 
       
   184     # nearest User was not in the current search range, so fetch the next set
       
   185     users = getUsersForOffsetAndLimit(offset=offset, limit=width)
       
   186 
       
   187     if not users:
       
   188       # nearest User never found, so indicate that with None
       
   189       break
       
   190 
       
   191   return None
       
   192 
       
   193 
   120 def doesUserExist(id):
   194 def doesUserExist(id):
   121   """Returns True if User exists in the Datastore for a Google Account.
   195   """Returns True if User exists in the Datastore for a Google Account.
   122     
   196     
   123   Args:
   197   Args:
   124     id: a Google Account object
   198     id: a Google Account object