[Zope3-checkins] SVN: Zope3/trunk/ Merged revision 68818 from the 3.3 branch:

Dmitry Vasiliev dima at hlabs.spb.ru
Sat Jun 24 05:47:02 EDT 2006


Log message for revision 68821:
  Merged revision 68818 from the 3.3 branch:
  
  Fixed issue 525: DateWidget ru-format
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/form/browser/ftests/test_datetimewidget.py
  U   Zope3/trunk/src/zope/app/form/browser/tests/test_browserwidget.py
  U   Zope3/trunk/src/zope/app/form/browser/tests/test_datetimewidget.py
  U   Zope3/trunk/src/zope/app/form/browser/tests/test_datewidget.py
  U   Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py
  U   Zope3/trunk/src/zope/app/form/browser/textwidgets.py
  U   Zope3/trunk/src/zope/formlib/form.txt

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2006-06-24 09:46:39 UTC (rev 68820)
+++ Zope3/trunk/doc/CHANGES.txt	2006-06-24 09:46:59 UTC (rev 68821)
@@ -16,6 +16,10 @@
 
     Bug fixes
 
+      - Fixed issue 525: DateWidget ru-format.
+        If you plan to use DatetimeWidget with timezone information take a look
+        at the note in the DatetimeWidget docstring;
+
       - Fixed issue 531: Spurious i18n deprecation warnings;
 
       - Fixed issue 639: Default ITraverser ignores dict methods

Modified: Zope3/trunk/src/zope/app/form/browser/ftests/test_datetimewidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/ftests/test_datetimewidget.py	2006-06-24 09:46:39 UTC (rev 68820)
+++ Zope3/trunk/src/zope/app/form/browser/ftests/test_datetimewidget.py	2006-06-24 09:46:59 UTC (rev 68821)
@@ -22,7 +22,7 @@
 from datetime import datetime
 
 import zope.security.checker
-from zope.datetime import parseDatetimetz, tzinfo
+from zope.datetime import tzinfo
 from zope.interface import Interface, implements
 from zope.schema import Datetime, Choice
 from zope.traversing.api import traverse
@@ -30,6 +30,7 @@
 from zope.app.form.browser.ftests.support import *
 from zope.app.testing.functional import BrowserTestCase
 
+
 class IDatetimeTest(Interface):
 
     d2 = Datetime(
@@ -38,93 +39,92 @@
     d3 = Choice(
         required=False,
         values=(
-            datetime(2003, 9, 15, tzinfo=tzinfo(0)),
-            datetime(2003, 10, 15, tzinfo=tzinfo(0))),
-        missing_value=datetime(2000, 1, 1, tzinfo=tzinfo(0)))
+            datetime(2003, 9, 15),
+            datetime(2003, 10, 15)),
+        missing_value=datetime(2000, 1, 1))
 
     d1 = Datetime(
         required=True,
-        min=datetime(2003, 1, 1, tzinfo=tzinfo(0)),
-        max=datetime(2020, 12, 31, tzinfo=tzinfo(0)))
+        min=datetime(2003, 1, 1),
+        max=datetime(2020, 12, 31))
 
 class DatetimeTest(Persistent):
 
     implements(IDatetimeTest)
 
     def __init__(self):
-        self.d1 = datetime(2003, 4, 6, tzinfo=tzinfo(0))
-        self.d2 = datetime(2003, 8, 6, tzinfo=tzinfo(0))
+        self.d1 = datetime(2003, 4, 6)
+        self.d2 = datetime(2003, 8, 6)
         self.d3 = None
 
-def getDateForField(field, source):
-    """Returns a datetime object for the specified field in source.
+class Test(BrowserTestCase):
 
-    Returns None if the field value cannot be converted to date.
-    """
+    def setUp(self):
+        BrowserTestCase.setUp(self)
+        registerEditForm(IDatetimeTest)
+        defineSecurity(DatetimeTest, IDatetimeTest)
 
