Commits

Carl Meyer committed d84d4a4

Rename template tag library from form_utils_tags to form_utils.

  • Participants
  • Parent commits 3f53bd5

Comments (0)

Files changed (5)

 tip (unreleased)
 ----------------
 
-- Removed ``FORM_UTILS_MEDIA_URL`` setting and updated to use ``STATIC_URL``
-  rather than ``MEDIA_URL`` throughout. (May be backwards-incompatible.)
+- BACKWARDS-INCOMPATIBLE: Renamed template tag library from ``form_utils_tags``
+  to ``form_utils``.
+
+- BACKWARDS-INCOMPATIBLE: Removed ``FORM_UTILS_MEDIA_URL`` setting and updated
+  to use ``STATIC_URL`` rather than ``MEDIA_URL`` throughout.
 
 - Added "error" class to row_attrs for fields with errors. Thanks Aron
   Griffis.
 ``django-form-utils`` also provides a convenience template filter,
 ``render``.  It is used like this::
 
+    {% load form_utils %}
+
     {{ form|render }}
 
 By default, it will check whether the form is a ``BetterForm``, and if

File form_utils/templatetags/form_utils.py

+"""
+templatetags for django-form-utils
+
+"""
+from __future__ import absolute_import
+
+from django import forms
+from django import template
+from django.template.loader import render_to_string
+
+from form_utils.forms import BetterForm, BetterModelForm
+from form_utils.utils import select_template_from_string
+
+register = template.Library()
+
+@register.filter
+def render(form, template_name=None):
+    """
+    Renders a ``django.forms.Form`` or
+    ``form_utils.forms.BetterForm`` instance using a template.
+
+    The template name(s) may be passed in as the argument to the
+    filter (use commas to separate multiple template names for
+    template selection).
+
+    If not provided, the default template name is
+    ``form_utils/form.html``.
+
+    If the form object to be rendered is an instance of
+    ``form_utils.forms.BetterForm`` or
+    ``form_utils.forms.BetterModelForm``, the template
+    ``form_utils/better_form.html`` will be used instead if present.
+
+    """
+    default = 'form_utils/form.html'
+    if isinstance(form, (BetterForm, BetterModelForm)):
+        default = ','.join(['form_utils/better_form.html', default])
+    tpl = select_template_from_string(template_name or default)
+
+    return tpl.render(template.Context({'form': form}))
+
+
+
+
+@register.filter
+def placeholder(boundfield, value):
+    """Set placeholder attribute for given boundfield."""
+    boundfield.field.widget.attrs["placeholder"] = value
+    return boundfield
+
+
+
+@register.filter
+def label(boundfield, contents=None):
+    """Render label tag for a boundfield, optionally with given contents."""
+    label_text = contents or boundfield.label
+    id_ = boundfield.field.widget.attrs.get('id') or boundfield.auto_id
+
+    return render_to_string(
+        "forms/_label.html",
+        {
+            "label_text": label_text,
+            "id": id_,
+            "field": boundfield,
+            })
+
+
+
+@register.filter
+def label_text(boundfield):
+    """Return the default label text for the given boundfield."""
+    return boundfield.label
+
+
+
+@register.filter
+def value_text(boundfield):
+    """Return the value for given boundfield as human-readable text."""
+    val = boundfield.value()
+    # If choices is set, use the display label
+    return unicode(
+        dict(getattr(boundfield.field, "choices", [])).get(val, val))
+
+
+
+@register.filter
+def values_text(boundfield):
+    """Return the values for given multiple-select as human-readable text."""
+    val = boundfield.value()
+    # If choices is set, use the display label
+    choice_dict = dict(getattr(boundfield.field, "choices", []))
+    return [unicode(choice_dict.get(v, v)) for v in val]
+
+
+
+@register.filter
+def classes(boundfield, classes):
+    """Append given classes to the widget attrs of given boundfield."""
+    attrs = boundfield.field.widget.attrs
+    attrs["class"] = " ".join(
+        [c for c in [attrs.get("class", None), classes] if c])
+    return boundfield
+
+
+
+@register.filter
+def optional(boundfield):
+    """Return True if given boundfield is optional, else False."""
+    return not boundfield.field.required
+
+
+
+@register.filter
+def is_checkbox(boundfield):
+    """Return True if this field's widget is a CheckboxInput."""
+    return isinstance(
+        boundfield.field.widget, forms.CheckboxInput)
+
+
+
+@register.filter
+def is_multiple(boundfield):
+    """Return True if this field has multiple values."""
+    return isinstance(boundfield.field.widget, forms.SelectMultiple)

