Mark Lavin avatar Mark Lavin committed 45e3343 Merge

More changes that I forgot to merge.

Comments (0)

Files changed (4)

 def runtests():
     TestRunner = get_runner(settings)
     test_runner = TestRunner(verbosity=1, interactive=True, failfast=False)
-    failures = test_runner.run_tests(['selectable', ])
+    args = sys.argv[1:] or ['selectable', ]
+    failures = test_runner.run_tests(args)
     sys.exit(failures)
 
 
 if __name__ == '__main__':
-    runtests(*sys.argv[1:])
+    runtests()
 

selectable/forms/widgets.py

 
     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 initial is None and data is None:
+            return False
         if data and not hasattr(data, '__iter__'):
             data = self.decompress(data)
         return super(SelectableMultiWidget, self)._has_changed(initial, data)
         value = ['', value]
         return super(AutoCompleteSelectMultipleWidget, self).render(name, value, attrs)
 
+    def _has_changed(self, initial, data):
+        """"
+        Decects if the widget was changed. This is removed in 1.6.
+
+        For the multi-select case we only care if the hidden inputs changed.
+        """
+        initial = ['', initial]
+        data = ['', data]
+        return super(AutoCompleteSelectMultipleWidget, self)._has_changed(initial, data)
+
+
 
 class AutoComboboxSelectMultipleWidget(SelectableMultiWidget, SelectableMediaMixin):
 
         value = ['', value]
         return super(AutoComboboxSelectMultipleWidget, self).render(name, value, attrs)
 
+    def _has_changed(self, initial, data):
+        """"
+        Decects if the widget was changed. This is removed in 1.6.
+
+        For the multi-select case we only care if the hidden inputs changed.
+        """
+        initial = ['', initial]
+        data = ['', data]
+        return super(AutoComboboxSelectMultipleWidget, self)._has_changed(initial, data)

selectable/tests/functests.py

         things = manything.things.all()
         self.assertEqual(things.count(), 0)
 
+    def test_has_changed(self):
+        "Populate intial data from a model."
+        manything = ManyThing.objects.create(name='Foo')
+        thing_1 = self.create_thing()
+        manything.things.add(thing_1)
+        data = {
+            'name': manything.name,
+            'things_0': '', # Text input
+            'things_1': [thing_1.pk], # Hidden inputs
+        }
+        form = ManyThingForm(data=data, instance=manything)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+
 
 class SimpleForm(forms.Form):
     "Non-model form usage."
             'things': '',
         }
         form = SimpleForm(data=data, initial=initial, empty_permitted=True)
-        self.assertFalse(form.has_changed())
-        self.assertTrue(form.is_valid(), str(form.errors))
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+        self.assertTrue(form.is_valid(), str(form.errors))
+
+    def test_no_initial_with_empty_permitted(self):
+        """
+        If empty data is submitted and allowed with no initial then
+        the form should not be seen as changed.
+        """
+        data = {
+            'thing_0': '',
+            'thing_1': '',
+            'new_thing_0': '',
+            'new_thing_1': '',
+            'things_0': '',
+            'things_1': '',
+        }
+        form = SimpleForm(data=data, empty_permitted=True)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+        self.assertTrue(form.is_valid(), str(form.errors))
+
+    def test_no_data_with_empty_permitted(self):
+        """
+        If no data is submitted and allowed with no initial then
+        the form should not be seen as changed.
+        """
+        form = SimpleForm(data={}, empty_permitted=True)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+        self.assertTrue(form.is_valid(), str(form.errors))
+
+    def test_select_multiple_changed(self):
+        """
+        Detect changes for a multiple select input with and without
+        initial data.
+        """
+        data = {
+            'thing_0': '',
+            'thing_1': '',
+            'new_thing_0': '',
+            'new_thing_1': '',
+            'things_0': '',
+            'things_1': [self.test_thing.pk, ]
+        }
+        form = SimpleForm(data=data)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('things' in form.changed_data)
+
+        initial = {
+            'thing': '',
+            'new_thing': '',
+            'things': [self.test_thing.pk, ],
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+
+        initial = {
+            'thing': '',
+            'new_thing': '',
+            'things': [],
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('things' in form.changed_data)
+
+    def test_single_select_changed(self):
+        """
+        Detect changes for a single select input with and without
+        initial data.
+        """
+        data = {
+            'thing_0': '',
+            'thing_1': self.test_thing.pk,
+            'new_thing_0': '',
+            'new_thing_1': '',
+            'things_0': '',
+            'things_1': ''
+        }
+        form = SimpleForm(data=data)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('thing' in form.changed_data)
+
+        initial = {
+            'thing': self.test_thing.pk,
+            'new_thing': '',
+            'things': '',
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+
+        initial = {
+            'thing': '',
+            'new_thing': '',
+            'things': '',
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('thing' in form.changed_data)
+
+    def test_new_select_changed(self):
+        """
+        Detect changes for a single select input which allows new items
+        with and without initial data.
+        """
+        data = {
+            'thing_0': '',
+            'thing_1': '',
+            'new_thing_0': 'Foo',
+            'new_thing_1': '',
+            'things_0': '',
+            'things_1': ''
+        }
+        form = SimpleForm(data=data)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('new_thing' in form.changed_data)
+
+        initial = {
+            'thing': '',
+            'new_thing': ['Foo', None],
+            'things': '',
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+
+        initial = {
+            'thing': '',
+            'new_thing': '',
+            'things': '',
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('new_thing' in form.changed_data)
 
 [testenv:py32-1.5.X]
 basepython = python3.2
-deps = https://www.djangoproject.com/download/1.5c1/tarball/
+deps = django>=1.5,<1.6
     mock
 
 [testenv:py26-1.5.X]
 basepython = python2.6
-deps = https://www.djangoproject.com/download/1.5c1/tarball/
+deps = django>=1.5,<1.6
     mock
 
 [testenv:py26-1.4.X]
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.