Commits

Karen Tracey  committed 00e70f1

More complete fix includes also overriding render to ensure that decompress is called even when caller specifies initial values as a list (superclass render won't call decompress if value is already a list, but we need decompress to be called since it inserts None as the value for the auto widget). Also fix the other auto/multi widget. Move tests into widgets since that's really where the problem was and make them more consistent with existing pattern.

  • Participants
  • Parent commits 4e5c4e0

Comments (0)

Files changed (4)

File selectable/forms/widgets.py

             return [None, value]
         return [None, None]
 
+    def render(self, name, value, attrs=None):
+        value = self.decompress(value)
+        return super(AutoCompleteSelectMultipleWidget, self).render(name, value, attrs)
+
 
 class AutoComboboxSelectMultipleWidget(forms.MultiWidget, SelectableMediaMixin):
 
 
     def decompress(self, value):
         if value:
-            if not isinstance(value, list):
+            if not hasattr(value, '__iter__'):
                 value = [value]
             return [None, value]
         return [None, None]
 
+    def render(self, name, value, attrs=None):
+        value = self.decompress(value)
+        return super(AutoComboboxSelectMultipleWidget, self).render(name, value, attrs)
+

File selectable/tests/fields.py

 from django import forms
-from django.utils import unittest
 
 from selectable.forms import fields
-from selectable.tests import Thing, ThingLookup
+from selectable.tests import ThingLookup
 from selectable.tests.base import BaseSelectableTestCase
 from selectable.tests.forms import Form1
 
         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

File 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)
-
-

File selectable/tests/widgets.py

 from django import forms
 
 from selectable.forms import widgets
-from selectable.tests import ThingLookup
+from selectable.tests import Thing, ThingLookup
 from selectable.tests.base import BaseSelectableTestCase
 
 
         self.assertTrue('data-selectable-type' in attrs)
         self.assertEqual(attrs['data-selectable-type'], 'hidden-multiple')
 
+    def test_render_single(self):
+        widget = self.get_widget_instance()
+        val = 4
+        rendered_value = widget.render('field_name', val)
+        ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % val
+        self.assertTrue(ev in rendered_value,
+            "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+
+    def test_render_list(self):
+        widget = self.get_widget_instance()
+        list_val = [8, 5]
+        rendered_value = widget.render('field_name', list_val)
+        for v in list_val:
+            ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % v
+            self.assertTrue(ev in rendered_value,
+                "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+
+    def test_render_qs(self):
+        widget = self.get_widget_instance()
+        t1 = self.create_thing()
+        t2 = self.create_thing()
+        qs_val = Thing.objects.filter(pk__in=[t1.pk, t2.pk]).values_list('pk', flat=True)
+        rendered_value = widget.render('field_name', qs_val)
+        for t in qs_val:
+            ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % t
+            self.assertTrue(ev in rendered_value,
+                "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
 
 class AutoComboboxSelectMultipleWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoComboboxSelectMultipleWidget
         self.assertTrue('data-selectable-type' in attrs)
         self.assertEqual(attrs['data-selectable-type'], 'hidden-multiple')
 
+    def test_render_single(self):
+        widget = self.get_widget_instance()
+        val = 4
+        rendered_value = widget.render('field_name', val)
+        ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % val
+        self.assertTrue(ev in rendered_value,
+            "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+
+    def test_render_list(self):
+        widget = self.get_widget_instance()
+        list_val = [8, 5]
+        rendered_value = widget.render('field_name', list_val)
+        for v in list_val:
+            ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % v
+            self.assertTrue(ev in rendered_value,
+                "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+
+    def test_render_qs(self):
+        widget = self.get_widget_instance()
+        t1 = self.create_thing()
+        t2 = self.create_thing()
+        qs_val = Thing.objects.filter(pk__in=[t1.pk, t2.pk]).values_list('pk', flat=True)
+        rendered_value = widget.render('field_name', qs_val)
+        for t in qs_val:
+            ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % t
+            self.assertTrue(ev in rendered_value,
+                "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+
+