Skip to content

Commit

Permalink
Merge pull request carltongibson#116 from augonis/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo13 committed Nov 2, 2013
2 parents e58e15e + eefabc0 commit 8c0cdfa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
8 changes: 6 additions & 2 deletions django_filters/fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import absolute_import
from __future__ import unicode_literals

from collections import namedtuple

from django import forms

from .widgets import RangeWidget, LookupTypeWidget
Expand All @@ -21,7 +23,7 @@ def compress(self, data_list):
return slice(*data_list)
return None


Lookup = namedtuple('Lookup', ('value', 'lookup_type'))
class LookupTypeField(forms.MultiValueField):
def __init__(self, field, lookup_choices, *args, **kwargs):
fields = (
Expand All @@ -36,4 +38,6 @@ def __init__(self, field, lookup_choices, *args, **kwargs):
super(LookupTypeField, self).__init__(fields, *args, **kwargs)

def compress(self, data_list):
return data_list
if len(data_list)==2:
return Lookup(value=data_list[0], lookup_type=data_list[1] or 'exact')
return Lookup(value=None, lookup_type='exact')
10 changes: 4 additions & 6 deletions django_filters/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _

from .fields import RangeField, LookupTypeField
from .fields import RangeField, LookupTypeField, Lookup


__all__ = [
Expand Down Expand Up @@ -63,11 +63,9 @@ def field(self):
return self._field

def filter(self, qs, value):
if isinstance(value, (list, tuple)):
lookup = six.text_type(value[1])
if not lookup:
lookup = 'exact' # fallback to exact if lookup is not provided
value = value[0]
if isinstance(value, Lookup):
lookup = six.text_type(value.lookup_type)
value = value.value
else:
lookup = self.lookup_type
if value in ([], (), {}, None, ''):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django_filters.widgets import RangeWidget
from django_filters.fields import RangeField
from django_filters.fields import LookupTypeField

from django_filters.fields import Lookup

def to_d(float_value):
return decimal.Decimal('%.2f' % float_value)
Expand Down Expand Up @@ -44,7 +44,7 @@ def test_clean(self):
f = LookupTypeField(inner, [('gt', 'gt'), ('lt', 'lt')])
self.assertEqual(
f.clean(['12.34', 'lt']),
[to_d(12.34), 'lt'])
Lookup(to_d(12.34), 'lt'))

@unittest.skipIf(django.VERSION >= (1, 6),
'Django 1.6 uses html5 fields')
Expand Down
17 changes: 9 additions & 8 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.utils import unittest
from django.test import TestCase

from django_filters.fields import Lookup
from django_filters.fields import RangeField
from django_filters.fields import LookupTypeField
from django_filters.filters import Filter
Expand Down Expand Up @@ -116,22 +117,23 @@ def test_filtering_skipped_with_none_value(self):

def test_filtering_with_list_value(self):
qs = mock.Mock(spec=['filter'])
f = Filter(name='somefield')
result = f.filter(qs, ['value', 'some_lookup_type'])
f = Filter(name='somefield', lookup_type=['some_lookup_type'])
result = f.filter(qs, Lookup('value', 'some_lookup_type'))
qs.filter.assert_called_once_with(somefield__some_lookup_type='value')
self.assertNotEqual(qs, result)

def test_filtering_skipped_with_list_value_with_blank(self):
qs = mock.Mock()
f = Filter(name='somefield')
result = f.filter(qs, ['', 'some_lookup_type'])
f = Filter(name='somefield', lookup_type=['some_lookup_type'])
result = f.filter(qs, Lookup('', 'some_lookup_type'))
self.assertListEqual(qs.method_calls, [])
self.assertEqual(qs, result)

def test_filtering_skipped_with_list_value_with_blank_lookup(self):
return # Now field is required to provide valid lookup_type if it provides any
qs = mock.Mock(spec=['filter'])
f = Filter(name='somefield')
result = f.filter(qs, ['value', ''])
f = Filter(name='somefield', lookup_type=None)
result = f.filter(qs, Lookup('value', ''))
qs.filter.assert_called_once_with(somefield__exact='value')
self.assertNotEqual(qs, result)

Expand Down Expand Up @@ -441,5 +443,4 @@ def test_default_field_with_assigning_model(self):
f = AllValuesFilter()
f.model = mocked
field = f.field
self.assertIsInstance(field, forms.ChoiceField)

self.assertIsInstance(field, forms.ChoiceField)

0 comments on commit 8c0cdfa

Please sign in to comment.