Passed
Pull Request — 2.x (#1892)
by Ramon
07:10 queued 02:13
created

DateTimeWidget.today()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
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 bika.lims import api
22
from AccessControl import ClassSecurityInfo
23
from bika.lims.browser import ulocalized_time as ut
24
from DateTime import DateTime
25
from Products.Archetypes.Registry import registerPropertyType
26
from Products.Archetypes.Registry import registerWidget
27
from Products.Archetypes.Widget import TypesWidget
28
29
30
class DateTimeWidget(TypesWidget):
31
    _properties = TypesWidget._properties.copy()
32
    _properties.update({
33
        "show_time": False,
34
        "datepicker_nofuture": False,
35
        "datepicker_nopast": False,
36
        "macro": "senaite_widgets/datetimewidget",
37
        "helper_js": ("senaite_widgets/datetimewidget.js",),
38
        "helper_css": ("senaite_widgets/datetimewidget.css",),
39
    })
40
41
    security = ClassSecurityInfo()
42
43
    def ulocalized_time(self, time, context, request):
44
        """Returns the localized time in string format
45
        """
46
        value = ut(time, long_format=self.show_time, time_only=False,
47
                   context=context, request=request)
48
        return value or ""
49
50
    def isoformat(self, time, context, request):
51
        dt = api.to_date(time)
52
        if self.show_time:
53
            return dt.ISO8601()
54
        return dt.strftime("%Y-%m-%d")
55
56
    def today(self, offset=0):
57
        now = DateTime() + offset
58
        if self.show_time:
59
            return now.ISO8601()
60
        return now.strftime("%Y-%m-%d")
61
62
    def attrs(self):
63
        attrs = {}
64
        if self.show_time:
65
            attrs["pattern"] = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}"
66
        else:
67
            attrs["pattern"] = r"\d{4}-\d{2}-\d{2}}"
68
        if self.datepicker_nofuture:
69
            attrs["max"] = self.today()
70
        if self.datepicker_nopast:
71
            attrs["min"] = self.today()
72
        return attrs
73
74
75
registerWidget(
76
    DateTimeWidget,
77
    title="DateTimeWidget",
78
    description=("Simple text field, with a jquery date widget attached.")
79
)
80
81
registerPropertyType("show_time", "boolean")
82