offline avatar offline committed 7215d10

autostrip decorator added

Comments (0)

Files changed (2)

annoying/decorators.py

             return response
     return wrapper
 
+
+def autostrip(cls):
+    """
+    strip text fields before validation
+
+    example:
+    class PersonForm(forms.Form):
+        name = forms.CharField(min_length=2, max_length=10)
+        email = forms.EmailField()
+
+    PersonForm = autostrip(PersonForm)
+    
+    #or you can use @autostrip in python >= 2.6
+
+    Author: nail.xx
+    """
+    fields = [(key, value) for key, value in cls.base_fields.iteritems() if isinstance(value, forms.CharField)]
+    for field_name, field_object in fields:
+        def get_clean_func(original_clean):
+            return lambda value: original_clean(value and value.strip())
+        clean_func = get_clean_func(getattr(field_object, 'clean'))
+        setattr(field_object, 'clean', clean_func)
+    return cls
+
 from setuptools import setup, find_packages
 setup(
     name = "django-annoying",
-    version = "0.6.0",
+    version = "0.7.0",
     packages = find_packages(),
     author = "Anderson",
     author_email = "self.anderson@gmail.com",
             - render_to decorator - reduce typing in django views.
             - signals decorator - allow use signals as decorators.
             - ajax_request decorator - returns JsonResponse with this dict as content.
+            - autostrip decoratro - strip text form fields before validation
             - get_object_or_None function - similar to get_object_or_404, but returns None if object not found.
             - get_config function - get settings from django.conf if exists, return default value otherwise.
             - AutoOneToOne field - creates related object on first call if it doesnt exists yet.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.