Show link to /site/user/list on /site/user/lookup when User is not found.
authorTodd Larsen <tlarsen@google.com>
Wed, 01 Oct 2008 02:11:56 +0000
changeset 229 a46c238be8db
parent 228 2204287da374
child 230 cb2c7ae5424d
Show link to /site/user/list on /site/user/lookup when User is not found. http://code.google.com/p/soc/issues/detail?id=61 Patch by: Chen Lunpeng Review by: Todd Larsen (with modifications)
app/soc/logic/site/id_user.py
app/soc/templates/soc/site/user/profile/lookup.html
app/soc/views/site/user/profile.py
--- a/app/soc/logic/site/id_user.py	Wed Oct 01 02:02:44 2008 +0000
+++ b/app/soc/logic/site/id_user.py	Wed Oct 01 02:11:56 2008 +0000
@@ -18,6 +18,7 @@
 """
 
 __authors__ = [
+  '"Chen Lunpeng" <forever.clp@gmail.com>',
   '"Todd Larsen" <tlarsen@google.com>',
   ]
 
@@ -117,6 +118,79 @@
   return user
 
 
+def getNearestUsers(id=None, link_name=None):
+  """Get User entities just before and just after the specified User.
+    
+  Args:
+    id: a Google Account (users.User) object; default is None (not supplied)
+    link_name: link name string; default is None (not supplied)
+
+  Returns:
+    User entities being those just before and just after the (possibly
+    non-existent) User for given id or link_name,
+      OR
+    possibly None if query had no results or neither id or link_name were
+    supplied.
+  """
+  if id:
+    query = db.GqlQuery("SELECT * FROM User WHERE id > :1", id)
+    return query.fetch(1)
+
+  #if id not supplied, try link name.
+  if link_name:
+    query = db.GqlQuery("SELECT * FROM User WHERE link_name > :1", link_name)
+    return query.fetch(1)
+
+  return None
+
+
+def findNearestUsersOffset(width, id=None, link_name=None):
+  """Finds offset of beginning of a range of Users around the nearest User.
+  
+  Args:
+    limit: the width of the "found" window around the nearest User found 
+    id: a Google Account (users.User) object, or None
+    link_name: link name input in the Lookup form or None if not supplied.
+    
+  Returns:
+    an offset into the list of Users that is width/2 less than the
+    offset of the first User returned by getNearestUsers(), or zero if
+    that offset would be less than zero
+      OR
+    None if there are no nearest Users or the offset of the beginning of
+    the range cannot be found for some reason 
+  """
+  # find User "nearest" to supplied id (Google Account) or link_name
+  nearest_users = getNearestUsers(id=id, link_name=link_name)
+  
+  if not nearest_users:
+    # no "nearest" User, so indicate that with None
+    return None
+
+  nearest_user = nearest_users[0]
+
+  # start search for beginning of nearest Users range at offset zero
+  offset = 0
+  users = getUsersForOffsetAndLimit(offset=offset, limit=width)
+  
+  while True:
+    for user in users:
+      if nearest_user.id == user.id:
+        # nearest User found in current search range, so return a range start
+        return max(0, (offset - (width/2)))
+
+      offset = offset + 1
+
+    # nearest User was not in the current search range, so fetch the next set
+    users = getUsersForOffsetAndLimit(offset=offset, limit=width)
+
+    if not users:
+      # nearest User never found, so indicate that with None
+      break
+
+  return None
+
+
 def doesUserExist(id):
   """Returns True if User exists in the Datastore for a Google Account.
     
--- a/app/soc/templates/soc/site/user/profile/lookup.html	Wed Oct 01 02:02:44 2008 +0000
+++ b/app/soc/templates/soc/site/user/profile/lookup.html	Wed Oct 01 02:11:56 2008 +0000
@@ -98,6 +98,17 @@
    </td>
   </tr>
 {% endif %}
+{% if lookup_link %}
+  <tr>
+   <td colspan="4">&nbsp;</td>
+  </tr>
+  <tr>
+   <td colspan="4">
+    <a href="{{ lookup_link }}">The specified User was not found, but similar
+     Users are listed here.</a>
+   </td>
+  </tr>
+{% endif %} 
  </table>
 </form>
 </p>
--- a/app/soc/views/site/user/profile.py	Wed Oct 01 02:02:44 2008 +0000
+++ b/app/soc/views/site/user/profile.py	Wed Oct 01 02:11:56 2008 +0000
@@ -32,6 +32,7 @@
 from soc.logic.site import id_user
 from soc.views import simple
 from soc.views.helpers import forms_helpers
+from soc.views.helpers import list_helpers
 from soc.views.helpers import request_helpers
 from soc.views.helpers import response_helpers
 from soc.views.user import profile
@@ -127,7 +128,13 @@
           lookup_message = ugettext_lazy('User found by email.')
         else:
           email_error = ugettext_lazy('User with that email not found.')
-
+          range_width = list_helpers.getPreferredListPagination()
+          nearest_user_range_start = id_user.findNearestUsersOffset(
+              range_width, id=form_id)
+            
+          if nearest_user_range_start is not None:            
+            context['lookup_link'] = './list?offset=%s&limit=%s' % (
+                nearest_user_range_start, range_width)
       if not user:
         # user not found yet, so see if link name was provided
         linkname = form.cleaned_data.get('link_name')
@@ -141,7 +148,14 @@
             email_error = None  # clear previous error, since User was found
           else:
             context['linkname_error'] = ugettext_lazy(
-                'User with that link name not found.')            
+                'User with that link name not found.')
+            range_width = list_helpers.getPreferredListPagination()
+            nearest_user_range_start = id_user.findNearestUsersOffset(
+                range_width, link_name=linkname)
+            
+            if nearest_user_range_start is not None:            
+              context['lookup_link'] = './list?offset=%s&limit=%s' % (
+                  nearest_user_range_start, range_width)
     # else: form was not valid
   # else:  # method == 'GET'
 
@@ -296,7 +310,7 @@
                 values=profile.SUBMIT_MESSAGES))
 
         # populate form with the existing User entity
-        form = EditForm(initial={ 'key_name': user.key().name(),
+        form = EditForm(initial={'key_name': user.key().name(),
             'id': user.id.email, 'link_name': user.link_name,
             'nick_name': user.nick_name, 'is_developer': user.is_developer})
       else:
@@ -368,7 +382,7 @@
 DEF_SITE_CREATE_USER_PROFILE_TMPL = 'soc/site/user/profile/edit.html'
 
 def create(request, template=DEF_SITE_CREATE_USER_PROFILE_TMPL):
-  """View for a Developer to modify the properties of a User Model entity.
+  """View for a Developer to create a new User Model entity.
 
   Args:
     request: the standard django request object
@@ -414,4 +428,4 @@
 
   context['form'] = form
 
-  return response_helpers.respond(request, template, context)
\ No newline at end of file
+  return response_helpers.respond(request, template, context)