diff backend/couchdb/ListDateTimeField.py @ 170:f0afcd1c5656

implement a couchdb mapping field that stores a datetime instance as a JSON array
author dirk
date Fri, 09 Sep 2011 16:22:44 +0200
parents
children bb3c851b18b1
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/couchdb/ListDateTimeField.py	Fri Sep 09 16:22:44 2011 +0200
@@ -0,0 +1,14 @@
+
+from couchdb.mapping import Field
+from datetime import datetime
+
+class ListDateTimeField(Field):
+    def _to_python(self, value):
+        if isinstance(value, list):
+            return datetime(*value)
+        return value
+
+    def _to_json(self, value):
+        if isinstance(value, datetime):
+            return [ value.year, value.month, value.day, value.hour, value.minute, value.second ]
+        return value