Skip to content

Commit

Permalink
Merge pull request #270 from scott-w/boolean_widget
Browse files Browse the repository at this point in the history
Add BooleanWidget for JS `true`/`false` compatibility
  • Loading branch information
Carlton Gibson committed Sep 2, 2015
2 parents 2f77f7a + 51b7dab commit 375c7e5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
20 changes: 20 additions & 0 deletions django_filters/widgets.py
Expand Up @@ -97,3 +97,23 @@ def decompress(self, value):
if value is None:
return [None, None]
return value


class BooleanWidget(forms.Widget):
"""Convert true/false values into the internal Python True/False.
This can be used for AJAX queries that pass true/false from JavaScript's
internal types through.
"""
def value_from_datadict(self, data, files, name):
"""
"""
value = super(BooleanWidget, self).value_from_datadict(
data, files, name)

if value is not None:
if value.lower() == 'true':
value = True
elif value.lower() == 'false':
value = False

return value
13 changes: 11 additions & 2 deletions docs/ref/widgets.txt
Expand Up @@ -8,12 +8,21 @@ arguments.
~~~~~~~~~~~~~~

This widget renders each option as a link, instead of an actual <input>. It has
one method that you can overide for additional customizability.
one method that you can override for additional customizability.
``option_string()`` should return a string with 3 Python keyword argument
placeholders::

1. ``attrs``: This is a string with all the attributes that will be on the
final ``<a>`` tag.
2. ``query_string``: This is the query string for use in the ``href``
option on the ``<a>`` elemeent.
option on the ``<a>`` element.
3. ``label``: This is the text to be displayed to the user.

``BooleanWidget``
~~~~~~~~~~~~~~~~~

This widget converts its input into Python's True/False values. It will convert
all case variations of ``True`` and ``False`` into the internal Python values.
To use it, pass this into the ``widgets`` argument of the ``BooleanFilter``::

active = BooleanFilter(widget=BooleanWidget())
20 changes: 20 additions & 0 deletions tests/test_widgets.py
Expand Up @@ -4,6 +4,7 @@
from django.test import TestCase
from django.forms import TextInput, Select

from django_filters.widgets import BooleanWidget
from django_filters.widgets import RangeWidget
from django_filters.widgets import LinkWidget
from django_filters.widgets import LookupTypeWidget
Expand Down Expand Up @@ -135,3 +136,22 @@ def test_widget(self):
-
<input type="text" name="price_1" value="9.99" />""")


class BooleanWidgetTests(TestCase):
"""
"""
def test_widget_value_from_datadict(self):
"""
"""
w = BooleanWidget()

trueActive = {'active': 'true'}
result = w.value_from_datadict(trueActive, {}, 'active')
self.assertEqual(result, True)

falseActive = {'active': 'false'}
result = w.value_from_datadict(falseActive, {}, 'active')
self.assertEqual(result, False)

result = w.value_from_datadict({}, {}, 'active')
self.assertEqual(result, None)

0 comments on commit 375c7e5

Please sign in to comment.