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
|
|
|
dt = self.to_tz_date(time) |
67
|
|
|
if self.show_time: |
68
|
|
|
return dt.strftime("%Y-%m-%dT%H:%M") |
69
|
|
|
return dt.strftime("%Y-%m-%d") |
70
|
|
|
|
71
|
|
|
def get_max(self): |
72
|
|
|
now = DateTime() |
73
|
|
|
if self.show_time: |
74
|
|
|
return now.strftime("%Y-%m-%dT23:59") |
75
|
|
|
return now.strftime("%Y-%m-%d") |
76
|
|
|
|
77
|
|
|
def get_min(self): |
78
|
|
|
now = DateTime() |
79
|
|
|
if self.show_time: |
80
|
|
|
return now.strftime("%Y-%m-%dT00:00") |
81
|
|
|
return now.strftime("%Y-%m-%d") |
82
|
|
|
|
83
|
|
|
def attrs(self): |
84
|
|
|
attrs = {} |
85
|
|
|
if self.show_time: |
86
|
|
|
attrs["pattern"] = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}" |
87
|
|
|
else: |
88
|
|
|
attrs["pattern"] = r"\d{4}-\d{2}-\d{2}}" |
89
|
|
|
if self.datepicker_nofuture: |
90
|
|
|
attrs["max"] = self.get_max() |
91
|
|
|
if self.datepicker_nopast: |
92
|
|
|
attrs["min"] = self.get_min() |
93
|
|
|
return attrs |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
registerWidget( |
97
|
|
|
DateTimeWidget, |
98
|
|
|
title="DateTimeWidget", |
99
|
|
|
description=("Simple text field, with a jquery date widget attached.") |
100
|
|
|
) |
101
|
|
|
|
102
|
|
|
registerPropertyType("show_time", "boolean") |
103
|
|
|
|