Added dates range field and widget ZTK-1.1
authorThierry Florac <tflorac@ulthar.net>
Tue, 20 Nov 2012 17:51:28 +0100
branchZTK-1.1
changeset 190 5152d59c147c
parent 189 c9ce3999f76b
child 191 bbb1270b179e
Added dates range field and widget
src/ztfy/utils/browser/configure.zcml
src/ztfy/utils/browser/daterange.py
src/ztfy/utils/browser/templates/daterange_display.pt
src/ztfy/utils/browser/templates/daterange_input.pt
src/ztfy/utils/schema.py
--- a/src/ztfy/utils/browser/configure.zcml	Tue Nov 13 16:03:37 2012 +0100
+++ b/src/ztfy/utils/browser/configure.zcml	Tue Nov 20 17:51:28 2012 +0100
@@ -29,8 +29,7 @@
 		weight="10" />
 
 	<!-- Encoding selection widget -->
-	<adapter
-		factory=".encoding.EncodingSelectFieldWidget" />
+	<adapter factory=".encoding.EncodingSelectFieldWidget" />
 
 	<class class=".encoding.EncodingSelectWidget">
 		<require
@@ -39,8 +38,7 @@
 	</class>
 
 	<!-- Color selection widget -->
-	<adapter
-		factory=".color.ColorFieldWidgetFactory" />
+	<adapter factory=".color.ColorFieldWidgetFactory" />
 
 	<class class=".color.ColorWidget">
 		<require
@@ -60,12 +58,33 @@
 		widget=".color.IColorWidget"
 		layer="ztfy.skin.layer.IZTFYBrowserLayer" />
 
+	<!-- Dates range widget -->
+	<adapter factory=".daterange.DatesRangeDataConverter" />
+
+	<adapter factory=".daterange.DatesRangeFieldWidgetFactory" />
+
+	<class class=".daterange.DatesRangeWidget">
+		<require
+			interface=".daterange.IDatesRangeWidget"
+			permission="zope.Public" />
+	</class>
+
+	<z3c:widgetTemplate
+		mode="input"
+		template="templates/daterange_input.pt"
+		widget=".daterange.IDatesRangeWidget"
+		layer="ztfy.skin.layer.IZTFYBrowserLayer" />
+
+	<z3c:widgetTemplate
+		mode="display"
+		template="templates/daterange_display.pt"
+		widget=".daterange.IDatesRangeWidget"
+		layer="ztfy.skin.layer.IZTFYBrowserLayer" />
+
 	<!-- TextLine list widget -->
-	<adapter
-		factory=".textlinelist.TextLineListDataConverter" />
+	<adapter factory=".textlinelist.TextLineListDataConverter" />
 
