Commits

Luke Plant  committed 10a2a78

Added test for actual filtering of ForeignKey fields.

This also changed the API of Filter to make it more testable, and generally
more symmetrical.

  • Participants
  • Parent commits 892a8ef

Comments (0)

Files changed (2)

File django_easyfilters/filterset.py

 from django.utils.http import urlencode
 from django.utils.text import capfirst
 
-standard_filter_mapping = {
-}
-
-
-FilterChoice = namedtuple('FilterChoice', 'label count url')
+FilterChoice = namedtuple('FilterChoice', 'label count params')
 
 
 class FilterOptions(object):
         else:
             return qs.filter(**{self.field: p_val})
 
-    def build_query_string(self, params, filter_val):
+    def build_params(self, params, filter_val):
         params = params.copy()
         params[self.query_param] = filter_val
         params.pop('page', None) # links should reset paging
-        return '?%s' % urlencode(params)
+        return params
 
     def get_choices(self, qs, params):
         """
             id = getattr(o, rel_field.attname)
             choices.append(FilterChoice(unicode(o),
                                         count_dict[id],
-                                        self.build_query_string(params, id)))
+                                        self.build_params(params, id)))
         return choices
 
     def get_remove_url(self, params, request=None):
         field_obj = qs.model._meta.get_field(self.field)
         label = capfirst(field_obj.verbose_name)
         for c in self.get_choices(qs, params):
-            out.append(u'<a href="%s">%s</a> (%d) &nbsp;&nbsp;' % (escape(c.url), escape(c.label), c.count))
+            out.append(u'<a href="%s">%s</a> (%d) &nbsp;&nbsp;' % (escape('?%s' % urlencode(c.params)), escape(c.label), c.count))
         return u'<div>%s: %s</div>' % (escape(label), u''.join(out))
 
 

File django_easyfilters/tests/filterset.py

         self.assertTrue(reached[0])
         self.assertTrue(reached[1])
 
+    def test_foreignkey_params_produced(self):
+        """
+        A ForeignKey filter shoud produce params that cause the query to be
+        limited by that filter.
+        """
+        class BookFilterSet(FilterSet):
+            fields = [
+                'genre',
+                ]
+
+        qs = Book.objects.all()
+        data = {}
+        fs = BookFilterSet(qs, data)
+        choices = fs.filters[0].get_choices(qs, data)
+
+        # If we use the params from e.g. the first choice, that should produce a
+        # filtered qs when fed back in (i.e. when we 'click' on that option we
+        # should get a filter on it).
+        reached = False
+        for choice in choices:
+            reached = True
+            fs_filtered = BookFilterSet(qs, choice.params)
+            qs_filtered = fs_filtered.qs
+            self.assertEqual(len(qs_filtered), choice.count)
+            for book in qs_filtered:
+                self.assertEqual(unicode(book.genre), choice.label)
+        self.assertTrue(reached)
+
+
     def test_filterset_render(self):
         """
         Smoke test to ensure that filtersets can be rendered