Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
authorPawel Solyga <Pawel.Solyga@gmail.com>
Wed, 01 Oct 2008 14:23:28 +0000
changeset 245 b14c2c4d3484
parent 244 da80c6519eea
child 246 5d2df032e19e
Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc). Patch by: Pawel Solyga Review by: to-be-reviewed
app/soc/logic/common.py
app/soc/logic/site/id_user.py
app/soc/views/site/user/profile.py
app/soc/views/user/profile.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/logic/common.py	Wed Oct 01 14:23:28 2008 +0000
@@ -0,0 +1,49 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Common helper functions.
+"""
+
+__authors__ = [
+  '"Pawel Solyga" <pawel.solyga@gmail.com>',
+  ]
+
+
+import re
+
+
+LINKNAME_PATTERN = r'''(?x)
+    ^
+    [0-9a-z]   # start with ASCII digit or lowercase
+    (
+     [0-9a-z]  # additional ASCII digit or lowercase
+     |         # -OR-
+     _[0-9a-z] # underscore and ASCII digit or lowercase
+    )*         # zero or more of OR group
+    $
+'''
+
+LINKNAME_REGEX = re.compile(LINKNAME_PATTERN)
+
+def isLinkNameFormatValid(link_name):
+  """Returns True if link_name is in a valid format.
+
+  Args:
+    link_name: link name used in URLs for identification
+  """
+  if LINKNAME_REGEX.match(link_name):
+    return True
+  return False
\ No newline at end of file
--- a/app/soc/logic/site/id_user.py	Wed Oct 01 09:43:25 2008 +0000
+++ b/app/soc/logic/site/id_user.py	Wed Oct 01 14:23:28 2008 +0000
@@ -201,7 +201,8 @@
     return True
   else:
     return False
-    
+
+
 def isIdUser(id=None):
   """Returns True if a Google Account has it's User entity in datastore.
 
@@ -223,6 +224,7 @@
   
   return True
 
+
 def isIdDeveloper(id=None):
   """Returns True if a Google Account is a Developer with special privileges.
   
@@ -263,19 +265,6 @@
   return user.is_developer
 
 
-LINKNAME_REGEX = re.compile(key_name.LINKNAME_PATTERN)
-
-def isLinkNameFormatValid(link_name):
-  """Returns True if link_name is in a valid format.
-  
-  Args:
-    link_name: link name used in URLs to identify user
-  """
-  if LINKNAME_REGEX.match(link_name):
-    return True
-  return False
-
-
 def getUserFromLinkName(link_name):
   """Returns User entity for link_name or None if not found.
     
@@ -284,6 +273,7 @@
   """
   return soc.models.user.User.gql('WHERE link_name = :1', link_name).get()
 
+
 def getUserFromKeyName(key_name):
   """Returns User entity for key_name or None if not found.
     
@@ -292,6 +282,7 @@
   """
   return soc.models.user.User.get_by_key_name(key_name)
 
+
 def getUserIfLinkName(link_name):
   """Returns User entity for supplied link_name if one exists.
   
@@ -413,6 +404,7 @@
   # in a transaction
   return updateUserProperties(user, **user_properties)
 
+
 def updateUserForKeyName(key_name, **user_properties):
   """Update existing User entity for keyname with supplied properties.
 
@@ -437,6 +429,7 @@
   # in a transaction
   return updateUserProperties(user, **user_properties)
 
+
 def updateUserProperties(user, **user_properties):
   """Update existing User entity using supplied User properties.
 
@@ -453,7 +446,7 @@
 
   return db.run_in_transaction(update)
 
-  
+
 def _unsafeUpdateUserProperties(user, **user_properties):
   """(see updateUserProperties)
   
@@ -478,4 +471,4 @@
       prop.__set__(user, value)
         
   user.put()
-  return user
+  return user
\ No newline at end of file
--- a/app/soc/views/site/user/profile.py	Wed Oct 01 09:43:25 2008 +0000
+++ b/app/soc/views/site/user/profile.py	Wed Oct 01 14:23:28 2008 +0000
@@ -28,6 +28,7 @@
 from django import newforms as forms
 from django.utils.translation import ugettext_lazy
 
+from soc.logic import common
 from soc.logic import out_of_band
 from soc.logic.site import id_user
 from soc.views import simple
@@ -69,7 +70,7 @@
       # link name not supplied (which is OK), so do not try to validate it
       return None
 
-    if not id_user.isLinkNameFormatValid(link_name):
+    if not common.isLinkNameFormatValid(link_name):
       raise forms.ValidationError('This link name is in wrong format.')
     
     return link_name
@@ -212,7 +213,7 @@
  
   def clean_link_name(self):
     link_name = self.cleaned_data.get('link_name')
-    if not id_user.isLinkNameFormatValid(link_name):
+    if not common.isLinkNameFormatValid(link_name):
       raise forms.ValidationError("This link name is in wrong format.")
     else:
       key_name = self.data.get('key_name')
@@ -364,7 +365,7 @@
   
   def clean_link_name(self):
     link_name = self.cleaned_data.get('link_name')
-    if not id_user.isLinkNameFormatValid(link_name):
+    if not common.isLinkNameFormatValid(link_name):
       raise forms.ValidationError("This link name is in wrong format.")
     else:
       if id_user.doesLinkNameExist(link_name):
--- a/app/soc/views/user/profile.py	Wed Oct 01 09:43:25 2008 +0000
+++ b/app/soc/views/user/profile.py	Wed Oct 01 14:23:28 2008 +0000
@@ -28,6 +28,7 @@
 from django import newforms as forms
 from django.utils.translation import ugettext_lazy
 
+from soc.logic import common
 from soc.logic import out_of_band
 from soc.logic.site import id_user
 from soc.views import simple
@@ -52,7 +53,7 @@
   
   def clean_link_name(self):
     link_name = self.cleaned_data.get('link_name')
-    if not id_user.isLinkNameFormatValid(link_name):
+    if not common.isLinkNameFormatValid(link_name):
       raise forms.ValidationError("This link name is in wrong format.")
     elif not id_user.isLinkNameAvailableForId(link_name):
       raise forms.ValidationError("This link name is already in use.")