-	<adapter
-		factory=".textlinelist.TextLineListFieldWidgetFactory" />
+	<adapter factory=".textlinelist.TextLineListFieldWidgetFactory" />
 
 	<class class=".textlinelist.TextLineListWidget">
 		<require
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/utils/browser/daterange.py	Tue Nov 20 17:51:28 2012 +0100
@@ -0,0 +1,116 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2012 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+
+# import standard packages
+
+# import Zope3 interfaces
+from z3c.form.interfaces import IMultiWidget, IFieldWidget, NO_VALUE
+
+# import local interfaces
+from ztfy.skin.layer import IZTFYBrowserLayer
+from ztfy.utils.schema import IDatesRangeField
+
+# import Zope3 packages
+from z3c.form.browser.widget import HTMLFormElement
+from z3c.form.converter import SequenceDataConverter, FormatterValidationError
+from z3c.form.widget import FieldWidget, Widget
+from zope.component import adapter, adapts
+from zope.interface import implementer, implements
+from zope.i18n.format import DateTimeParseError
+
+# import local packages
+from ztfy.jqueryui import jquery_datetime
+
+
+class IDatesRangeWidget(IMultiWidget):
+    """Dates range widget interface"""
+
+
+class DatesRangeDataConverter(SequenceDataConverter):
+    """Dates range data converter"""
+
+    adapts(IDatesRangeField, IDatesRangeWidget)
+
+    def toWidgetValue(self, value):
+        if value is self.field.missing_value:
+            return (u'', u'')
+        locale = self.widget.request.locale
+        formatter = locale.dates.getFormatter('date', 'short')
+        return (formatter.format(value[0]) if value[0] else None,
+                formatter.format(value[1]) if value[1] else None)
+
+    def toFieldValue(self, value):
+        if not value:
+            return self.field.missing_value
+        try:
+            locale = self.widget.request.locale
+            formatter = locale.dates.getFormatter('date', 'short')
+            return (formatter.parse(value[0]) if value[0] else None,
+                    formatter.parse(value[1]) if value[1] else None)
+        except DateTimeParseError, err:
+            raise FormatterValidationError(err.args[0], value)
+
+
+class DatesRangeWidget(HTMLFormElement, Widget):
+    """Dates range widget"""
+
+    implements(IDatesRangeWidget)
+
+    @property
+    def pattern(self):
+        result = self.request.locale.dates.getFormatter('date', 'short').getPattern()
+        return result.replace('dd', '%d').replace('MM', '%m').replace('yy', '%y')
+
+    @property
+    def begin_id(self):
+        return '%s-begin' % self.id
+
+    @property
+    def begin_name(self):
+        return '%s.begin' % self.name
+
+    @property
+    def begin_date(self):
+        return (self.value[0] or '') if self.value else ''
+
+    @property
+    def end_id(self):
+        return '%s-end' % self.id
+
+    @property
+    def end_name(self):
+        return '%s.end' % self.name
+
+    @property
+    def end_date(self):
+        return (self.value[1] or '') if self.value else ''
+
+    def extract(self, default=NO_VALUE):
+        begin_date = self.request.get(self.begin_name)
+        end_date = self.request.get(self.end_name)
+        return (begin_date, end_date)
+
+    def render(self):
+        result = super(DatesRangeWidget, self).render()
+        if result:
+            jquery_datetime.need()
+        return result
+
+@adapter(IDatesRangeField, IZTFYBrowserLayer)
+@implementer(IFieldWidget)
+def DatesRangeFieldWidgetFactory(field, request):
+    """IDatesRangeField widget factory"""
+    return FieldWidget(field, DatesRangeWidget(request))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/utils/browser/templates/daterange_display.pt	Tue Nov 20 17:51:28 2012 +0100
@@ -0,0 +1,6 @@
+<div tal:attributes="class string:daterange ${view/klass}" i18n:domain="ztfy.utils">
+	<span i18n:translate="">Between:</label>
+	<span tal:content="python:view.begin_date or '--'" />&nbsp;
+	<span i18n:translate="">and:</label>
+	<span tal:content="python:view.end_date or '--';" />
+</div>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/utils/browser/templates/daterange_input.pt	Tue Nov 20 17:51:28 2012 +0100
@@ -0,0 +1,25 @@
+<div tal:attributes="class string:daterange ${view/klass}" i18n:domain="ztfy.utils">
+	<label tal:attributes="for view/begin_id" i18n:translate="">Between:</label>
+	<input type="text" class="date-field" tal:attributes="id view/begin_id;
+														  name view/begin_name;
+														  value view/begin_date;" />&nbsp;
+	<label tal:attributes="for view/end_id" i18n:translate="">and:</label>
+	<input type="text" class="date-field" tal:attributes="id view/end_id;
+														  name view/end_name;
+														  value view/end_date;" />
+</div>
+<script type="text/javascript" tal:content="structure string:
+	// <![CDATA[
+	$$(document).ready(function() {
+		if (typeof($$.i18n_calendar) == 'undefined') {
+			var lang = $$('html').attr('lang') || $$('html').attr('xml:lang');
+			$$.i18n_calendar = lang;
+			$$.getScript('/--static--/ztfy.jqueryui/js/lang/calendar-' + lang + '-utf8.js');
+		}
+		$$('#${view/begin_id},#${view/end_id}').dynDateTime({
+			showsTime: false,
+			ifFormat: '${view/pattern}'
+		});
+	});
+	// ]]>
+" />
\ No newline at end of file
--- a/src/ztfy/utils/schema.py	Tue Nov 13 16:03:37 2012 +0100
+++ b/src/ztfy/utils/schema.py	Tue Nov 20 17:51:28 2012 +0100
@@ -20,7 +20,7 @@
 
 # import Zope3 interfaces
 from z3c.form.interfaces import IWidget
-from zope.schema.interfaces import ITextLine, IDecimal, IList
+from zope.schema.interfaces import ITextLine, IDecimal, IList, ITuple
 
 # import local interfaces
 
@@ -28,7 +28,7 @@
 from z3c.form.converter import BaseDataConverter, FormatterValidationError
 from zope.component import adapts
 from zope.interface import implements
-from zope.schema import TextLine, Decimal, List
+from zope.schema import TextLine, Decimal, List, Tuple, Date
 from zope.schema._bootstrapfields import InvalidValue
 
 # import local packages
@@ -111,6 +111,24 @@
 
 
 #
+# Dates range field
+#
+
+class IDatesRangeField(ITuple):
+    """Marker interface for dates range fields"""
+
+
+class DatesRangeField(Tuple):
+    """Dates range field"""
+
+    implements(IDatesRangeField)
+
+    def __init__(self, value_type=None, unique=False, **kw):
+        super(DatesRangeField, self).__init__(value_type=None, unique=False,
+                                              min_length=2, max_length=2, **kw)
+
+
+#
 # TextLine list field
 #