-    # look in input element first
-    pattern = '<input .* name="field.%s".* value="(.*)".*>' % field
-    m = re.search(pattern, source)
-    if m is None:
-        # look in a select element
-        pattern = '<select .* name="field.%s".*>.*' \
-            '<option value="(.*)" selected>*.</select>' % field
-        m = re.search(pattern, source, re.DOTALL)
+    def getDateForField(self, field, source):
+        """Returns a datetime object for the specified field in source.
+
+        Returns None if the field value cannot be converted to date.
+        """
+
+        # look in input element first
+        pattern = '<input .* name="field.%s".* value="(.*)".*>' % field
+        m = re.search(pattern, source)
         if m is None:
-            return None
+            # look in a select element
+            pattern = '<select .* name="field.%s".*>.*' \
+                '<option value="(.*)" selected>*.</select>' % field
+            m = re.search(pattern, source, re.DOTALL)
+            if m is None:
+                return None
 
-    try:
-        return parseDatetimetz(m.group(1))
-    except:
-        # ignore specifics
-        return None
+        request = self.makeRequest(env={"HTTP_ACCEPT_LANGUAGE": "ru"})
 
+        formatter = request.locale.dates.getFormatter("dateTime")
 
-class Test(BrowserTestCase):
+        return formatter.parse(m.group(1))
 
-    def setUp(self):
-        BrowserTestCase.setUp(self)
-        registerEditForm(IDatetimeTest)
-        defineSecurity(DatetimeTest, IDatetimeTest)
-
     def test_display_editform(self):
         self.getRootFolder()['test'] = DatetimeTest()
         transaction.commit()
         object = traverse(self.getRootFolder(), 'test')
 
         # display edit view
-        response = self.publish('/test/edit.html')
+        response = self.publish('/test/edit.html',
+            env={"HTTP_ACCEPT_LANGUAGE": "ru"})
         self.assertEqual(response.getStatus(), 200)
 
         # confirm date values in form with actual values
-        self.assertEqual(getDateForField('d1', response.getBody()), object.d1)
-        self.assertEqual(getDateForField('d2', response.getBody()), object.d2)
-        self.assert_(getDateForField('d3', response.getBody()) is None)
+        self.assertEqual(self.getDateForField('d1', response.getBody()),
+            object.d1)
+        self.assertEqual(self.getDateForField('d2', response.getBody()),
+            object.d2)
+        self.assert_(self.getDateForField('d3', response.getBody()) is None)
 
 
     def test_submit_editform(self):
         self.getRootFolder()['test'] = DatetimeTest()
         transaction.commit()
 
-        d1 = datetime(2003, 2, 1, tzinfo=tzinfo(0))
-        d2 = datetime(2003, 2, 2, tzinfo=tzinfo(0))
-        d3 = datetime(2003, 10, 15, tzinfo=tzinfo(0))
-
         # submit edit view
         response = self.publish('/test/edit.html', form={
             'UPDATE_SUBMIT' : '',
-            'field.d1' : str(d1),
-            'field.d2' : str(d2),
-            'field.d3' : str(d3) })
+            'field.d1' : u'Feb 1, 2003 12:00:00 AM',
+            'field.d2' : u'Feb 2, 2003 12:00:00 AM',
+            'field.d3' : u'2003-10-15 00:00:00' },
+            env={"HTTP_ACCEPT_LANGUAGE": "en"})
         self.assertEqual(response.getStatus(), 200)
         self.assert_(updatedMsgExists(response.getBody()))
 
         # check new values in object
         object = traverse(self.getRootFolder(), 'test')
 
-        self.assertEqual(object.d1, d1)
-        self.assertEqual(object.d2, d2)
-        self.assertEqual(object.d3, d3)
+        self.assertEqual(object.d1, datetime(2003, 2, 1))
+        self.assertEqual(object.d2, datetime(2003, 2, 2))
+        self.assertEqual(object.d3, datetime(2003, 10, 15))
 
 
     def test_missing_value(self):
