cubicweb/web/formfields.py
changeset 11240 1694e6e9ff94
parent 11131 2dafcdd19c99
child 11244 4d2a7d6c9390
--- a/cubicweb/web/formfields.py	Mon May 02 17:14:43 2016 +0200
+++ b/cubicweb/web/formfields.py	Wed May 04 17:07:41 2016 +0200
@@ -42,6 +42,7 @@
 .. autoclass:: cubicweb.web.formfields.BooleanField()
 .. autoclass:: cubicweb.web.formfields.DateField()
 .. autoclass:: cubicweb.web.formfields.DateTimeField()
+.. autoclass:: cubicweb.web.formfields.TZDatetimeField()
 .. autoclass:: cubicweb.web.formfields.TimeField()
 .. autoclass:: cubicweb.web.formfields.TimeIntervalField()
 
@@ -65,6 +66,8 @@
 
 from datetime import datetime, timedelta
 
+import pytz
+
 from six import PY2, text_type, string_types
 
 from logilab.mtconverter import xml_escape
@@ -1015,6 +1018,18 @@
     etype = 'Datetime'
 
 
+class TZDatetimeField(DateTimeField):
+    """ Use this field to edit a timezone-aware datetime (`TZDatetime` yams
+    type). Note the posted values are interpreted as UTC, so you may need to
+    convert them client-side, using some javascript in the corresponding widget.
+    """
+
+    def _ensure_correctly_typed(self, form, value):
+        tz_naive = super(TZDatetimeField, self)._ensure_correctly_typed(
+            form, value)
+        return tz_naive.replace(tzinfo=pytz.utc)
+
+
 class TimeField(DateField):
     """Use this field to edit time (`Time` yams type).
 
@@ -1267,7 +1282,7 @@
 
     'Date':       DateField,
     'Datetime':   DateTimeField,
-    'TZDatetime': DateTimeField,
+    'TZDatetime': TZDatetimeField,
     'Time':       TimeField,
     'TZTime':     TimeField,
     'Interval':   TimeIntervalField,