File form_utils/templatetags/form_utils_tags.py

-"""
-templatetags for django-form-utils
-
-Time-stamp: <2009-03-26 12:32:08 carljm form_utils_tags.py>
-
-"""
-from django import forms
-from django import template
-from django.template.loader import render_to_string
-
-from form_utils.forms import BetterForm, BetterModelForm
-from form_utils.utils import select_template_from_string
-
-register = template.Library()
-
-@register.filter
-def render(form, template_name=None):
-    """
-    Renders a ``django.forms.Form`` or
-    ``form_utils.forms.BetterForm`` instance using a template.
-
-    The template name(s) may be passed in as the argument to the
-    filter (use commas to separate multiple template names for
-    template selection).
-
-    If not provided, the default template name is
-    ``form_utils/form.html``.
-
-    If the form object to be rendered is an instance of
-    ``form_utils.forms.BetterForm`` or
-    ``form_utils.forms.BetterModelForm``, the template
-    ``form_utils/better_form.html`` will be used instead if present.
-
-    """
-    default = 'form_utils/form.html'
-    if isinstance(form, (BetterForm, BetterModelForm)):
-        default = ','.join(['form_utils/better_form.html', default])
-    tpl = select_template_from_string(template_name or default)
-
-    return tpl.render(template.Context({'form': form}))
-
-
-
-
-@register.filter
-def placeholder(boundfield, value):
-    """Set placeholder attribute for given boundfield."""
-    boundfield.field.widget.attrs["placeholder"] = value
-    return boundfield
-
-
-
-@register.filter
-def label(boundfield, contents=None):
-    """Render label tag for a boundfield, optionally with given contents."""
-    label_text = contents or boundfield.label
-    id_ = boundfield.field.widget.attrs.get('id') or boundfield.auto_id
-
-    return render_to_string(
-        "forms/_label.html",
-        {
-            "label_text": label_text,
-            "id": id_,
-            "field": boundfield,
-            })
-
-
-
-@register.filter
-def label_text(boundfield):
-    """Return the default label text for the given boundfield."""
-    return boundfield.label
-
-
-
-@register.filter
-def value_text(boundfield):
-    """Return the value for given boundfield as human-readable text."""
-    val = boundfield.value()
-    # If choices is set, use the display label
-    return unicode(
-        dict(getattr(boundfield.field, "choices", [])).get(val, val))
-
-
-
-@register.filter
-def values_text(boundfield):
-    """Return the values for given multiple-select as human-readable text."""
-    val = boundfield.value()
-    # If choices is set, use the display label
-    choice_dict = dict(getattr(boundfield.field, "choices", []))
-    return [unicode(choice_dict.get(v, v)) for v in val]
-
-
-
-@register.filter
-def classes(boundfield, classes):
-    """Append given classes to the widget attrs of given boundfield."""
-    attrs = boundfield.field.widget.attrs
-    attrs["class"] = " ".join(
-        [c for c in [attrs.get("class", None), classes] if c])
-    return boundfield
-
-
-
-@register.filter
-def optional(boundfield):
-    """Return True if given boundfield is optional, else False."""
-    return not boundfield.field.required
-
-
-
-@register.filter
-def is_checkbox(boundfield):
-    """Return True if this field's widget is a CheckboxInput."""
-    return isinstance(
-        boundfield.field.widget, forms.CheckboxInput)
-
-
-
-@register.filter
-def is_multiple(boundfield):
-    """Return True if this field has multiple values."""
-    return isinstance(boundfield.field.widget, forms.SelectMultiple)

File tests/tests.py

 
         """
         form = BoringForm()
-        tpl = template.Template('{% load form_utils_tags %}{{ form|render }}')
+        tpl = template.Template('{% load form_utils %}{{ form|render }}')
         html = tpl.render(template.Context({'form': form}))
         self.assertEqual([l.strip() for l in html.splitlines() if l.strip()],
                           self.boring_form_html)
 
         """
         form = ApplicationForm()
-        tpl = template.Template('{% load form_utils_tags %}{{ form|render }}')
+        tpl = template.Template('{% load form_utils %}{{ form|render }}')
         html = tpl.render(template.Context({'form': form}))
         self.assertEqual([l.strip() for l in html.splitlines() if l.strip()],
                           self.betterform_html)
 class FieldFilterTests(TestCase):
     """Tests for form field filters."""
     @property
-    def form_utils_tags(self):
+    def form_utils(self):
         """The module under test."""
