Commits

Mark Lavin committed b20d8cd

Allow passing query parameters in widget __init__. See #42.

  • Participants
  • Parent commits 182b886

Comments (0)

Files changed (2)

selectable/forms/widgets.py

     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
         self.allow_new = kwargs.pop('allow_new', False)
-        self.qs = {}
+        self.qs = kwargs.pop('query_params', {})
         self.limit = kwargs.pop('limit', None)
         super(AutoCompleteWidget, self).__init__(*args, **kwargs)
 
         self.lookup_class = lookup_class
         self.allow_new = kwargs.pop('allow_new', False)
         self.limit = kwargs.pop('limit', None)
+        query_params = kwargs.pop('query_params', {})
         widgets = [
-            AutoCompleteWidget(lookup_class, allow_new=self.allow_new, limit=self.limit),
+            AutoCompleteWidget(
+                lookup_class, allow_new=self.allow_new,
+                limit=self.limit, query_params=query_params
+            ),
             forms.HiddenInput(attrs={u'data-selectable-type': 'hidden'})
         ]
         super(AutoCompleteSelectWidget, self).__init__(widgets, *args, **kwargs)
         self.lookup_class = lookup_class
         self.allow_new = kwargs.pop('allow_new', False)
         self.limit = kwargs.pop('limit', None)
+        query_params = kwargs.pop('query_params', {})
         widgets = [
-            AutoComboboxWidget(lookup_class, allow_new=self.allow_new, limit=self.limit),
+            AutoComboboxWidget(
+                lookup_class, allow_new=self.allow_new,
+                limit=self.limit, query_params=query_params
+            ),
             forms.HiddenInput(attrs={u'data-selectable-type': 'hidden'})
         ]
         super(AutoComboboxSelectWidget, self).__init__(widgets, *args, **kwargs)
             u'data-selectable-multiple': 'true',
             u'data-selectable-position': position
         }
+        query_params = kwargs.pop('query_params', {})
         widgets = [
-            AutoCompleteWidget(lookup_class, allow_new=False, limit=self.limit, attrs=attrs),
+            AutoCompleteWidget(
+                lookup_class, allow_new=False,
+                limit=self.limit, query_params=query_params, attrs=attrs
+            ),
             LookupMultipleHiddenInput(lookup_class)
         ]
         super(AutoCompleteSelectMultipleWidget, self).__init__(widgets, *args, **kwargs)
             u'data-selectable-multiple': 'true',
             u'data-selectable-position': position
         }
+        query_params = kwargs.pop('query_params', {})
         widgets = [
-            AutoComboboxWidget(lookup_class, allow_new=False, limit=self.limit, attrs=attrs),
+            AutoComboboxWidget(
+                lookup_class, allow_new=False,
+                limit=self.limit, query_params=query_params, attrs=attrs
+            ),
             LookupMultipleHiddenInput(lookup_class)
         ]
         super(AutoComboboxSelectMultipleWidget, self).__init__(widgets, *args, **kwargs)

selectable/tests/widgets.py

     widget_cls = None
     lookup_cls = None
 
-    def get_widget_instance(self, limit=None):
-        return self.__class__.widget_cls(self.__class__.lookup_cls, limit=limit)
+    def get_widget_instance(self, **kwargs):
+        return self.__class__.widget_cls(self.__class__.lookup_cls, **kwargs)
 
     def test_init(self):
         widget = self.get_widget_instance()
         query = parse.query
         self.assertTrue('limit=10' in query)
 
+    def test_initial_query_parameters(self):
+        params = {'active': 1} 
+        widget = self.get_widget_instance(query_params=params)
+        attrs = widget.build_attrs()
+        url = attrs['data-selectable-url']
+        parse = urlparse(url)
+        query = parse.query
+        self.assertEqual(query, urlencode(params))
+
 
 class AutoCompleteSelectWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoCompleteSelectWidget
         query = parse.query
         self.assertTrue('limit=10' in query)
 
+    def test_initial_query_parameters(self):
+        params = {'active': 1} 
+        widget = self.get_widget_instance(query_params=params)
+        sub_widget = widget.widgets[0]
+        attrs = sub_widget.build_attrs()
+        url = attrs['data-selectable-url']
+        parse = urlparse(url)
+        query = parse.query
+        self.assertEqual(query, urlencode(params))
+
 
 class AutoComboboxWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoComboboxWidget
         query = parse.query
         self.assertTrue('limit=10' in query)
 
+    def test_initial_query_parameters(self):
+        params = {'active': 1} 
+        widget = self.get_widget_instance(query_params=params)
+        attrs = widget.build_attrs()
+        url = attrs['data-selectable-url']
+        parse = urlparse(url)
+        query = parse.query
+        self.assertEqual(query, urlencode(params))
+
 
 class AutoComboboxSelectWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoComboboxSelectWidget
         query = parse.query
         self.assertTrue('limit=10' in query)
 
+    def test_initial_query_parameters(self):
+        params = {'active': 1} 
+        widget = self.get_widget_instance(query_params=params)
+        sub_widget = widget.widgets[0]
+        attrs = sub_widget.build_attrs()
+        url = attrs['data-selectable-url']
+        parse = urlparse(url)
+        query = parse.query
+        self.assertEqual(query, urlencode(params))
+
 
 class AutoCompleteSelectMultipleWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoCompleteSelectMultipleWidget
         query = parse.query
         self.assertTrue('limit=10' in query)
 
+    def test_initial_query_parameters(self):
+        params = {'active': 1} 
+        widget = self.get_widget_instance(query_params=params)
+        sub_widget = widget.widgets[0]
+        attrs = sub_widget.build_attrs()
+        url = attrs['data-selectable-url']
+        parse = urlparse(url)
+        query = parse.query
+        self.assertEqual(query, urlencode(params))
+
 
 class AutoComboboxSelectMultipleWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoComboboxSelectMultipleWidget
         query = parse.query
         self.assertTrue('limit=10' in query)
 
+    def test_initial_query_parameters(self):
+        params = {'active': 1} 
+        widget = self.get_widget_instance(query_params=params)
+        sub_widget = widget.widgets[0]
+        attrs = sub_widget.build_attrs()
+        url = attrs['data-selectable-url']
+        parse = urlparse(url)
+        query = parse.query
+        self.assertEqual(query, urlencode(params))
+