@@ -143,7 +143,7 @@
         object = traverse(self.getRootFolder(), 'test')
         self.assert_(object.d2 is None) # default missing_value for dates
         # 2000-1-1 is missing_value for d3
-        self.assertEqual(object.d3, datetime(2000, 1, 1, tzinfo=tzinfo(0)))
+        self.assertEqual(object.d3, datetime(2000, 1, 1))
 
 
     def test_required_validation(self):
@@ -171,7 +171,7 @@
         # submit a value for d3 that isn't allowed
         response = self.publish('/test/edit.html', form={
             'UPDATE_SUBMIT' : '',
-            'field.d3' : str(datetime(2003, 2, 1, tzinfo=tzinfo(0))) })
+            'field.d3' : u'Feb 1, 2003 12:00:00 AM'})
         self.assertEqual(response.getStatus(), 200)
         self.assert_(invalidValueErrorExists('d3', response.getBody()))
 
@@ -183,7 +183,8 @@
         # submit value for d1 that is too low
         response = self.publish('/test/edit.html', form={
             'UPDATE_SUBMIT' : '',
-            'field.d1' : str(datetime(2002, 12, 31, tzinfo=tzinfo(0))) })
+            'field.d1' : u'Dec 31, 2002 12:00:00 AM'},
+            env={"HTTP_ACCEPT_LANGUAGE": "en"})
         self.assertEqual(response.getStatus(), 200)
         self.assert_(validationErrorExists('d1', 'Value is too small',
             response.getBody()))
@@ -191,7 +192,8 @@
         # submit value for i1 that is too high
         response = self.publish('/test/edit.html', form={
             'UPDATE_SUBMIT' : '',
-            'field.d1' : str(datetime(2021, 1, 1, tzinfo=tzinfo(0))) })
+            'field.d1' : u'Dec 1, 2021 12:00:00 AM'},
+            env={"HTTP_ACCEPT_LANGUAGE": "en"})
         self.assertEqual(response.getStatus(), 200)
         self.assert_(validationErrorExists('d1', 'Value is too big',
             response.getBody()))
@@ -230,5 +232,3 @@
 
 if __name__=='__main__':
     unittest.main(defaultTest='test_suite')
-
-

Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_browserwidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_browserwidget.py	2006-06-24 09:46:39 UTC (rev 68820)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_browserwidget.py	2006-06-24 09:46:59 UTC (rev 68821)
@@ -53,7 +53,7 @@
         self.content = TestObject()
         field = ITestContent['foo']
         field = field.bind(self.content)
-        request = TestRequest(HTTP_ACCEPT_LANGUAGE='pl')
+        request = TestRequest(HTTP_ACCEPT_LANGUAGE='ru')
         request.form['field.foo'] = u'Foo Value'
         self._widget = self._WidgetFactory(field, request)
 
@@ -86,10 +86,12 @@
         self.assertEqual(self._widget.cssClass, '')
         self.assertEqual(self._widget.extra, '')
 
-    def testRender(self):
-        value = 'Foo Value'
-        check_list = ('type="text"', 'id="field.foo"', 'name="field.foo"',
-                      'value="Foo Value"')
+    def testRender(self, value=None, check_list=None):
+        if value is None:
+            value = 'Foo Value'
+        if check_list is None:
+            check_list = ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                          'value="Foo Value"')
         self._widget.setRenderedValue(value)
         self.verifyResult(self._widget(), check_list)
         check_list = ('type="hidden"',) + check_list[1:]

Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_datetimewidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_datetimewidget.py	2006-06-24 09:46:39 UTC (rev 68820)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_datetimewidget.py	2006-06-24 09:46:59 UTC (rev 68821)
@@ -15,20 +15,22 @@
 
 $Id$
 """
+import datetime
 import unittest, doctest
-from zope.datetime import parseDatetimetz
+
+from zope.schema import Datetime
+from zope.datetime import tzinfo
+from zope.interface.verify import verifyClass
+
 from zope.app.form.browser.tests.test_browserwidget import SimpleInputWidgetTest
 from zope.app.form.interfaces import IInputWidget
 from zope.app.form.browser import DatetimeWidget
 from zope.app.form.interfaces import ConversionError, WidgetInputError
-from zope.interface.verify import verifyClass
 
-from zope.schema import Datetime
 
-
 class DatetimeWidgetTest(SimpleInputWidgetTest):
     """Documents and tests the datetime widget.