-        from form_utils.templatetags import form_utils_tags
-        return form_utils_tags
+        from form_utils.templatetags import form_utils
+        return form_utils
 
 
     @property
 
     def test_placeholder(self):
         """``placeholder`` filter sets placeholder attribute."""
-        bf = self.form_utils_tags.placeholder(self.form()["name"], "Placeholder")
+        bf = self.form_utils.placeholder(self.form()["name"], "Placeholder")
         self.assertIn('placeholder="Placeholder"', unicode(bf))
 
 
-    @patch("form_utils.templatetags.form_utils_tags.render_to_string")
+    @patch("form_utils.templatetags.form_utils.render_to_string")
     def test_label(self, render_to_string):
         """``label`` filter renders field label from template."""
         render_to_string.return_value = "<label>something</label>"
         bf = self.form()["name"]
 
-        label = self.form_utils_tags.label(bf)
+        label = self.form_utils.label(bf)
 
         self.assertEqual(label, "<label>something</label>")
         render_to_string.assert_called_with(
             )
 
 
-    @patch("form_utils.templatetags.form_utils_tags.render_to_string")
+    @patch("form_utils.templatetags.form_utils.render_to_string")
     def test_label_override(self, render_to_string):
         """label filter allows overriding the label text."""
         bf = self.form()["name"]
 
-        self.form_utils_tags.label(bf, "override")
+        self.form_utils.label(bf, "override")
 
         render_to_string.assert_called_with(
             "forms/_label.html",
 
     def test_label_text(self):
         """``label_text`` filter returns field's default label text."""
-        self.assertEqual(self.form_utils_tags.label_text(self.form()["name"]), "Name")
+        self.assertEqual(self.form_utils.label_text(self.form()["name"]), "Name")
 
 
     def test_value_text(self):
         """``value_text`` filter returns value of field."""
         self.assertEqual(
-            self.form_utils_tags.value_text(self.form({"name": "boo"})["name"]), "boo")
+            self.form_utils.value_text(self.form({"name": "boo"})["name"]), "boo")
 
 
     def test_value_text_unbound(self):
         """``value_text`` filter returns default value of unbound field."""
-        self.assertEqual(self.form_utils_tags.value_text(self.form()["name"]), "none")
+        self.assertEqual(self.form_utils.value_text(self.form()["name"]), "none")
 
 
     def test_value_text_choices(self):
         """``value_text`` filter returns human-readable value of choicefield."""
         self.assertEqual(
-            self.form_utils_tags.value_text(
+            self.form_utils.value_text(
                 self.form({"level": "a"})["level"]), "Advanced")
 
 
         f = self.form({"level": ["a", "b"]})
 
         self.assertEqual(
-            self.form_utils_tags.values_text(f["level"]), ["Advanced", "Beginner"])
+            self.form_utils.values_text(f["level"]), ["Advanced", "Beginner"])
 
 
     def test_optional_false(self):
         """A required field should not be marked optional."""
-        self.assertFalse(self.form_utils_tags.optional(self.form()["name"]))
+        self.assertFalse(self.form_utils.optional(self.form()["name"]))
 
 
     def test_optional_true(self):
         """A non-required field should be marked optional."""
-        self.assertTrue(self.form_utils_tags.optional(self.form()["level"]))
+        self.assertTrue(self.form_utils.optional(self.form()["level"]))
 
 
     def test_detect_checkbox(self):
         """``is_checkbox`` detects checkboxes."""
         f = self.form()
 
-        self.assertTrue(self.form_utils_tags.is_checkbox(f["awesome"]))
+        self.assertTrue(self.form_utils.is_checkbox(f["awesome"]))
 
 
     def test_detect_non_checkbox(self):
         """``is_checkbox`` detects that select fields are not checkboxes."""
         f = self.form()
 
-        self.assertFalse(self.form_utils_tags.is_checkbox(f["level"]))
+        self.assertFalse(self.form_utils.is_checkbox(f["level"]))
 
 
     def test_is_multiple(self):
         f = self.form()
         f.fields["level"].widget = forms.SelectMultiple()
 
-        self.assertTrue(self.form_utils_tags.is_multiple(f["level"]))
+        self.assertTrue(self.form_utils.is_multiple(f["level"]))
 
 
     def test_is_not_multiple(self):
         """`is_multiple` detects a non-multiple widget."""
         f = self.form()
 
-        self.assertFalse(self.form_utils_tags.is_multiple(f["level"]))
+        self.assertFalse(self.form_utils.is_multiple(f["level"]))