Added check_field_is_empty decorator to cleaning.py.
authorLennard de Rijk <ljvderijk@gmail.com>
Thu, 12 Feb 2009 22:52:16 +0000
changeset 1287 9b18d612510a
parent 1286 940f06d34787
child 1288 4ef7de6c2b7a
Added check_field_is_empty decorator to cleaning.py. This makes sure the cleaning checks are skipped when a field is empty. Note that Djanog will raise it's own ValidationError when a required field is empty before attempting to clean it. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed.
app/soc/logic/cleaning.py
--- a/app/soc/logic/cleaning.py	Thu Feb 12 22:20:13 2009 +0000
+++ b/app/soc/logic/cleaning.py	Thu Feb 12 22:52:16 2009 +0000
@@ -39,9 +39,37 @@
     'This link ID is already in use, please specify another one')
 
 
+def check_field_is_empty(field_name):
+  """Returns decorator that bypasses cleaning for empty fields.
+  """
+
+  def decorator(fun):
+    """Decorator that checks if a field is empty if so doesn't do the cleaning.
+
+    Note Django will capture errors concerning required fields that are empty.
+    """
+    from functools import wraps
+
+    @wraps(fun)
+    def wrapper(self):
+      field_content = self.cleaned_data.get(field_name)
+
+      if not field_content:
+        # field has no content so bail out
+        return field_content
+      else:
+        # field has contents
+        return fun(self)
+    return wrapper
+
+  return decorator
+
+
 def clean_link_id(field_name):
   """Checks if the field_name value is in a valid link ID format.
   """
+
+  @check_field_is_empty(field_name)
   def wrapper(self):
     # convert to lowercase for user comfort
     link_id = self.cleaned_data.get(field_name).lower()
@@ -54,6 +82,8 @@
 def clean_scope_path(field_name):
   """Checks if the field_name value is in a valid scope path format.
   """
+
+  @check_field_is_empty(field_name)
   def wrapper(self):
     # convert to lowercase for user comfort
     scope_path = self.cleaned_data.get(field_name).lower()
@@ -68,6 +98,7 @@
      the field_name field false.
   """
 
+  @check_field_is_empty(field_name)
   def wrapper(self):
     agrees_to_tos = self.cleaned_data.get(field_name)
 
@@ -90,6 +121,7 @@
   """Check if the field_name field is a valid user.
   """
 
+  @check_field_is_empty(field_name)
   def wrapped(self):
     link_id = clean_link_id(field_name)(self)
   
@@ -109,6 +141,7 @@
      link id does not exist.
   """ 
 
+  @check_field_is_empty(field_name)
   def wrapped(self):
     link_id = clean_link_id(field_name)(self)
   
@@ -128,6 +161,7 @@
      equal to the current user.
   """
 
+  @check_field_is_empty(field_name)
   def wrapped(self):
     
     clean_user_field = clean_existing_user(field_name)
@@ -146,6 +180,8 @@
 def clean_user_account(field_name):
   """Returns the User with the given field_name value.
   """
+
+  @check_field_is_empty(field_name)
   def wrapped(self):
     email_adress = self.cleaned_data.get(field_name).lower()
 
@@ -161,6 +197,7 @@
      address that hasn't been used for an existing account.
   """ 
 
+  @check_field_is_empty(field_name)
   def wrapped(self):
     email_adress = self.cleaned_data.get(field_name).lower()
 
@@ -194,6 +231,7 @@
   """Clean method for cleaning a field belonging to a LinkProperty.
   """
 
+  @check_field_is_empty(field_name)
   def wrapped(self):
 
     value = self.cleaned_data.get(field_name)
@@ -214,6 +252,7 @@
     -Another User has the given email address as account
     -Another User has the given email address in it's FormerAccounts list
   """
+
   def wrapper(self):
     cleaned_data = self.cleaned_data
 
@@ -251,6 +290,7 @@
     -A application with this link id and scope path already exists
     -A group with this link id and scope path already exists
   """
+
   def wrapper(self):
       cleaned_data = self.cleaned_data