From 6c6849ceab8a4c7534af89959432972fe487bb29 Mon Sep 17 00:00:00 2001 From: Daniel Nouri Date: Wed, 14 Dec 2011 20:15:30 +0100 Subject: [PATCH 1/2] Allow use of 'None' as 'missing' for DateTimes. --- colander/__init__.py | 2 +- colander/tests/test_colander.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/colander/__init__.py b/colander/__init__.py index 6719450..284ebd0 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -1231,7 +1231,7 @@ class DateTime(SchemaType): self.default_tzinfo = default_tzinfo def serialize(self, node, appstruct): - if appstruct is null: + if not appstruct: return null if type(appstruct) is datetime.date: # cant use isinstance; dt subs date diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index 631cf8a..17e5a5b 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -1542,6 +1542,14 @@ class TestDateTime(unittest.TestCase): result = typ.serialize(node, val) self.assertEqual(result, colander.null) + def test_serialize_none(self): + import colander + val = None + node = DummySchemaNode(None) + typ = self._makeOne() + result = typ.serialize(node, val) + self.assertEqual(result, colander.null) + def test_serialize_with_garbage(self): typ = self._makeOne() node = DummySchemaNode(None) From 3a75cb9bb35e60af01c80ecc7f72278ec655a15d Mon Sep 17 00:00:00 2001 From: Christian Neumann Date: Tue, 27 Mar 2012 17:27:41 +0200 Subject: [PATCH 2/2] False evaluating values are now serialized to colander.null for String, Date, and Time --- colander/__init__.py | 6 +++--- colander/tests/test_colander.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/colander/__init__.py b/colander/__init__.py index 284ebd0..6d56520 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -901,7 +901,7 @@ class String(SchemaType): self.encoding = encoding def serialize(self, node, appstruct): - if appstruct is null: + if not appstruct: return null try: @@ -1307,7 +1307,7 @@ class Date(SchemaType): err_template = _('Invalid date') def serialize(self, node, appstruct): - if appstruct is null: + if not appstruct: return null if isinstance(appstruct, datetime.datetime): @@ -1383,7 +1383,7 @@ class Time(SchemaType): err_template = _('Invalid time') def serialize(self, node, appstruct): - if appstruct is null: + if not appstruct: return null if isinstance(appstruct, datetime.datetime): diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index 17e5a5b..92de583 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -1078,6 +1078,14 @@ class TestString(unittest.TestCase): result = typ.serialize(node, null) self.assertEqual(result, null) + def test_serialize_none(self): + import colander + val = None + node = DummySchemaNode(None) + typ = self._makeOne() + result = typ.serialize(node, val) + self.assertEqual(result, colander.null) + def test_serialize_uncooperative(self): val = Uncooperative() node = DummySchemaNode(None) @@ -1677,6 +1685,14 @@ class TestDate(unittest.TestCase): result = typ.serialize(node, val) self.assertEqual(result, colander.null) + def test_serialize_none(self): + import colander + val = None + node = DummySchemaNode(None) + typ = self._makeOne() + result = typ.serialize(node, val) + self.assertEqual(result, colander.null) + def test_serialize_with_garbage(self): typ = self._makeOne() node = DummySchemaNode(None) @@ -1762,6 +1778,14 @@ class TestTime(unittest.TestCase): result = typ.serialize(node, val) self.assertEqual(result, colander.null) + def test_serialize_none(self): + import colander + val = None + node = DummySchemaNode(None) + typ = self._makeOne() + result = typ.serialize(node, val) + self.assertEqual(result, colander.null) + def test_serialize_with_garbage(self): typ = self._makeOne() node = DummySchemaNode(None)