Commits

Luke Plant committed 6b9672b

Explicit 'drilldown' option for NumericRangeFilter

Comments (0)

Files changed (2)

django_easyfilters/filters.py

 
     def __init__(self, field, model, params, **kwargs):
         self.max_links = kwargs.pop('max_links', 5)
+        self.drilldown = kwargs.pop('drilldown', True)
         self.ranges = kwargs.pop('ranges', None)
         field_obj = model._meta.get_field(field)
         self.choice_type = make_numeric_range_choice(field_obj.to_python, str)
         chosen = list(self.chosen)
         range_type = None
 
-        if self.ranges is not None and len(chosen) > 0:
+        if not self.drilldown and len(chosen) > 0:
             return []
 
         all_vals = qs.values_list(self.field).distinct()
         num = all_vals.count()
 
         choices = []
-        if num <= self.max_links and self.ranges is None:
+        if num <= self.max_links:
             val_counts = value_counts(qs, self.field)
             for v, count in val_counts.items():
                 choice = self.choice_type([RangeEnd(v, True)])

django_easyfilters/tests/filterset.py

         self.assertEqual(choices[0].label, "$4 or less")
         self.assertEqual(choices[1].label, "$4.00 - $6.00")
 
-    def test_numericrange_filter_manual_ranges_no_drill_down(self):
+    def test_numericrange_filter_drilldown(self):
+        # Can specify to turn off drilldown
         # We shouldn't get drilldown if ranges is specified manually.
 
-        ranges = [(Decimal('3.50'), Decimal('5.00')),
-                  (Decimal('5.00'), Decimal('6.00'))]
         params1 = MultiValueDict({'price': ['3.50i..5.00i']})
-        filter1 = NumericRangeFilter('price', Book, params1, ranges=ranges)
+        filter1 = NumericRangeFilter('price', Book, params1, drilldown=False)
 
         qs = Book.objects.all()
         qs_filtered1 = filter1.apply_filter(qs)