[Checkins] SVN: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/ initial version of a dropdown date widget

Oliver Petznick oliver at mopa.at
Fri Feb 2 06:39:38 EST 2007


Log message for revision 72322:
  initial version of a dropdown date widget

Changed:
  A   z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/
  A   z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/README.txt
  A   z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/__init__.py
  A   z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/overrides.zcml
  A   z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/tests.py
  A   z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/widget.py
  A   z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/z3c.widget.dropdowndatewidget-overrides.zcml

-=-
Added: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/README.txt
===================================================================
--- z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/README.txt	2007-02-02 09:38:37 UTC (rev 72321)
+++ z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/README.txt	2007-02-02 11:39:36 UTC (rev 72322)
@@ -0,0 +1,57 @@
+DropDownDateWidget
+==================
+
+  >>> from z3c.widget.dropdowndatewidget.widget import DropDownDateWidget
+
+  >>> from zope.publisher.browser import TestRequest
+  >>> request = TestRequest()
+
+Widgets are use for fields.
+
+  >>> from zope.schema import Date
+  >>> dateField = Date(__name__='foo', title=u'Foo')
+
+  >>> widget = DropDownDateWidget(dateField, request)
+
+  >>> widget.name
+  'field.foo'
+  >>> widget.label
+  u'Foo'
+  >>> widget.hasInput()
+  False
+
+We need to provide some input.
+
+  >>> request.form['field.foo.day'] = '1'
+  >>> widget.hasInput()
+  False
+  >>> request.form['field.foo.month'] = '6'
+  >>> widget.hasInput()
+  False
+  >>> request.form['field.foo.year'] = '1963'
+  >>> widget.hasInput()
+  True
+
+Read the value.
+
+  >>> widget.getInputValue()
+  datetime.date(1963, 6, 1)
+
+Let's render the widget.
+
+  >>> print widget()
+  <div class="dropDownDateWidget"><select class="dayField" id="field.foo.day" name="field.foo.day">...</select>
+  <select class="monthField" id="field.foo.month" name="field.foo.month">...</select>
+  <select class="yearField" id="field.foo.year" name="field.foo.year">...</select>
+  </div>
+
+And if we set a value.
+
+  >>> from datetime import date
+  >>> widget.setRenderedValue(date(1977, 4, 3))
+  >>> print widget()
+  <div class="dropDownDateWidget"><select ...<option selected="selected" value="03">...
+  <select ...<option selected="selected" value="04">...
+  <select ...<option selected="selected" value="1977">...
+  ...
+


Property changes on: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/__init__.py
===================================================================


Property changes on: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/overrides.zcml
===================================================================
--- z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/overrides.zcml	2007-02-02 09:38:37 UTC (rev 72321)
+++ z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/overrides.zcml	2007-02-02 11:39:36 UTC (rev 72322)
@@ -0,0 +1,12 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope">
+
+  <view
+      type="zope.publisher.interfaces.browser.IBrowserRequest"
+      for="zope.schema.interfaces.IDate"
+      provides="zope.app.form.interfaces.IInputWidget"
+      factory=".widget.DropDownDateWidget"
+      permission="zope.Public"
+      />
+
+</configure>
\ No newline at end of file


Property changes on: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/overrides.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/tests.py
===================================================================
--- z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/tests.py	2007-02-02 09:38:37 UTC (rev 72321)
+++ z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/tests.py	2007-02-02 11:39:36 UTC (rev 72322)
@@ -0,0 +1,15 @@
+import unittest
+
+from zope.testing import doctest
+from zope.testing.doctestunit import DocFileSuite
+
+
+def test_suite():
+    return unittest.TestSuite((
+        DocFileSuite('README.txt',
+             optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+             ),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/widget.py
===================================================================
--- z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/widget.py	2007-02-02 09:38:37 UTC (rev 72321)
+++ z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/widget.py	2007-02-02 11:39:36 UTC (rev 72322)
@@ -0,0 +1,92 @@
+import datetime
+
+from zope.datetime import parseDatetimetz
+
+from zope.app.form.browser.widget import SimpleInputWidget, renderElement
+from zope.app.form.interfaces import ConversionError
+from zope.app.i18n import ZopeMessageFactory as _
+
+
+
+class DropDownDateWidget(SimpleInputWidget):
+
+    _missing = None
+
+    day   = [str(i+1).zfill(2) for i in range(31)]
+    month = [str(i+1).zfill(2) for i in range(12)]
+    year  = [str(i+1900) for i in range(130)]
+
+    elements = [[day,'day'],[month,'month'],[year,'year']]
+
+    def __call__(self, *args, **kw):
+        html = ''
+        data = self._getFormValue()
+        if data is None or data == self._data_marker:
+            data = datetime.date.today()
+        # day
+        contents = self.renderItemsWithValues(self.elements[0][0], data.day)
+        name = self.name+'.'+self.elements[0][1]
+        html += renderElement('select',
+                              name=name,
+                              id=name,
+                              contents=contents,
+                              cssClass='dayField')
+        html += '\n'
+        # month
+        contents = self.renderItemsWithValues(self.elements[1][0],data.month)
+        name = self.name+'.'+self.elements[1][1]
+        html += renderElement('select',
+                              name=name,
+                              id=name,
+                              contents=contents,
+                              cssClass='monthField')
+        html += '\n'
+        # year
+        contents = self.renderItemsWithValues(self.elements[2][0],data.year)
+        name = self.name+'.'+self.elements[2][1]
+        html += renderElement('select',
+                              name=name,
+                              id=name,
+                              contents=contents,
+                              cssClass='yearField')
+        html += '\n'
+        return renderElement('div',contents=html,cssClass='dropDownDateWidget')
+
+
+    def renderItemsWithValues(self, values, selected=None):
+        """ Render all values from a dropdown """
+        html = ''
+        for value in values:
+            if selected==int(value):
+                html += renderElement('option',
+                                      value=value,
+                                      selected='selected',
+                                      contents=value)
+            else:
+                html += renderElement('option',
+                                      value=value,
+                                      contents=value)
+        return html
+
+
+    def hasInput(self):
+        prefix = self.name + '.'
+        requestForm = self.request.form
+        return prefix + self.elements[0][1] in requestForm and \
+               prefix + self.elements[1][1] in requestForm and \
+               prefix + self.elements[2][1] in requestForm
+
+
+    def _getFormInput(self):
+        prefix = self.name + '.'
+        year = int(self.request.form.get(prefix+self.elements[2][1],None))
+        month = int(self.request.form.get(prefix+self.elements[1][1],None))
+        day = int(self.request.form.get(prefix+self.elements[0][1],None))
+        if year and month and day:
+            try:
+                return datetime.date(year, month, day)
+            except ValueError, v:
+                raise ConversionError(_("Invalid datetime data"), v)
+        else:
+            return None
+


Property changes on: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/widget.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/z3c.widget.dropdowndatewidget-overrides.zcml
===================================================================
--- z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/z3c.widget.dropdowndatewidget-overrides.zcml	2007-02-02 09:38:37 UTC (rev 72321)
+++ z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/z3c.widget.dropdowndatewidget-overrides.zcml	2007-02-02 11:39:36 UTC (rev 72322)
@@ -0,0 +1 @@
+<include package="z3c.widget.dropdowndatewidget" file="overrides.zcml"/>
\ No newline at end of file


Property changes on: z3c.widget/trunk/src/z3c/widget/dropdowndatewidget/z3c.widget.dropdowndatewidget-overrides.zcml
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list