[Erp5-report] r12585 - in /erp5/trunk/products/ERP5Form: DurationField.py __init__.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Thu Feb 8 18:45:38 CET 2007
Author: romain
Date: Thu Feb 8 18:45:36 2007
New Revision: 12585
URL: http://svn.erp5.org?rev=12585&view=rev
Log:
Add DurationField.
Duration Widget is used to enter time duration.
It may be used in movement of Labour (in Task, Calendar Period, ...).
Time duration in ERP5 are saved ALWAYS IN SECOND.
The field purpose is to display second quantity in hour, minute and second,
in order to make it more user friendly.
Added:
erp5/trunk/products/ERP5Form/DurationField.py
Modified:
erp5/trunk/products/ERP5Form/__init__.py
Added: erp5/trunk/products/ERP5Form/DurationField.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Form/DurationField.py?rev=12585&view=auto
==============================================================================
--- erp5/trunk/products/ERP5Form/DurationField.py (added)
+++ erp5/trunk/products/ERP5Form/DurationField.py Thu Feb 8 18:45:36 2007
@@ -1,0 +1,131 @@
+##############################################################################
+#
+# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
+# Romain Courteaud <romain at nexedi.com>
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsability of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# garantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+##############################################################################
+
+from Products.Formulator import Widget, Validator
+from Products.Formulator.Field import ZMIField
+from Products.Formulator import StandardFields
+from Products.Formulator.DummyField import fields
+from Products.PythonScripts.Utility import allow_class
+from Products.ERP5Form import FormulatorPatch
+
+from zLOG import LOG
+from AccessControl import ClassSecurityInfo
+from Products.Formulator.Errors import ValidationError
+
+MINUTE_IN_SECOND = 60
+HOUR_IN_SECOND = 3600
+# DAY_IN_SECOND = 86400
+
+class DurationWidget(FormulatorPatch.FloatWidget):
+ """
+ Duration Widget is used to enter time duration.
+ It may be used in movement of Labour (in Task, Calendat Period, ...).
+
+ Time duration in ERP5 are saved ALWAYS IN SECOND.
+
+ The field purpose is to display second quantity in hour, minute and second,
+ in order to make it more readable.
+ """
+ def render_htmlgrid(self, field, key, value, REQUEST):
+ sub_field_render_list = []
+ for title, sub_key, convertion in (('Hour', 'hour', HOUR_IN_SECOND),
+ ('Minute', 'minute', MINUTE_IN_SECOND)):
+ sub_value, value = divmod(value, convertion)
+ sub_field_render_list.append((title, self.render_sub_field(
+ field, key,
+ sub_value, REQUEST, sub_key)))
+ # Render second
+ sub_field_render_list.append(('Second', self.render_sub_field(
+ field, key,
+ value, REQUEST, 'second')))
+ return sub_field_render_list
+
+ def render_sub_field(self, field, key, value, REQUEST, keyword):
+ """
+ Render dynamically a subfield
+ """
+ return FormulatorPatch.FloatWidgetInstance.render(
+ field,
+ field.generate_subfield_key(keyword,
+ key=key),
+ value,
+ REQUEST)
+
+class DurationValidator(Validator.FloatValidator):
+ """
+ Duration Validator
+ """
+ def validate(self, field, key, REQUEST):
+ second_value = None
+ for sub_key, convertion in (('hour', HOUR_IN_SECOND),
+ ('minute', MINUTE_IN_SECOND),
+ ('second', 1)):
+ second_value = self._validate_sub_field(
+ field, key, REQUEST, sub_key, convertion, second_value,
+ )
+ return second_value
+
+ def _validate_sub_field(self, field, key, REQUEST, sub_key,
+ convertion, second_value):
+ """
+ Validates a subfield (as part of field validation).
+ """
+ sub_field_value = Validator.FloatValidatorInstance.validate(
+ field,
+ field.generate_subfield_key(
+ sub_key,
+ validation=1, key=key),
+ REQUEST
+ )
+ if sub_field_value is not None:
+ if second_value is not None:
+ second_value += sub_field_value * convertion
+ else:
+ second_value = sub_field_value * convertion
+ return second_value
+
+ def render_htmlgrid(self, value=None, REQUEST=None, key=None):
+ """
+ render_htmlgrid returns a list of tuple (title, html render)
+ We will use title generated by the widget.
+ """
+ key = self.generate_field_key(key=key)
+ value = self._get_default(key, value, REQUEST)
+ html = self.widget.render_htmlgrid(self, key, value, REQUEST)
+ return html
+
+
+DurationWidgetInstance = DurationWidget()
+DurationFieldValidatorInstance = DurationValidator()
+
+class DurationField(ZMIField):
+ security = ClassSecurityInfo()
+ meta_type = "DurationField"
+
+ widget = DurationWidgetInstance
+ validator = DurationFieldValidatorInstance
Modified: erp5/trunk/products/ERP5Form/__init__.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Form/__init__.py?rev=12585&r1=12584&r2=12585&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Form/__init__.py (original)
+++ erp5/trunk/products/ERP5Form/__init__.py Thu Feb 8 18:45:36 2007
@@ -42,7 +42,7 @@
# Define object classes and tools
import Form, FSForm, ListBox, MatrixBox, SelectionTool
import ZGDChart, PDFTemplate, Report, PDFForm, ParallelListField
-import PlanningBox, POSBox, FormBox, EditorField, ProxyField
+import PlanningBox, POSBox, FormBox, EditorField, ProxyField, DurationField
import RelationField, ImageField, MultiRelationField, MultiLinkField, InputButtonField
import ZPyChart
import PreferenceTool
@@ -81,6 +81,8 @@
FieldRegistry.registerField(ZPyChart.ZPyChart,
'www/StringField.gif')
FieldRegistry.registerField(ProxyField.ProxyField,
+ 'www/StringField.gif')
+ FieldRegistry.registerField(DurationField.DurationField,
'www/StringField.gif')
FieldRegistry.registerField(EditorField.EditorField,
'www/TextAreaField.gif')
More information about the Erp5-report
mailing list