[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/ add default display widgets for Date and Datetime fields

Fred L. Drake, Jr. fdrake at gmail.com
Wed Dec 1 15:34:36 EST 2004


Log message for revision 28550:
  add default display widgets for Date and Datetime fields

Changed:
  U   Zope3/trunk/src/zope/app/form/browser/__init__.py
  U   Zope3/trunk/src/zope/app/form/browser/configure.zcml
  U   Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py
  U   Zope3/trunk/src/zope/app/form/browser/textwidgets.py

-=-
Modified: Zope3/trunk/src/zope/app/form/browser/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/__init__.py	2004-12-01 20:20:54 UTC (rev 28549)
+++ Zope3/trunk/src/zope/app/form/browser/__init__.py	2004-12-01 20:34:35 UTC (rev 28550)
@@ -25,6 +25,8 @@
 from zope.app.form.browser.textwidgets import ASCIIWidget
 from zope.app.form.browser.textwidgets import IntWidget, FloatWidget
 from zope.app.form.browser.textwidgets import DatetimeWidget, DateWidget
+from zope.app.form.browser.textwidgets import DatetimeDisplayWidget
+from zope.app.form.browser.textwidgets import DateDisplayWidget
 from zope.app.form.browser.textwidgets import BytesDisplayWidget
 
 # Widgets for boolean fields

Modified: Zope3/trunk/src/zope/app/form/browser/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/configure.zcml	2004-12-01 20:20:54 UTC (rev 28549)
+++ Zope3/trunk/src/zope/app/form/browser/configure.zcml	2004-12-01 20:34:35 UTC (rev 28550)
@@ -78,6 +78,22 @@
       permission="zope.Public"
       />
 
+  <view
+      type="zope.publisher.interfaces.browser.IBrowserRequest"
+      for="zope.schema.interfaces.IDate"
+      provides="zope.app.form.interfaces.IDisplayWidget"
+      factory=".DateDisplayWidget"
+      permission="zope.Public"
+      />
+
+  <view
+      type="zope.publisher.interfaces.browser.IBrowserRequest"
+      for="zope.schema.interfaces.IDatetime"
+      provides="zope.app.form.interfaces.IDisplayWidget"
+      factory=".DatetimeDisplayWidget"
+      permission="zope.Public"
+      />
+
   <!-- Core edit widgets -->
 
   <view

Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py	2004-12-01 20:20:54 UTC (rev 28549)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py	2004-12-01 20:34:35 UTC (rev 28550)
@@ -15,7 +15,9 @@
 
 $Id$
 """
-import unittest, doctest
+import datetime
+import doctest
+import unittest
 
 from zope.interface.verify import verifyClass
 from zope.schema import TextLine
@@ -23,6 +25,7 @@
 
 from zope.app.form.interfaces import IInputWidget
 from zope.app.form.browser import TextWidget
+from zope.app.form.browser import DateDisplayWidget, DatetimeDisplayWidget
 from zope.app.form.browser.textwidgets import URIDisplayWidget
 from zope.app.tests.placelesssetup import setUp, tearDown
 from zope.app.form.browser.tests.test_browserwidget import BrowserWidgetTest
@@ -136,10 +139,101 @@
                                            'target="there"'])
 
 
+class DateDisplayWidgetTest(BrowserWidgetTest):
+
+    _WidgetFactory = DateDisplayWidget
+
+    expected_class = "date"
+
+    def setUp(self):
+        super(DateDisplayWidgetTest, self).setUp()
+        self._value = datetime.date(2004, 12, 01)
+
+    def testDefaultDisplayStyle(self):
+        self.failIf(self._widget.displayStyle)
+
+    def testRenderDefault(self):
+        self._widget.setRenderedValue(self._value)
+        self.verifyResult(self._widget(),
+                          ["<span",
+                           'class="%s"' % self.expected_class,
+                           "2004-12-01",
+                           "</span"])
+
+    def testRenderShort(self):
+        self._widget.setRenderedValue(self._value)
+        self._widget.displayStyle = "short"
+        self.verifyResult(self._widget(),
+                          ["<span",
+                           'class="%s"' % self.expected_class,
+                           u"04-12-01",
+                           "</span"])
+
+    def testRenderMedium(self):
+        self._widget.setRenderedValue(self._value)
+        self._widget.displayStyle = "medium"
+        self.verifyResult(self._widget(),
+                          ["<span",
+                           'class="%s"' % self.expected_class,
+                           u"2004-12-01",
+                           "</span"])
+
+    def testRenderLong(self):
+        self._widget.setRenderedValue(self._value)
+        self._widget.displayStyle = "long"
+        self.verifyResult(self._widget(),
+                          ["<span",
+                           'class="%s"' % self.expected_class,
+                           u"1 grudzie\u0144 2004",
+                           "</span"])
+
+    def testRenderFull(self):
+        self._widget.setRenderedValue(self._value)
+        self._widget.displayStyle = "full"
+        self.verifyResult(self._widget(),
+                          ["<span",
+                           'class="%s"' % self.expected_class,
+                           u"1 grudzie\u0144 2004",
+                           "</span"])
+
+
+class DatetimeDisplayWidgetTest(DateDisplayWidgetTest):
+
+    _WidgetFactory = DatetimeDisplayWidget
+
+    expected_class = "dateTime"
+
+    def setUp(self):
+        super(DatetimeDisplayWidgetTest, self).setUp()
+        self._value = datetime.datetime(2004, 12, 01, 14, 39, 01)
+
+    def testRenderDefault(self):
+        super(DatetimeDisplayWidgetTest, self).testRenderDefault()
+        self.verifyResult(self._widget(), ["14:39:01"])
+
+    def testRenderShort(self):
+        super(DatetimeDisplayWidgetTest, self).testRenderShort()
+        self.verifyResult(self._widget(), ["14:39"])
+
+    def testRenderMedium(self):
+        super(DatetimeDisplayWidgetTest, self).testRenderMedium()
+        self.verifyResult(self._widget(), ["14:39:01"])
+
+    def testRenderLong(self):
+        super(DatetimeDisplayWidgetTest, self).testRenderLong()
+        self.verifyResult(self._widget(), ["14:39:01 +000"])
+
+    def testRenderFull(self):
+        super(DatetimeDisplayWidgetTest, self).testRenderFull()
+        self.verifyResult(self._widget(), ["14:39:01 +000"])
+
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(TextWidgetTest),
         unittest.makeSuite(URIDisplayWidgetTest),
+        unittest.makeSuite(DateDisplayWidgetTest),
+        unittest.makeSuite(DatetimeDisplayWidgetTest),
         doctest.DocTestSuite(),
         ))
 

Modified: Zope3/trunk/src/zope/app/form/browser/textwidgets.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2004-12-01 20:20:54 UTC (rev 28549)
+++ Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2004-12-01 20:34:35 UTC (rev 28550)
@@ -479,3 +479,49 @@
                 return parseDatetimetz(input).date()
             except (DateTimeError, ValueError, IndexError), v:
                 raise ConversionError("Invalid datetime data", v)
+
+
+class DateDisplayWidget(DisplayWidget):
+    """Date display widget.
+
+    The `cssClass` and `displayStyle` attributes 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).
+
+    """
+
+    cssClass = "date"
+    displayStyle = None
+
+    _category = "date"
+
+    def __call__(self):
+        if self._renderedValueSet():
+            content = self._data
+        else:
+            content = self.context.default
+        formatter = self.request.locale.dates.getFormatter(
+            self._category, (self.displayStyle or None))
+        content = formatter.format(content)
+        return renderElement("span", contents=escape(content),
+                             cssClass=self.cssClass)
+
+
+class DatetimeDisplayWidget(DateDisplayWidget):
+    """Datetime display widget.
+
+    The `cssClass` and `displayStyle` attributes 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).
+
+    """
+
+    cssClass = "dateTime"
+
+    _category = "dateTime"



More information about the Zope3-Checkins mailing list