-        
+
         >>> verifyClass(IInputWidget, DatetimeWidget)
         True
     """
@@ -36,25 +38,97 @@
     _FieldFactory = Datetime
     _WidgetFactory = DatetimeWidget
 
+    def testDefaultDisplayStyle(self):
+        self.failIf(self._widget.displayStyle)
+
+    def testRender(self):
+        super(DatetimeWidgetTest, self).testRender(
+            datetime.datetime(2004, 3, 26, 12, 58, 59),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                'value="26.03.2004 12:58:59"'))
+
+    def testRenderShort(self):
+        self._widget.displayStyle = "short"
+        super(DatetimeWidgetTest, self).testRender(
+            datetime.datetime(2004, 3, 26, 12, 58, 59),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                'value="26.03.04 12:58"'))
+
+    def testRenderMedium(self):
+        self._widget.displayStyle = "medium"
+        super(DatetimeWidgetTest, self).testRender(
+            datetime.datetime(2004, 3, 26, 12, 58, 59),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                'value="26.03.2004 12:58:59"'))
+
+    def testRenderLong(self):
+        self._widget.displayStyle = "long"
+        super(DatetimeWidgetTest, self).testRender(
+            datetime.datetime(2004, 3, 26, 12, 58, 59),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                u'value="26 \u041c\u0430\u0440\u0442 2004 \u0433.'
+                u' 12:58:59 +000"'))
+
+    def testRenderFull(self):
+        self._widget.displayStyle = "full"
+        super(DatetimeWidgetTest, self).testRender(
+            datetime.datetime(2004, 3, 26, 12, 58, 59),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                u'value="26 \u041c\u0430\u0440\u0442 2004 \u0433.'
+                u' 12:58:59 +000"'))
+
     def test_hasInput(self):
         del self._widget.request.form['field.foo']
         self.failIf(self._widget.hasInput())
         # widget has input, even if input is an empty string
         self._widget.request.form['field.foo'] = u''
         self.failUnless(self._widget.hasInput())
-        self._widget.request.form['field.foo'] = u'2003/03/26 12:00:00'
+        self._widget.request.form['field.foo'] = u'26.03.2003 12:00:00'
         self.failUnless(self._widget.hasInput())
 
-    def test_getInputValue(self):
-        TEST_DATE= u'2003/03/26 12:00:00'
+    def test_getDefaultInputValue(self,
+            value=u'26.03.2004 12:58:59',
+            check_value=datetime.datetime(2004, 3, 26, 12, 58, 59)):
         self._widget.request.form['field.foo'] = u''
         self.assertRaises(WidgetInputError, self._widget.getInputValue)
-        self._widget.request.form['field.foo'] = TEST_DATE
-        self.assertEquals(self._widget.getInputValue(), parseDatetimetz(TEST_DATE))
+        self._widget.request.form['field.foo'] = value
+        self.assertEquals(self._widget.getInputValue(), check_value)
         self._widget.request.form['field.foo'] = u'abc'
         self.assertRaises(ConversionError, self._widget.getInputValue)
 
+    def test_getShortInputValue(self):
+        self._widget.displayStyle = "short"
+        self.test_getDefaultInputValue(
+            value=u'26.03.04 12:58:59',
+            check_value=datetime.datetime(2004, 3, 26, 12, 58)
+            )
 
+    def test_getMediumInputValue(self):
+        self._widget.displayStyle = "medium"
+        self.test_getDefaultInputValue(
+            value=u'26.03.2004 12:58:59',
+            check_value=datetime.datetime(2004, 3, 26, 12, 58, 59)
+            )
+
+    def test_getLongInputValue(self):
+        self._widget.displayStyle = "long"
+        self.test_getDefaultInputValue(
+            value=(u'26 \u041c\u0430\u0440\u0442 2004 \u0433.'
+                u' 12:58:59 +030'),
+            check_value=datetime.datetime(2004, 3, 26, 12, 58, 59,
+                tzinfo=tzinfo(30))
+            )
+
+    def test_getFullInputValue(self):
+        self._widget.displayStyle = "full"
+        self.test_getDefaultInputValue(
+            value=(u'26 \u041c\u0430\u0440\u0442 2004 \u0433.'
+                u' 12:58:59 +030'),
+            check_value=datetime.datetime(2004, 3, 26, 12, 58, 59,
+                tzinfo=tzinfo(30))
+            )
+
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(DatetimeWidgetTest),
@@ -63,4 +137,3 @@
 
 if __name__=='__main__':
     unittest.main(defaultTest='test_suite')
-

Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_datewidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_datewidget.py	2006-06-24 09:46:39 UTC (rev 68820)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_datewidget.py	2006-06-24 09:46:59 UTC (rev 68821)
@@ -15,15 +15,19 @@
 
 $Id$
 """
