Karen Tracey avatar Karen Tracey committed 4e5c4e0

Check for iterable, not list, in AutoCompleteSelectMultipleWidget.

Comments (0)

Files changed (3)

selectable/forms/widgets.py

 
     def decompress(self, value):
         if value:
-            if not isinstance(value, list):
+            if not hasattr(value, '__iter__'):
                 value = [value]
             return [None, value]
         return [None, None]

selectable/tests/fields.py

 from django import forms
+from django.utils import unittest
 
 from selectable.forms import fields
-from selectable.tests import ThingLookup
+from selectable.tests import Thing, ThingLookup
 from selectable.tests.base import BaseSelectableTestCase
+from selectable.tests.forms import Form1
 
 
 __all__ = (
         value = field.clean([names, ids])
         self.assertEqual([thing, other_thing], value)
 
+    def test_initial(self):
+        t1 = self.create_thing()
+        f1 = Form1(initial={
+            'f': Thing.objects.filter(
+                pk=t1.pk
+            ).values_list('pk', flat=True)})
+        rendered_value = f1.as_p()
+        hidden_widget_expected = 'value="%d" data-selectable-type="hidden-multiple" type="hidden"' % t1.pk
+        c = rendered_value.count(hidden_widget_expected)
+        ev = 1
+        msg = c < ev and \
+            'Did not find:\n\t%s\nas expected in:\n\t%s' % (hidden_widget_expected, rendered_value) or \
+            'Found:\n\t%s\ntoo many times (%d) in:\n\t%s\nexpecting onnly %d' % (hidden_widget_expected, c, rendered_value, ev)
+        self.assertEquals(c, ev, msg)
+
+    def test_initial_multiple(self):
+        t1 = self.create_thing()
+        t2 = self.create_thing()
+        f1 = Form1(initial={
+            'f': Thing.objects.filter(
+                pk__in=[t1.pk, t2.pk]
+            ).values_list('pk', flat=True)})
+        rendered_value = f1.as_p()
+        ev = 1
+        for t in [t1, t2]:
+            hidden_widget_expected = 'value="%d" data-selectable-type="hidden-multiple" type="hidden"' % t.pk
+            c = rendered_value.count(hidden_widget_expected)
+            msg = c < ev and \
+                'Did not find:\n\t%s\nas expected in:\n\t%s' % (hidden_widget_expected, rendered_value) or \
+                'Found:\n\t%s\ntoo many times (%d) in:\n\t%s\nexpecting onnly %d' % (hidden_widget_expected, c, rendered_value, ev)
+            self.assertEquals(c, ev, msg)
+
+    @unittest.expectedFailure
+    def test_initial_multiple_list(self):
+        t1 = self.create_thing()
+        t2 = self.create_thing()
+        f1 = Form1(initial={
+            'f': list(Thing.objects.filter(
+                pk__in=[t1.pk, t2.pk]
+            ).values_list('pk', flat=True))})
+        rendered_value = f1.as_p()
+        ev = 1
+        for t in [t1, t2]:
+            hidden_widget_expected = 'value="%d" data-selectable-type="hidden-multiple" type="hidden"' % t.pk
+            c = rendered_value.count(hidden_widget_expected)
+            msg = c < ev and \
+                'Did not find:\n\t%s\nas expected in:\n\t%s' % (hidden_widget_expected, rendered_value) or \
+                'Found:\n\t%s\ntoo many times (%d) in:\n\t%s\nexpecting onnly %d' % (hidden_widget_expected, c, rendered_value, ev)
+            self.assertEquals(c, ev, msg)
 
 class AutoComboboxSelectMultipleFieldTestCase(BaseFieldTestCase):
     field_cls = fields.AutoComboboxSelectMultipleField

selectable/tests/forms.py

+from django import forms
+
+from selectable.forms import fields
+from selectable.tests import ThingLookup
+
+class Form1(forms.Form):
+    f = fields.AutoCompleteSelectMultipleField(ThingLookup)
+
+
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.