1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of SENAITE.CORE. |
4
|
|
|
# |
5
|
|
|
# SENAITE.CORE is free software: you can redistribute it and/or modify it under |
6
|
|
|
# the terms of the GNU General Public License as published by the Free Software |
7
|
|
|
# Foundation, version 2. |
8
|
|
|
# |
9
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
10
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
11
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
12
|
|
|
# details. |
13
|
|
|
# |
14
|
|
|
# You should have received a copy of the GNU General Public License along with |
15
|
|
|
# this program; if not, write to the Free Software Foundation, Inc., 51 |
16
|
|
|
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17
|
|
|
# |
18
|
|
|
# Copyright 2018-2024 by it's authors. |
19
|
|
|
# Some rights reserved, see README and LICENSE. |
20
|
|
|
|
21
|
|
|
from datetime import timedelta |
22
|
|
|
|
23
|
|
|
from senaite.core.interfaces import ISenaiteFormLayer |
24
|
|
|
from senaite.core.schema.interfaces import IDurationField |
25
|
|
|
from senaite.core.z3cform.interfaces import IDurationWidget |
26
|
|
|
from senaite.core.z3cform.widgets.basewidget import BaseWidget |
27
|
|
|
from z3c.form.browser import widget |
28
|
|
|
from z3c.form.browser.widget import HTMLInputWidget |
29
|
|
|
from z3c.form.converter import TimedeltaDataConverter |
30
|
|
|
from z3c.form.interfaces import IFieldWidget |
31
|
|
|
from z3c.form.widget import FieldWidget |
32
|
|
|
from zope.component import adapter |
33
|
|
|
from zope.interface import implementer |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
@adapter(IDurationField, IDurationWidget) |
37
|
|
|
class DurationDataConverter(TimedeltaDataConverter): |
38
|
|
|
"""Converts the value between the field and the widget |
39
|
|
|
""" |
40
|
|
|
|
41
|
|
|
def toWidgetValue(self, value): |
42
|
|
|
"""Converts from field value to widget |
43
|
|
|
""" |
44
|
|
|
if not isinstance(value, timedelta): |
45
|
|
|
return {} |
46
|
|
|
|
47
|
|
|
# Note timedelta keeps days and seconds a part! |
48
|
|
|
return { |
49
|
|
|
"days": value.days, |
50
|
|
|
"hours": (value.seconds / 3600) % 24, # hours within a day |
51
|
|
|
"minutes": (value.seconds / 60) % 60, # minutes within an hour |
52
|
|
|
"seconds": value.seconds % 60, # seconds within a minute |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
def toFieldValue(self, value): |
56
|
|
|
"""Converts from widget to field value |
57
|
|
|
""" |
58
|
|
|
return timedelta(**value) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
@implementer(IDurationWidget) |
62
|
|
|
class DurationWidget(HTMLInputWidget, BaseWidget): |
63
|
|
|
"""Widget for duration in days, hours, minutes and seconds |
64
|
|
|
""" |
65
|
|
|
klass = u"senaite-duration-widget" |
66
|
|
|
|
67
|
|
|
# Basic properties |
68
|
|
|
show_days = True |
69
|
|
|
show_hours = True |
70
|
|
|
show_minutes = True |
71
|
|
|
show_seconds = False |
72
|
|
|
|
73
|
|
|
def update(self): |
74
|
|
|
"""Computes self.value for the widget templates |
75
|
|
|
|
76
|
|
|
see z3c.form.widget.Widget |
77
|
|
|
""" |
78
|
|
|
super(DurationWidget, self).update() |
79
|
|
|
widget.addFieldClass(self) |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
@adapter(IDurationField, ISenaiteFormLayer) |
83
|
|
|
@implementer(IFieldWidget) |
84
|
|
|
def DurationWidgetFactory(field, request): |
85
|
|
|
"""Widget factory for duration field |
86
|
|
|
""" |
87
|
|
|
return FieldWidget(field, DurationWidget(request)) |
88
|
|
|
|