Repository: CybOXProject/mixbox

Issue:
DateField does not properly serialize datetime objects
When storing a Python `datetime` object in a property specified by a `DateField`, the value incorrectly serializes to an ISO timestamp (`YYYY-MM-DDThh:mm:ss`) instead of to a `xs:date` format (`YYYY-MM-DD`).

    class Foo(Entity):
        my_date = DateField("my_date");

    obj = Foo()
    obj.my_date = datetime.datetime.now()

There are two issues:

* the `DateField::binding_value` function uses `serialize_datetime` instead of `serialize_date`

* the `serialize_date` function erroneously includes time information in its output when processing datetimes

Discussion:

Patch:
diff --git a/mixbox/dates.py b/mixbox/dates.py
index be279e1..794e0b0 100644
--- a/mixbox/dates.py
+++ b/mixbox/dates.py
@@ -73,10 +73,10 @@ def serialize_date(value):
     """
     if not value:
         return None
-    elif isinstance(value, datetime.date):
-        return value.isoformat()
     elif isinstance(value, datetime.datetime):
         return value.date().isoformat()
+    elif isinstance(value, datetime.date):
+        return value.isoformat()
     else:
         return parse_date(value).isoformat()
 
diff --git a/mixbox/fields.py b/mixbox/fields.py
index f623c6b..cad106a 100644
--- a/mixbox/fields.py
+++ b/mixbox/fields.py
@@ -373,7 +373,7 @@ class DateField(TypedField):
         return serialize_date(value)
 
     def binding_value(self, value):
-        return serialize_datetime(value)
+        return serialize_date(value)
 
 
 class CDATAField(TypedField):


Test patch:
diff --git a/tests/dates_tests.py b/tests/dates_tests.py
index f139939..d3be86b 100644
--- a/tests/dates_tests.py
+++ b/tests/dates_tests.py
@@ -15,6 +15,12 @@ class DatesTests(unittest.TestCase):
         dstr = "2015-04-01"
         parsed = dates.parse_date(dstr)
         self.assertEqual(dstr, parsed.isoformat())
+        
+    def test_serialize_datetime_as_date(self):
+        now = dates.now()
+        self.assertTrue(isinstance(now, datetime.datetime))
+        nowstr = dates.serialize_date(now)
+        self.assertEquals(nowstr, now.date().isoformat())
 
     def test_parse_datetime(self):
         dtstr = '2015-04-02T16:44:30.423149+00:00'