Commits

Mark Lavin committed c9e7fb6

Allow AutoCompleteSelectField and AutoCompleteSelectMultipleField to use the combobox widgets. Deprecate the combobox fields. See #55.

  • Participants
  • Parent commits 39e45dd

Comments (0)

Files changed (2)

File selectable/forms/fields.py

+import warnings
+
 from django import forms
 from django.core.exceptions import ValidationError
 from django.core.validators import EMPTY_VALUES
         self.lookup_class = lookup_class
         self.allow_new = kwargs.pop('allow_new', False)
         self.limit = kwargs.pop('limit', None)
-        kwargs['widget'] = self.widget(lookup_class, allow_new=self.allow_new, limit=self.limit)
+        widget = kwargs.pop('widget', self.widget) or self.widget
+        if isinstance(widget, type):
+            kwargs['widget'] = widget(lookup_class, allow_new=self.allow_new, limit=self.limit)
         super(AutoCompleteSelectField, self).__init__(*args, **kwargs)
 
 
 class AutoComboboxSelectField(AutoCompleteSelectField):
     widget = AutoComboboxSelectWidget
 
+    def __init__(self, *args, **kwargs):
+        super(AutoComboboxSelectField, self).__init__(*args, **kwargs)
+        warnings.warn(
+            u"AutoComboboxSelectField is deprecated; " +
+            "Use AutoCompleteSelectField with a AutoComboboxSelectWidget instead.", 
+            DeprecationWarning,  stacklevel=2
+        )
+
 
 class AutoCompleteSelectMultipleField(forms.Field):
     widget = AutoCompleteSelectMultipleWidget
     def __init__(self, lookup_class, *args, **kwargs):
         self.lookup_class = lookup_class
         self.limit = kwargs.pop('limit', None)
-        kwargs['widget'] = self.widget(lookup_class, limit=self.limit)
+        widget = kwargs.pop('widget', self.widget) or self.widget
+        if isinstance(widget, type):
+            kwargs['widget'] = widget(lookup_class, limit=self.limit)
         super(AutoCompleteSelectMultipleField, self).__init__(*args, **kwargs)
 
     def to_python(self, value):
 class AutoComboboxSelectMultipleField(AutoCompleteSelectMultipleField):
     widget = AutoComboboxSelectMultipleWidget
 
+    def __init__(self, *args, **kwargs):
+        super(AutoComboboxSelectMultipleField, self).__init__(*args, **kwargs)
+        warnings.warn(
+            u"AutoComboboxSelectMultipleField is deprecated; " +
+            "Use AutoCompleteSelectMultipleField with a " +
+            "AutoComboboxSelectMultipleWidget instead.", 
+            DeprecationWarning,  stacklevel=2
+        )

File selectable/tests/fields.py

 from django import forms
 
-from selectable.forms import fields
+from selectable.forms import fields, widgets
 from selectable.tests import ThingLookup
 from selectable.tests.base import BaseSelectableTestCase
 
     field_cls = None
     lookup_cls = None
 
-    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 get_field_instance(self, allow_new=False, limit=None, widget=None):
+        return self.field_cls(self.lookup_cls, allow_new=allow_new, limit=limit, widget=widget)
 
     def test_init(self):
         field = self.get_field_instance()
-        self.assertEqual(field.lookup_class, self.__class__.lookup_cls)
+        self.assertEqual(field.lookup_class, self.lookup_cls)
 
     def test_init_with_limit(self):
         field = self.get_field_instance(limit=10)
         value = field.clean([value, ''])
         self.assertTrue(isinstance(value, ThingLookup.model))
 
+    def test_default_widget(self):
+        field = self.get_field_instance()
+        self.assertTrue(isinstance(field.widget, widgets.AutoCompleteSelectWidget))
+
+    def test_alternate_widget(self):
+        widget_cls = widgets.AutoComboboxWidget
+        field = self.get_field_instance(widget=widget_cls)
+        self.assertTrue(isinstance(field.widget, widget_cls))
+
 
 class AutoComboboxSelectFieldTestCase(BaseFieldTestCase):
     field_cls = fields.AutoComboboxSelectField
     field_cls = fields.AutoCompleteSelectMultipleField
     lookup_cls = ThingLookup
 
-    def get_field_instance(self, limit=None):
-        return self.__class__.field_cls(self.__class__.lookup_cls, limit=limit)
+    def get_field_instance(self, limit=None, widget=None):
+        return self.field_cls(self.lookup_cls, limit=limit, widget=widget)
 
     def test_clean(self):
         thing = self.create_thing()
         value = field.clean(ids)
         self.assertEqual([thing, other_thing], value)
 
+    def test_default_widget(self):
+        field = self.get_field_instance()
+        self.assertTrue(isinstance(field.widget, widgets.AutoCompleteSelectMultipleWidget))
+
+    def test_alternate_widget(self):
+        widget_cls = widgets.AutoComboboxSelectMultipleWidget
+        field = self.get_field_instance(widget=widget_cls)
+        self.assertTrue(isinstance(field.widget, widget_cls))
+
 
 class AutoComboboxSelectMultipleFieldTestCase(BaseFieldTestCase):
     field_cls = fields.AutoComboboxSelectMultipleField