Luke Plant avatar Luke Plant committed a2ab3ec

Added ability to specify custom labels in NumericRangeFilter.ranges

Comments (0)

Files changed (2)

django_easyfilters/filters.py

         self.choice_type = make_numeric_range_choice(field_obj.to_python, str)
         super(NumericRangeFilter, self).__init__(field, model, params, **kwargs)
 
+    def display_choice(self, c):
+        if self.ranges is None:
+            return c.display()
+        if len(c.values) == 1:
+            return c.display()
+        for r in self.ranges:
+            if r[0] == c.values[0].value and r[1] == c.values[1].value:
+                # found a match in ranges
+                if len(r) > 2:
+                    # found a label, so use it
+                    return r[2]
+                else:
+                    return c.display()
+        return c.display()
+
     def get_choices_add(self, qs):
         chosen = list(self.chosen)
         range_type = None
             val_counts = value_counts(qs, self.field)
             for v, count in val_counts.items():
                 choice = self.choice_type([RangeEnd(v, True)])
-                choices.append(FilterChoice(choice.display(),
+                choices.append(FilterChoice(self.display_choice(choice),
                                             count,
                                             self.build_params(add=choice),
                                             FILTER_ADD))
                 # the first will include 10 and 20, the second will exlude 20.
                 choice = self.choice_type([RangeEnd(vals[0], i == 0),
                                            RangeEnd(vals[1], True)])
-                choices.append(FilterChoice(choice.display(),
+                choices.append(FilterChoice(self.display_choice(choice),
                                             count,
                                             self.build_params(add=choice),
                                             FILTER_ADD))

django_easyfilters/tests/filterset.py

         self.assertEqual(choices[0].count, qs.filter(price__gte=Decimal('3.50'), price__lte=Decimal('5.00')).count())
         self.assertEqual(choices[1].count, qs.filter(price__gt=Decimal('5.00'), price__lte=Decimal('6.00')).count())
 
+    def test_numericrange_filter_manual_ranges_labels(self):
+        """
+        Test we can specify 'ranges' with manual labels
+        """
+        qs = Book.objects.all()
+
+        ranges = [(Decimal('1.00'), Decimal('4.00'), "$4 or less"),
+                  (Decimal('4.00'), Decimal('6.00'), "$4.00 - $6.00"),
+                  (Decimal('6.00'), Decimal('100.00'), "More than $6")]
+
+        filter1 = NumericRangeFilter('price', Book, MultiValueDict(), ranges=ranges)
+        choices = filter1.get_choices(qs)
+        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):
         # We shouldn't get drilldown if ranges is specified manually.
 
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.