Passed
Pull Request — 2.x (#1892)
by Ramon
05:39
created

DateTimeWidget.to_tz_date()   A

Complexity

Conditions 4

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 13
rs 9.85
c 0
b 0
f 0
cc 4
nop 2
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
        This is required for the native datetime-local field.
69
        """
70
        dt = self.to_tz_date(time)
71
        if self.show_time:
72
            return dt.strftime("%Y-%m-%dT%H:%M")
73
        return dt.strftime("%Y-%m-%d")
74
75
    def get_max(self):
76
        now = DateTime()
77
        if self.show_time:
78
            return now.strftime("%Y-%m-%dT23:59")
79
        return now.strftime("%Y-%m-%d")
80
81
    def get_min(self):
82
        now = DateTime()
83
        if self.show_time:
84
            return now.strftime("%Y-%m-%dT00:00")
85
        return now.strftime("%Y-%m-%d")
86
87
    def attrs(self):
88
        attrs = {}
89
        if self.show_time:
90
            attrs["pattern"] = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}"
91
        else:
92
            attrs["pattern"] = r"\d{4}-\d{2}-\d{2}}"
93
        if self.datepicker_nofuture:
94
            attrs["max"] = self.get_max()
95
        if self.datepicker_nopast:
96
            attrs["min"] = self.get_min()
97
        return attrs
98
99
100
registerWidget(
101
    DateTimeWidget,
102
    title="DateTimeWidget",
103
    description=("Simple text field, with a jquery date widget attached.")
104
)
105
106
registerPropertyType("show_time", "boolean")
107