|
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-2021 by it's authors. |
|
19
|
|
|
# Some rights reserved, see README and LICENSE. |
|
20
|
|
|
|
|
21
|
|
|
from AccessControl import ClassSecurityInfo |
|
22
|
|
|
from bika.lims.browser import ulocalized_time as ut |
|
23
|
|
|
from DateTime import DateTime |
|
24
|
|
|
from DateTime.DateTime import safelocaltime |
|
25
|
|
|
from DateTime.interfaces import DateTimeError |
|
26
|
|
|
from Products.Archetypes.Registry import registerPropertyType |
|
27
|
|
|
from Products.Archetypes.Registry import registerWidget |
|
28
|
|
|
from Products.Archetypes.Widget import TypesWidget |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
class DateTimeWidget(TypesWidget): |
|
32
|
|
|
_properties = TypesWidget._properties.copy() |
|
33
|
|
|
_properties.update({ |
|
34
|
|
|
"show_time": False, |
|
35
|
|
|
"datepicker_nofuture": False, |
|
36
|
|
|
"datepicker_nopast": False, |
|
37
|
|
|
"macro": "senaite_widgets/datetimewidget", |
|
38
|
|
|
"helper_js": ("senaite_widgets/datetimewidget.js",), |
|
39
|
|
|
"helper_css": ("senaite_widgets/datetimewidget.css",), |
|
40
|
|
|
}) |
|
41
|
|
|
|
|
42
|
|
|
security = ClassSecurityInfo() |
|
43
|
|
|
|
|
44
|
|
|
def ulocalized_time(self, time, context, request): |
|
45
|
|
|
"""Returns the localized time in string format |
|
46
|
|
|
""" |
|
47
|
|
|
value = ut(time, long_format=self.show_time, time_only=False, |
|
48
|
|
|
context=context, request=request) |
|
49
|
|
|
return value or "" |
|
50
|
|
|
|
|
51
|
|
|
def to_tz_date(self, value): |
|
52
|
|
|
if not isinstance(value, DateTime): |
|
53
|
|
|
try: |
|
54
|
|
|
value = DateTime(value) |
|
55
|
|
|
if value.timezoneNaive(): |
|
56
|
|
|
# Use local timezone for tz naive strings |
|
57
|
|
|
# see http://dev.plone.org/plone/ticket/10141 |
|
58
|
|
|
zone = value.localZone(safelocaltime(value.timeTime())) |
|
59
|
|
|
parts = value.parts()[:-1] + (zone,) |
|
60
|
|
|
value = DateTime(*parts) |
|
61
|
|
|
except DateTimeError: |
|
62
|
|
|
value = None |
|
63
|
|
|
return value |
|
64
|
|
|
|
|
65
|
|
|
def to_local_date(self, time, context, request): |
|
66
|
|
|
"""This method converts to a local date w/o timezone |
|
67
|
|
|
""" |
|
68
|
|
|
dt = self.to_tz_date(time) |
|
69
|
|
|
if self.show_time: |
|
70
|
|
|
return dt.strftime("%Y-%m-%dT%H:%M") |
|
71
|
|
|
return dt.strftime("%Y-%m-%d") |
|
72
|
|
|
|
|
73
|
|
|
def get_date(self, value): |
|
74
|
|
|
if not value: |
|
75
|
|
|
return "" |
|
76
|
|
|
dt = self.to_tz_date(value) |
|
77
|
|
|
return dt.strftime("%Y-%m-%d") |
|
78
|
|
|
|
|
79
|
|
|
def get_time(self, value): |
|
80
|
|
|
if not value: |
|
81
|
|
|
return "" |
|
82
|
|
|
dt = self.to_tz_date(value) |
|
83
|
|
|
return dt.strftime("%H:%M") |
|
84
|
|
|
|
|
85
|
|
|
def get_max(self): |
|
86
|
|
|
now = DateTime() |
|
87
|
|
|
return now.strftime("%Y-%m-%d") |
|
88
|
|
|
|
|
89
|
|
|
def get_min(self): |
|
90
|
|
|
now = DateTime() |
|
91
|
|
|
return now.strftime("%Y-%m-%d") |
|
92
|
|
|
|
|
93
|
|
|
def attrs(self): |
|
94
|
|
|
attrs = {} |
|
95
|
|
|
if self.datepicker_nofuture: |
|
96
|
|
|
attrs["max"] = self.get_max() |
|
97
|
|
|
if self.datepicker_nopast: |
|
98
|
|
|
attrs["min"] = self.get_min() |
|
99
|
|
|
return attrs |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
registerWidget( |
|
103
|
|
|
DateTimeWidget, |
|
104
|
|
|
title="DateTimeWidget", |
|
105
|
|
|
description=("Simple text field, with a jquery date widget attached.") |
|
106
|
|
|
) |
|
107
|
|
|
|
|
108
|
|
|
registerPropertyType("show_time", "boolean") |
|
109
|
|
|
|