Commits

Mark Lavin committed 8f350b2

Fix issue with selectable widgets has_changed. Fixes #92

  • Participants
  • Parent commits dcfd9dc

Comments (0)

Files changed (3)

File docs/releases.rst

 
 - Fixed bug with matching hidden input when the name contains '_1'. Thanks to Augusto Men for the report and fix.
 - Fixed bug where the enter button would open the combobox options rather than submit the form. Thanks to Felipe Prenholato for the report.
-- Fixed bug with using ``allow_new=True`` creating items when no data was submitted. See #91
+- Fixed bug with using ``allow_new=True`` creating items when no data was submitted. See #91.
+- Fixed bug with widget ``has_changed`` when there is no initial data. See #92.
 
 
 Backwards Incompatible Changes

File selectable/forms/widgets.py

     def update_query_parameters(self, qs_dict):
         self.widgets[0].update_query_parameters(qs_dict)
 
+    def _has_changed(self, initial, data):
+        "Decects if the widget was changed. This is removed in 1.6."
+        if initial is None and data is not None:
+            return True
+        if data and not hasattr(data, '__iter__'):
+            data = self.decompress(data)
+        return super(SelectableMultiWidget, self)._has_changed(initial, data)
+
+    def decompress(self, value):
+        if value:
+            lookup = self.lookup_class()
+            model = getattr(self.lookup_class, 'model', None)
+            if model and isinstance(value, model):
+                item = value
+                value = lookup.get_item_id(item)
+            else:
+                item = lookup.get_item(value)
+            item_value = lookup.get_item_value(item)
+            return [item_value, value]
+        return [None, None]
+
 
 class AutoCompleteSelectWidget(SelectableMultiWidget, SelectableMediaMixin):
 
         ]
         super(AutoCompleteSelectWidget, self).__init__(widgets, *args, **kwargs)
 
-    def decompress(self, value):
-        if value:
-            lookup = self.lookup_class()
-            model = getattr(self.lookup_class, 'model', None)
-            if model and isinstance(value, model):
-                item = value
-                value = lookup.get_item_id(item)
-            else:
-                item = lookup.get_item(value)
-            item_value = lookup.get_item_value(item)
-            return [item_value, value]
-        return [None, None]
-
     def value_from_datadict(self, data, files, name):
         value = super(AutoCompleteSelectWidget, self).value_from_datadict(data, files, name)
         if not self.allow_new:
         ]
         super(AutoComboboxSelectWidget, self).__init__(widgets, *args, **kwargs)
 
-    def decompress(self, value):
-        if value:
-            lookup = self.lookup_class()
-            model = getattr(self.lookup_class, 'model', None)
-            if model and isinstance(value, model):
-                item = value
-                value = lookup.get_item_id(item)
-            else:
-                item = lookup.get_item(value)
-            item_value = lookup.get_item_value(item)
-            return [item_value, value]
-        return [None, None]
-
     def value_from_datadict(self, data, files, name):
         value = super(AutoComboboxSelectWidget, self).value_from_datadict(data, files, name)
         if not self.allow_new:

File selectable/tests/functests.py

         }
         form = SimpleForm(data=data)
         self.assertFalse(form.is_valid())
-        self.assertTrue('new_thing' in form.errors)
+        self.assertTrue('new_thing' in form.errors)
+
+    def test_has_changed_with_empty_permitted(self):
+        """
+        Regression test for #92. has_changed fails when there is no initial and
+        allow_new=False.
+        """
+        data = {
+            'thing_0': '',
+            'thing_1': self.test_thing.pk,
+            'new_thing_0': self.test_thing.name,
+            'new_thing_1': self.test_thing.pk,
+            'things_0': '',
+            'things_1': [self.test_thing.pk, ]
+        }
+        form = SimpleForm(data=data, empty_permitted=True)
+        self.assertTrue(form.has_changed())
+        self.assertTrue(form.is_valid(), str(form.errors))
+
+    def test_not_changed(self):
+        """
+        Regression test for #92. has_changed fails when there is no initial and
+        allow_new=False.
+        """
+        data = {
+            'thing_0': self.test_thing.name,
+            'thing_1': self.test_thing.pk,
+            'new_thing_0': self.test_thing.name,
+            'new_thing_1': self.test_thing.pk,
+            'things_0': '',
+            'things_1': [self.test_thing.pk, ]
+        }
+        initial = {
+            'thing': self.test_thing.pk,
+            'new_thing': self.test_thing.pk,
+            'things': [self.test_thing.pk, ]
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertFalse(form.has_changed())
+        self.assertTrue(form.is_valid(), str(form.errors))
+
+    def test_not_changed_with_empty_permitted(self):
+        """
+        Regression test for #92. has_changed fails when there is no initial and
+        allow_new=False.
+        """
+        data = {
+            'thing_0': '',
+            'thing_1': '',
+            'new_thing_0': '',
+            'new_thing_1': '',
+            'things_0': '',
+            'things_1': '',
+        }
+        initial = {
+            'thing': '',
+            'new_thing': '',
+            'things': '',
+        }
+        form = SimpleForm(data=data, initial=initial, empty_permitted=True)
+        self.assertFalse(form.has_changed())
+        self.assertTrue(form.is_valid(), str(form.errors))