Skip to content

Commit

Permalink
Do not convert numeric types to string
Browse files Browse the repository at this point in the history
FIX: #149
  • Loading branch information
bmihelac committed Oct 3, 2014
1 parent 14fdcbd commit 7e59ba7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 11 additions & 5 deletions import_export/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ def render(self, value):
return force_text(value)


class IntegerWidget(Widget):
class NumberWidget(Widget):

def render(self, value):
return value


class IntegerWidget(NumberWidget):
"""
Widget for converting integer fields.
"""
Expand All @@ -47,7 +53,7 @@ def clean(self, value):
return int(value)


class DecimalWidget(Widget):
class DecimalWidget(NumberWidget):
"""
Widget for converting decimal fields.
"""
Expand Down Expand Up @@ -76,12 +82,12 @@ class BooleanWidget(Widget):

def render(self, value):
if value is None:
return ""
return ""
return self.TRUE_VALUES[0] if value else self.FALSE_VALUE

def clean(self, value):
if value == "":
return None
return None
return True if value in self.TRUE_VALUES else False


Expand Down Expand Up @@ -157,7 +163,7 @@ def render(self, value):
class ForeignKeyWidget(Widget):
"""
Widget for ``ForeignKey`` which looks up a related model.
The lookup field defaults to using the primary key (``pk``), but
can be customised to use any field on the related model.
Expand Down
10 changes: 8 additions & 2 deletions tests/core/tests/widgets_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ def test_clean(self):

class DecimalWidgetTest(TestCase):

def setUp(self):
self.value = Decimal("11.111")
self.widget = widgets.DecimalWidget()

def test_clean(self):
widget = widgets.DecimalWidget()
self.assertEqual(widget.clean("11.111"), Decimal("11.111"))
self.assertEqual(self.widget.clean("11.111"), self.value)

def test_render(self):
self.assertEqual(self.widget.render(self.value), self.value)


class ForeignKeyWidgetTest(TestCase):
Expand Down

0 comments on commit 7e59ba7

Please sign in to comment.