[Checkins] SVN: zc.datetimewidget/trunk/src/zc/datetimewidget/
Adjust implementation and tests to new zope datetime widget code.
Gary Poster
gary at zope.com
Mon Jun 26 11:28:07 EDT 2006
Log message for revision 68840:
Adjust implementation and tests to new zope datetime widget code.
Changed:
U zc.datetimewidget/trunk/src/zc/datetimewidget/datetimewidget.py
U zc.datetimewidget/trunk/src/zc/datetimewidget/timezones.txt
-=-
Modified: zc.datetimewidget/trunk/src/zc/datetimewidget/datetimewidget.py
===================================================================
--- zc.datetimewidget/trunk/src/zc/datetimewidget/datetimewidget.py 2006-06-26 14:37:15 UTC (rev 68839)
+++ zc.datetimewidget/trunk/src/zc/datetimewidget/datetimewidget.py 2006-06-26 15:28:06 UTC (rev 68840)
@@ -19,8 +19,7 @@
import pytz
from zope.interface.common.idatetime import ITZInfo
-from zope.datetime import DateTimeError
-from zope.datetime import parseDatetimetz
+from zope.datetime import parseDatetimetz, DateTimeError
from zope.app.form.browser import textwidgets
from zope.app.form.browser.widget import renderElement
import zope.datetime
@@ -96,9 +95,16 @@
"datetime_format": self._format,
"langDef":langDef}
+ def _toFieldValue(self, input):
+ if input == self._missing:
+ return self.context.missing_value
+ else:
+ try:
+ return parseDatetimetz(input)
+ except (DateTimeError, ValueError, IndexError), v:
+ return super(DatetimeBase, self)._toFieldValue(input)
+
def _toFormValue(self, value):
- if not isinstance(value, datetime.date):
- return super(DatetimeBase, self)._toFormValue(value)
value = localizeDateTime(value, self.request)
return value.strftime(self._format)
Modified: zc.datetimewidget/trunk/src/zc/datetimewidget/timezones.txt
===================================================================
--- zc.datetimewidget/trunk/src/zc/datetimewidget/timezones.txt 2006-06-26 14:37:15 UTC (rev 68839)
+++ zc.datetimewidget/trunk/src/zc/datetimewidget/timezones.txt 2006-06-26 15:28:06 UTC (rev 68840)
@@ -2,13 +2,13 @@
Handling Timezones
==================
-Datetimes are always stored timezone aware, by default the utc
+Datetimes are always stored timezone aware, and by default the utc
timezone is used. We use the demo package here to have a content class
and interface.
In order to handle timezones correctly the zope instance has to
provide an adapter from IBrowserRequest to ITZInfo. It is up to the
-instance what kind of implementation it uses. For this tests we just
+instance what kind of implementation it uses. For this test, we just
use the implementation of the demo.timezone module which always
returns Europe/Vienna as timezone.
@@ -19,24 +19,15 @@
>>> from zc.datetimewidget.demo import timezone
>>> from zc.datetimewidget.demo.content import DemoContent
>>> from zc.datetimewidget.demo.interfaces import IDemoContent
- >>> from zope.publisher.browser import TestRequest
- >>> request = TestRequest()
+ >>> from zope.publisher.browser import TestRequest, BrowserLanguages
+ >>> component.provideAdapter(BrowserLanguages)
+ >>> request = TestRequest(HTTP_ACCEPT_LANGUAGE='en-US')
>>> component.provideAdapter(timezone.tzinfo)
- >>> fmt = '%Y-%m-%d %H:%M:%S %Z%z'
>>> tz = pytz.timezone('Europe/Vienna')
>>> field = IDemoContent['startDatetime']
>>> widget = datetimewidget.DatetimeWidget(field,request)
>>> dt = datetime(2006,5,1,12,tzinfo=pytz.utc)
-By the way, if the value to be displayed in the form is not a datetime
-object the superlclass's implementation is used.
-
- >>> widget._toFormValue(None)
- u''
-
- >>> widget._toFormValue(u'this is not a date')
- u'this is not a date'
-
Now let us convert a real datetime.
>>> formValue = widget._toFormValue(dt)
@@ -46,7 +37,9 @@
>>> parsedValue
datetime.datetime(2006, 5, 1, 12, 0, tzinfo=<UTC>)
+While the widget tries to parse dates in the form '%Y-%m-%d %H:%M:%S'
+first, it will fall through to the locale-specific parsing of the core
+datetimewidget.
-
-
-
+ >>> widget._toFieldValue('May 1, 2006 2:00:00 PM')
+ datetime.datetime(2006, 5, 1, 12, 0, tzinfo=<UTC>)
More information about the Checkins
mailing list