+import datetime
 import unittest, doctest
+
 from zope.datetime import parseDatetimetz
+from zope.schema import Date
+from zope.interface.verify import verifyClass
+
 from zope.app.form.browser.tests.test_browserwidget import SimpleInputWidgetTest
 from zope.app.form.interfaces import IInputWidget
 from zope.app.form.browser import DateWidget
 from zope.app.form.interfaces import ConversionError, WidgetInputError
-from zope.schema import Date
-from zope.interface.verify import verifyClass
 
+
 class DateWidgetTest(SimpleInputWidgetTest):
     """Documents and tests the date widget.
 
@@ -34,26 +38,82 @@
     _FieldFactory = Date
     _WidgetFactory = DateWidget
 
+    def testDefaultDisplayStyle(self):
+        self.failIf(self._widget.displayStyle)
+
+    def testRender(self):
+        super(DateWidgetTest, self).testRender(
+            datetime.date(2003, 3, 26),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                'value="26.03.2003"'))
+
+    def testRenderShort(self):
+        self._widget.displayStyle = "short"
+        super(DateWidgetTest, self).testRender(
+            datetime.datetime(2004, 3, 26, 12, 58, 59),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                'value="26.03.04"'))
+
+    def testRenderMedium(self):
+        self._widget.displayStyle = "medium"
+        super(DateWidgetTest, self).testRender(
+            datetime.datetime(2004, 3, 26, 12, 58, 59),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                'value="26.03.2004"'))
+
+    def testRenderLong(self):
+        self._widget.displayStyle = "long"
+        super(DateWidgetTest, self).testRender(
+            datetime.datetime(2004, 3, 26, 12, 58, 59),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                u'value="26 \u041c\u0430\u0440\u0442 2004 \u0433."'))
+
+    def testRenderFull(self):
+        self._widget.displayStyle = "full"
+        super(DateWidgetTest, self).testRender(
+            datetime.datetime(2004, 3, 26, 12, 58, 59),
+            ('type="text"', 'id="field.foo"', 'name="field.foo"',
+                u'value="26 \u041c\u0430\u0440\u0442 2004 \u0433."'))
+
     def test_hasInput(self):
         del self._widget.request.form['field.foo']
         self.failIf(self._widget.hasInput())
         self._widget.request.form['field.foo'] = u''
         self.failUnless(self._widget.hasInput())
-        self._widget.request.form['field.foo'] = u'2003/03/26'
+        self._widget.request.form['field.foo'] = u'26.03.2003'
         self.failUnless(self._widget.hasInput())
 
-    def test_getInputValue(self):
-        TEST_DATE= u'2003/03/26'
+    def test_getDefaultInputValue(self,
+            value=u'26.03.2004',
+            check_value=datetime.date(2004, 3, 26)):
         self._widget.request.form['field.foo'] = u''
         self.assertRaises(WidgetInputError, self._widget.getInputValue)
-        self._widget.request.form['field.foo'] = TEST_DATE
-        self.assertEquals(
-            self._widget.getInputValue(), 
-            parseDatetimetz(TEST_DATE).date())
+        self._widget.request.form['field.foo'] = value
+        self.assertEquals(self._widget.getInputValue(), check_value)
         self._widget.request.form['field.foo'] = u'abc'
         self.assertRaises(ConversionError, self._widget.getInputValue)
 
+    def test_getShortInputValue(self):
+        self._widget.displayStyle = "short"
+        self.test_getDefaultInputValue(u'26.03.04')
 
+    def test_getMediumInputValue(self):
+        self._widget.displayStyle = "medium"
+        self.test_getDefaultInputValue(u'26.03.2004')
+
+    def test_getLongInputValue(self):
+        self._widget.displayStyle = "long"
+        self.test_getDefaultInputValue(
+            u'26 \u041c\u0430\u0440\u0442 2004 \u0433.'
+            )
+
+    def test_getFullInputValue(self):
+        self._widget.displayStyle = "full"
+        self.test_getDefaultInputValue(
+            u'26 \u041c\u0430\u0440\u0442 2004 \u0433.'
+            )
+
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(DateWidgetTest),
@@ -62,4 +122,3 @@
 
 if __name__=='__main__':
     unittest.main(defaultTest='test_suite')
-

Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py	2006-06-24 09:46:39 UTC (rev 68820)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py	2006-06-24 09:46:59 UTC (rev 68821)
@@ -168,7 +168,7 @@
         self.verifyResult(self._widget(),
                           ["<span",
                            'class="%s"' % self.expected_class,
-                           "2004-12-01",
+                           "01.12.2004",
                            "</span"])
 
     def testRenderShort(self):
@@ -177,7 +177,7 @@
         self.verifyResult(self._widget(),
                           ["<span",
                            'class="%s"' % self.expected_class,
-                           u"04-12-01",
+                           u"01.12.04",
                            "</span"])
 
     def testRenderMedium(self):
@@ -186,7 +186,7 @@
         self.verifyResult(self._widget(),
                           ["<span",
                            'class="%s"' % self.expected_class,
-                           u"2004-12-01",
+                           u"01.12.2004",
                            "</span"])
 
     def testRenderLong(self):
@@ -195,7 +195,8 @@
         self.verifyResult(self._widget(),
                           ["<span",
                            'class="%s"' % self.expected_class,
-                           u"1 grudzie\u0144 2004",
+                           u"1 \u0414\u0435\u043a\u0430\u0431\u0440\u044c"
+                                u" 2004 \u0433.",
                            "</span"])
 
     def testRenderFull(self):
@@ -204,7 +205,8 @@
         self.verifyResult(self._widget(),
                           ["<span",
                            'class="%s"' % self.expected_class,
-                           u"1 grudzie\u0144 2004",
+                           u"1 \u0414\u0435\u043a\u0430\u0431\u0440\u044c"
+                                u" 2004 \u0433.",
                            "</span"])
 
 

Modified: Zope3/trunk/src/zope/app/form/browser/textwidgets.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2006-06-24 09:46:39 UTC (rev 68820)
+++ Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2006-06-24 09:46:59 UTC (rev 68821)
@@ -19,8 +19,7 @@
 
 from xml.sax import saxutils
 from zope.interface import implements
-from zope.datetime import parseDatetimetz
-from zope.datetime import DateTimeError
+from zope.i18n.format import DateTimeParseError
 
 from zope.app.form.interfaces import IInputWidget, ConversionError
 from zope.app.form.browser.interfaces import ITextBrowserWidget
@@ -302,7 +301,7 @@
             except ValueError, v:
                 raise ConversionError(_("Invalid unicode data"), v)
             else:
-                value = value.replace("\r\n", "\n")                
+                value = value.replace("\r\n", "\n")
         return value
 
     def _toFormValue(self, value):
@@ -312,7 +311,7 @@
             value = escape(value)
         else:
             value = u''
-            
+
         return value
 
     def __call__(self):
@@ -484,33 +483,59 @@
             except ValueError, v:
                 raise ConversionError(_("Invalid floating point data"), v)
 
+class DateWidget(TextWidget):
+    """Date entry widget.
 
