Repository: django-import-export/django-import-export

Issue:
NumberWidget.is_empty() should strip the value if string type
At the moment `NumberWidget.is_empty()` check doesn't strip the value before making the `value == ""` comparison. As a consequence, if the value happens to a be a string comprised entirely of spaces e.g `u'  '`, the `is_empty()` check evaluates to False. This in effect can cause value errors (e.g `ValueError: could not convert string to float:  ` ) in child widgets (`IntegerWidget`, etc)  which do a type conversion.

To resolve this, I think we should strip the value (if it's a string type) before the comparison.

```Python
class NumberWidget(Widget):
    """
    """

    def is_empty(self, value):
        if isinstance(value, six.string_types):
            value = value.strip()
        # 0 is not empty
        return value is None or value == ""
```


Discussion:

Resolving patch:
diff --git a/import_export/widgets.py b/import_export/widgets.py
--- a/import_export/widgets.py
+++ b/import_export/widgets.py
@@ -56,6 +56,8 @@ class NumberWidget(Widget):
     """
 
     def is_empty(self, value):
+        if isinstance(value, six.string_types):
+            value = value.strip()
         # 0 is not empty
         return value is None or value == ""
 


Test patch:
diff --git a/tests/core/tests/widgets_tests.py b/tests/core/tests/widgets_tests.py
--- a/tests/core/tests/widgets_tests.py
+++ b/tests/core/tests/widgets_tests.py
@@ -131,6 +131,28 @@ def test_clean(self):
         self.assertEqual(self.widget.clean("1:57:00"), self.duration)
 
 
+class FloatWidgetTest(TestCase):
+
+    def setUp(self):
+        self.value = 11.111
+        self.widget = widgets.FloatWidget()
+
+    def test_clean(self):
+        self.assertEqual(self.widget.clean(11.111), self.value)
+
+    def test_render(self):
+        self.assertEqual(self.widget.render(self.value), self.value)
+
+    def test_clean_string_zero(self):
+        self.assertEqual(self.widget.clean("0"), 0.0)
+        self.assertEqual(self.widget.clean("0.0"), 0.0)
+
+    def test_clean_empty_string(self):
+        self.assertEqual(self.widget.clean(""), None)
+        self.assertEqual(self.widget.clean(" "), None)
+        self.assertEqual(self.widget.clean("\r\n\t"), None)
+
+
 class DecimalWidgetTest(TestCase):
 
     def setUp(self):
@@ -147,6 +169,11 @@ def test_clean_string_zero(self):
         self.assertEqual(self.widget.clean("0"), Decimal("0"))
         self.assertEqual(self.widget.clean("0.0"), Decimal("0"))
 
+    def test_clean_empty_string(self):
+        self.assertEqual(self.widget.clean(""), None)
+        self.assertEqual(self.widget.clean(" "), None)
+        self.assertEqual(self.widget.clean("\r\n\t"), None)
+
 
 class IntegerWidgetTest(TestCase):
 
@@ -161,6 +188,11 @@ def test_clean_string_zero(self):
         self.assertEqual(self.widget.clean("0"), self.value)
         self.assertEqual(self.widget.clean("0.0"), self.value)
 
+    def test_clean_empty_string(self):
+        self.assertEqual(self.widget.clean(""), None)
+        self.assertEqual(self.widget.clean(" "), None)
+        self.assertEqual(self.widget.clean("\n\t\r"), None)
+
 
 class ForeignKeyWidgetTest(TestCase):