Commits

Mark Lavin committed 5817d61

Field/widget hooks for result limiting.

  • Participants
  • Parent commits 56deac2

Comments (0)

Files changed (4)

selectable/forms/fields.py

     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
         self.allow_new = kwargs.pop('allow_new', False)
-        kwargs['widget'] = self.widget(lookup_class, allow_new=self.allow_new)
+        self.limit = kwargs.pop('limit', None)
+        kwargs['widget'] = self.widget(lookup_class, allow_new=self.allow_new, limit=self.limit)
         super(AutoCompleteSelectField, self).__init__(*args, **kwargs)
 
 
 
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
-        kwargs['widget'] = self.widget(lookup_class)
+        self.limit = kwargs.pop('limit', None)
+        kwargs['widget'] = self.widget(lookup_class, limit=self.limit)
         super(AutoCompleteSelectMultipleField, self).__init__(*args, **kwargs)
 
     def to_python(self, value):

selectable/forms/widgets.py

         self.lookup_class = lookup_class
         self.allow_new = kwargs.pop('allow_new', False)
         self.qs = {}
+        self.limit = kwargs.pop('limit', None)
         super(AutoCompleteWidget, self).__init__(*args, **kwargs)
 
     def update_query_parameters(self, qs_dict):
     def build_attrs(self, extra_attrs=None, **kwargs):
         attrs = super(AutoCompleteWidget, self).build_attrs(extra_attrs, **kwargs)
         url = self.lookup_class.url()
+        if self.limit and 'limit' not in self.qs:
+            self.qs['limit'] = self.limit
         if self.qs:
             url = '%s?%s' % (url, urlencode(self.qs))
         attrs[u'data-selectable-url'] = url
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
         self.allow_new = kwargs.pop('allow_new', False)
+        self.limit = kwargs.pop('limit', None)
         widgets = [
-            AutoCompleteWidget(lookup_class, allow_new=self.allow_new),
+            AutoCompleteWidget(lookup_class, allow_new=self.allow_new, limit=self.limit),
             forms.HiddenInput(attrs={u'data-selectable-type': 'hidden'})
         ]
         super(AutoCompleteSelectWidget, self).__init__(widgets, *args, **kwargs)
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
         self.allow_new = kwargs.pop('allow_new', False)
+        self.limit = kwargs.pop('limit', None)
         widgets = [
-            AutoComboboxWidget(lookup_class, allow_new=self.allow_new),
+            AutoComboboxWidget(lookup_class, allow_new=self.allow_new, limit=self.limit),
             forms.HiddenInput(attrs={u'data-selectable-type': 'hidden'})
         ]
         super(AutoComboboxSelectWidget, self).__init__(widgets, *args, **kwargs)
 
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
+        self.limit = kwargs.pop('limit', None)
         position = kwargs.pop('position', 'bottom')
         attrs = {
             u'data-selectable-multiple': 'true',
             u'data-selectable-position': position
         }
         widgets = [
-            AutoCompleteWidget(lookup_class, allow_new=False, attrs=attrs),
+            AutoCompleteWidget(lookup_class, allow_new=False, limit=self.limit, attrs=attrs),
             LookupMultipleHiddenInput(lookup_class)
         ]
         super(AutoCompleteSelectMultipleWidget, self).__init__(widgets, *args, **kwargs)
 
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
+        self.limit = kwargs.pop('limit', None)
         position = kwargs.pop('position', 'bottom')
         attrs = {
             u'data-selectable-multiple': 'true',
             u'data-selectable-position': position
         }
         widgets = [
-            AutoComboboxWidget(lookup_class, allow_new=False, attrs=attrs),
+            AutoComboboxWidget(lookup_class, allow_new=False, limit=self.limit, attrs=attrs),
             LookupMultipleHiddenInput(lookup_class)
         ]
         super(AutoComboboxSelectMultipleWidget, self).__init__(widgets, *args, **kwargs)

selectable/tests/fields.py

     field_cls = None
     lookup_cls = None
 
-    def get_field_instance(self, allow_new=False):
-        return self.__class__.field_cls(self.__class__.lookup_cls, allow_new=allow_new)
+    def get_field_instance(self, allow_new=False, limit=None):
+        return self.__class__.field_cls(self.__class__.lookup_cls, allow_new=allow_new, limit=limit)
 
     def test_init(self):
         field = self.get_field_instance()
         self.assertEqual(field.lookup_class, self.__class__.lookup_cls)
 
+    def test_init_with_limit(self):
+        field = self.get_field_instance(limit=10)
+        self.assertEqual(field.limit, 10)
+        self.assertEqual(field.widget.limit, 10)
+
     def test_clean(self):
         self.fail('This test has not yet been written')
 
     field_cls = fields.AutoCompleteSelectMultipleField
     lookup_cls = ThingLookup
 
-    def get_field_instance(self):
-        return self.__class__.field_cls(self.__class__.lookup_cls)
+    def get_field_instance(self, limit=None):
+        return self.__class__.field_cls(self.__class__.lookup_cls, limit=limit)
 
     def test_clean(self):
         thing = self.create_thing()
     field_cls = fields.AutoComboboxSelectMultipleField
     lookup_cls = ThingLookup
 
-    def get_field_instance(self):
-        return self.__class__.field_cls(self.__class__.lookup_cls)
+    def get_field_instance(self, limit=None):
+        return self.__class__.field_cls(self.__class__.lookup_cls, limit=limit)
 
     def test_clean(self):
         thing = self.create_thing()

selectable/tests/widgets.py

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