Commit 5cfdbb70 authored by Romain Courteaud's avatar Romain Courteaud

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.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@12585 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 674e5696
##############################################################################
#
# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
# Romain Courteaud <romain@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
......@@ -42,7 +42,7 @@ document_classes = updateGlobals( this_module, globals(),
# 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
......@@ -82,6 +82,8 @@ def initialize( context ):
'www/StringField.gif')
FieldRegistry.registerField(ProxyField.ProxyField,
'www/StringField.gif')
FieldRegistry.registerField(DurationField.DurationField,
'www/StringField.gif')
FieldRegistry.registerField(EditorField.EditorField,
'www/TextAreaField.gif')
FieldRegistry.registerField(FormBox.FormBox,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment