Commits

Luke Plant committed 0bffeed

Fixed bug with bridging from group to single for DateTimeFilter

Comments (0)

Files changed (2)

django_easyfilters/filters.py

             return []
         if len(chosen) == 0:
             chosen_level = 0
+            bridge_to_single = False
         else:
             chosen_level = chosen[-1].range_type.level
+            bridge_to_single = not chosen[-1].range_type.single
 
         # first choice in list can act as template, as it will have all the
         # values we need.
         new_level = new_choice.range_type.level
 
         retval = []
-        while chosen_level < new_level - 1:
-            chosen_level += 1
+        while (chosen_level < new_level - 1) or (chosen_level < new_level and bridge_to_single):
+            # If the first chosen was multi, first bridge just bridges to single
+            if bridge_to_single:
+                bridge_to_single = False
+            else:
+                chosen_level += 1
             if chosen_level > self.max_depth_level:
                 continue
             date_choice = DateChoice(DateRangeType.get(chosen_level, True),

django_easyfilters/tests/filterset.py

         """
         Tests that in the case produced in test_datetime_filter_drill_down_to_choice,
         the remove links display correctly.
-        down to reach it.
         """
         # Two birthdays in Jan 2011
         Person.objects.create(name="Joe", date_of_birth=date(2011, 1, 10))
                           ],
                          [(c.label, c.link_type) for c in choices])
 
+    def test_datetime_filter_bridge_from_multi_to_single(self):
+        """
+        Tests that bridge_choices will bridge from range (multi) choices to
+        single choices.
+        """
+        # Two birthdays in Jan 2011
+        Person.objects.create(name="Joe", date_of_birth=date(2011, 1, 10))
+        Person.objects.create(name="Peter", date_of_birth=date(2011, 1, 20))
+
+        # Chosen year = 2010 - 2011
+        params = MultiValueDict({'date_of_birth':['2010..2011']})
+
+        f = DateTimeFilter('date_of_birth', Person, params)
+        qs = Person.objects.all()
+        qs_filtered = f.apply_filter(qs)
+        choices = f.get_choices(qs_filtered)
+
+        # Expect 2010 - 2011 as remove link
+        self.assertEqual(['2010-2011'], [c.label for c in choices if c.link_type == FILTER_REMOVE])
+        # Expect 2011 and January as display
+        self.assertEqual(['2011', 'January'], [c.label for c in choices if c.link_type == FILTER_DISPLAY])
+        # Expect '10' and '20' as choices
+        self.assertEqual(['10', '20'], [c.label for c in choices if c.link_type == FILTER_ADD])
+
     def test_numericrange_filter_simple_vals(self):
         # If data is less than max_links, we should get a simple list of values.
         filter1 = NumericRangeFilter('price', Book, MultiValueDict(), max_links=20)
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.