-class DatetimeWidget(TextWidget):
-    """Datetime entry widget."""
+    The `displayStyle` attribute may be set to control the formatting of the
+    value.
 
+    `displayStyle` must be one of 'full', 'long', 'medium', 'short',
+    or None ('' is accepted an an alternative to None to support
+    provision of a value from ZCML).
+    """
+
+    _category = "date"
+
     displayWidth = 20
 
+    displayStyle = None
+
     def _toFieldValue(self, input):
         if input == self._missing:
             return self.context.missing_value
         else:
             try:
-                return parseDatetimetz(input)
-            except (DateTimeError, ValueError, IndexError), v:
-                raise ConversionError(_("Invalid datetime data"), v)
+                formatter = self.request.locale.dates.getFormatter(
+                    self._category, (self.displayStyle or None))
+                return formatter.parse(input)
+            except (DateTimeParseError, ValueError), v:
+                raise ConversionError(_("Invalid datetime data"),
+                    "%s (%r)" % (v, input))
 
+    def _toFormValue(self, value):
+        value = super(DateWidget, self)._toFormValue(value)
+        if value:
+            formatter = self.request.locale.dates.getFormatter(
+                self._category, (self.displayStyle or None))
+            value = formatter.format(value)
+        return value
 
-class DateWidget(DatetimeWidget):
-    """Date entry widget.
+class DatetimeWidget(DateWidget):
+    """Datetime entry widget.
+
+    The `displayStyle` attribute may be set to control the formatting of the
+    value.
+
+    `displayStyle` must be one of 'full', 'long', 'medium', 'short',
+    or None ('' is accepted an an alternative to None to support
+    provision of a value from ZCML).
+
+    NOTE: If you need timezone information you need to set `displayStyle`
+    to either 'long' or 'full' since other display styles just ignore it.
     """
 
