skyl avatar skyl committed 30c0feb

added #4, silence_without_field and #5, instructions for testing

Comments (0)

Files changed (6)

 *.orig
 *~
 stuff/
+*.swp
 
 #os files
 .DS_Store
 
 #my files
 db.sqlite
+*.log
+
 
 * Mikhail Korobov <kmike84@gmail.com>
 * Anonymous
+* Skylar Saveland <skylar.saveland@gmail.com>
+
 Source code: https://bitbucket.org/kmike/django-widget-tweaks/
 
 Bug tracker: https://bitbucket.org/kmike/django-widget-tweaks/issues/new
+
+running the tests
+-----------------
+
+::
+
+    export DJANGO_SETTINGS_MODULE=settings
+    django-admin.py test widget_tweaks
+
+DATABASE_ENGINE = 'django.db.backends.sqlite3'
+INSTALLED_APPS = [
+    "widget_tweaks",
+]
+

widget_tweaks/templatetags/widget_tweaks.py

 from django.template import Library
 register = Library()
 
+
+def silence_without_field(fn):
+    def wrapped(field, attr):
+        if not field:
+            return ""
+        else:
+            return fn(field, attr)
+    return wrapped
+
+
 def _process_field_attributes(field, attr, process):
 
     # split attribute name and value from 'attr:value' string
 
     # decorate field.as_widget method with updated attributes
     old_as_widget = field.as_widget
+
     def as_widget(self, widget=None, attrs=None, only_initial=False):
         attrs = attrs or {}
         process(widget or self.field.widget, attrs, attribute, value)
     field.as_widget = bound_method(as_widget, field, field.__class__)
     return field
 
-@register.filter('attr')
+
+@register.filter("attr")
+@silence_without_field
 def set_attr(field, attr):
+
     def process(widget, attrs, attribute, value):
         attrs[attribute] = value
+
     return _process_field_attributes(field, attr, process)
 
-@register.filter
+
+@register.filter("append_attr")
+@silence_without_field
 def append_attr(field, attr):
     def process(widget, attrs, attribute, value):
         if attrs.get(attribute):
             attrs[attribute] = value
     return _process_field_attributes(field, attr, process)
 
-@register.filter
+
+@register.filter("add_class")
+@silence_without_field
 def add_class(field, css_class):
-    return append_attr(field, 'class:'+ css_class)
+    return append_attr(field, 'class:' + css_class)
 
-@register.filter
+
+@register.filter("add_error_class")
+@silence_without_field
 def add_error_class(field, css_class):
     if hasattr(field, 'errors') and field.errors:
         return add_class(field, css_class)
     return field
 
-@register.filter
+
+@register.filter("set_data")
+@silence_without_field
 def set_data(field, data):
     return set_attr(field, 'data-' + data)
 
-@register.filter
+
+@register.filter("behave")
+@silence_without_field
 def behave(field, names):
     ''' https://github.com/anutron/behavior support '''
-    return set_data(field, 'filters:'+names)
+    return set_data(field, 'filters:' + names)
+
+
+

widget_tweaks/tests.py

         res = render_field('simple', 'add_class', 'foo', 'add_class', 'bar')
         assertIn('class="bar foo"', res)
 
+    def test_silence_without_field(self):
+        res = render_field("nothing", 'attr', 'foo:bar')
+        self.assertEquals(res, "")
+        res = render_field("nothing", 'add_class', 'some')
+        self.assertEquals(res, "")
+
 class CustomizedWidgetTest(TestCase):
     def test_attr(self):
         res = render_field('with_attrs', 'attr', 'foo:bar')
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.