Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
authorPawel Solyga <Pawel.Solyga@gmail.com>
Fri, 12 Sep 2008 16:33:04 +0000
changeset 135 a7ccde9d9eed
parent 134 1f64d7a4d82d
child 136 a95f511bfcf8
Fixed one typo in response_helpers which caused is_admin context variable not to work correctly. Created new checkLinkNameForId function that checks if link_name is correct when creating new user and modifying existing one. This is now used for validation instead of doesLinkNameBelongToId in UserForm clean_link_name function. The previous validation function didn't allow to create new users and modify linkname of existing ones. Additionally I created new doesLinkNameExist helper function.
app/soc/logic/site/id_user.py
app/soc/views/helpers/response_helpers.py
app/soc/views/user/profile.py
--- a/app/soc/logic/site/id_user.py	Fri Sep 12 10:56:21 2008 +0000
+++ b/app/soc/logic/site/id_user.py	Fri Sep 12 16:33:04 2008 +0000
@@ -23,6 +23,7 @@
 
 
 import re
+import logging
 
 from google.appengine.api import users
 from google.appengine.ext import db
@@ -216,6 +217,49 @@
   raise out_of_band.ErrorResponse(
       'There is no user with a "link name" of "%s".' % link_name, status=404)
 
+def checkLinkNameForId(link_name=None, id=None):
+  """Returns True if link name is correct and can be stored.
+
+  Args:
+    link_name: link name used in URLs to identify user
+    id: a Google Account object; optional, current logged-in user will
+      be used (or False will be returned if no user is logged in)
+  """
+  id = getIdIfMissing(id)
+    
+  if not id:
+    # id not supplied and no Google Account logged in, so link name cannot
+    # belong to an unspecified User
+    return False
+
+  user = getUserFromId(id)
+  link_name_exist = doesLinkNameExist(link_name)
+  
+  # New user with not existing link_name
+  if not user and not link_name_exist:
+    return True
+  # New user with existing link_name
+  elif not user and link_name_exist:
+    return False
+  
+  # Existing user with link_name that exists and doesn't belond to him     
+  if user and link_name_exist and (user.link_name != link_name):
+    return False
+    
+  return True
+
+
+def doesLinkNameExist(link_name=None):
+  """Returns True if link name exists in the Datastore.
+
+  Args:
+    link_name: link name used in URLs to identify user
+  """
+  if getUserFromLinkName(link_name):
+    return True
+  else:
+    return False
+
 
 def doesLinkNameBelongToId(link_name, id=None):
   """Returns True if supplied link name belongs to supplied Google Account.
--- a/app/soc/views/helpers/response_helpers.py	Fri Sep 12 10:56:21 2008 +0000
+++ b/app/soc/views/helpers/response_helpers.py	Fri Sep 12 16:33:04 2008 +0000
@@ -112,7 +112,7 @@
   context['user'] = id_user.getUserIfMissing(context.get('user', None),
                                              context['id'])
   context['is_admin'] = context.get(
-      'is_admin', id_user.isIdDeveloper(id=context['id'])),
+      'is_admin', id_user.isIdDeveloper(id=context['id']))
   context['is_debug'] = context.get('is_debug', system.isDebug())
   context['sign_in'] = context.get(
       'sign_in', users.create_login_url(request.path))
--- a/app/soc/views/user/profile.py	Fri Sep 12 10:56:21 2008 +0000
+++ b/app/soc/views/user/profile.py	Fri Sep 12 16:33:04 2008 +0000
@@ -55,8 +55,7 @@
     link_name = self.cleaned_data.get('link_name')
     if not id_user.isLinkNameFormatValid(link_name):
       raise forms.ValidationError("This link name is in wrong format.")
-    elif not id_user.doesLinkNameBelongToId(link_name):
-      # link_name exists in Datastore but doesn't belong to current user
+    elif not id_user.checkLinkNameForId(link_name):
       raise forms.ValidationError("This link name is already in use.")
     return link_name