-    def _toFieldValue(self, input):
-        v = super(DateWidget, self)._toFieldValue(input)
-        if v != self.context.missing_value:
-            v = v.date()
-        return v
+    _category = "dateTime"
 
-
 class DateDisplayWidget(DisplayWidget):
     """Date display widget.
 
@@ -520,7 +545,6 @@
     `displayStyle` must be one of 'full', 'long', 'medium', 'short',
     or None ('' is accepted an an alternative to None to support
     provision of a value from ZCML).
-
     """
 
     cssClass = "date"
@@ -551,7 +575,6 @@
     `displayStyle` must be one of 'full', 'long', 'medium', 'short',
     or None ('' is accepted an an alternative to None to support
     provision of a value from ZCML).
-
     """
 
     cssClass = "dateTime"

Modified: Zope3/trunk/src/zope/formlib/form.txt
===================================================================
--- Zope3/trunk/src/zope/formlib/form.txt	2006-06-24 09:46:39 UTC (rev 68820)
+++ Zope3/trunk/src/zope/formlib/form.txt	2006-06-24 09:46:59 UTC (rev 68821)
@@ -1554,7 +1554,7 @@
     <input class="textType" id="form.max_size" name="form.max_size" 
            size="10" type="text" value=""  />
     <input class="textType" id="form.now" name="form.now" size="20" 
-           type="text" value="2002-12-02 12:30:00"  />
+           type="text" value="2002 12 2  12:30:00 "  />
 
 Note that a EditForm can't make use of a get_rendered method. The get_rendered
 method does only set initial values.



More information about the Zope3-Checkins mailing list