Passed
Push — 2.x ( 1da8e4...ac8200 )
by Jordi
05:35
created

bika.lims.browser.ulocalized_time()   A

Complexity

Conditions 4

Size

Total Lines 35
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 35
rs 9.65
c 0
b 0
f 0
cc 4
nop 5
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
22
from AccessControl import ClassSecurityInfo
23
from bika.lims import api
24
from bika.lims import logger
25
from Products.CMFCore.utils import getToolByName
26
from Products.Five.browser import BrowserView as BaseBrowserView
27
from senaite.core.api.dtime import to_localized_time as ulocalized_time
28
from zope.cachedescriptors.property import Lazy as lazy_property
29
from zope.i18n import translate
30
31
32
class BrowserView(BaseBrowserView):
33
    security = ClassSecurityInfo()
34
35
    logger = logger
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable logger does not seem to be defined.
Loading history...
36
37
    def __init__(self, context, request):
38
        self.context = context
39
        self.request = request
40
        super(BrowserView, self).__init__(context, request)
41
42
    security.declarePublic('ulocalized_time')
43
44
    def ulocalized_time(self, time, long_format=None, time_only=None):
45
        return ulocalized_time(time, long_format, time_only,
46
                               context=self.context, request=self.request)
47
48
    @lazy_property
49
    def portal(self):
50
        return getToolByName(self.context, 'portal_url').getPortalObject()
51
52
    @lazy_property
53
    def portal_url(self):
54
        return self.portal.absolute_url().split("?")[0]
55
56
    @lazy_property
57
    def portal_catalog(self):
58
        return getToolByName(self.context, 'portal_catalog')
59
60
    @lazy_property
61
    def reference_catalog(self):
62
        return getToolByName(self.context, 'reference_catalog')
63
64
    @lazy_property
65
    def senaite_catalog_analysis(self):
66
        return getToolByName(self.context, 'senaite_catalog_analysis')
67
68
    @lazy_property
69
    def senaite_catalog_setup(self):
70
        return getToolByName(self.context, 'senaite_catalog_setup')
71
72
    @lazy_property
73
    def senaite_catalog(self):
74
        return getToolByName(self.context, 'senaite_catalog')
75
76
    @lazy_property
77
    def portal_membership(self):
78
        return getToolByName(self.context, 'portal_membership')
79
80
    @lazy_property
81
    def portal_groups(self):
82
        return getToolByName(self.context, 'portal_groups')
83
84
    @lazy_property
85
    def portal_workflow(self):
86
        return getToolByName(self.context, 'portal_workflow')
87
88
    @lazy_property
89
    def checkPermission(self, perm, obj):
90
        return self.portal_membership.checkPermission(perm, obj)
91
92
    # TODO: user_fullname is deprecated and will be removed in Bika LIMS 3.3.
93
    # Use bika.utils.user_fullnameinstead
94
    # I was having a problem trying to import the function from bika.lims.utils
95
    # so i copied the code here.
96
    def user_fullname(self, userid):
97
        member = self.portal_membership.getMemberById(userid)
98
        if member is None:
99
            return userid
100
        member_fullname = member.getProperty('fullname')
101
        portal_catalog = getToolByName(self, 'portal_catalog')
102
        c = portal_catalog(portal_type='Contact', getUsername=userid)
103
        contact_fullname = c[0].getObject().getFullname() if c else None
104
        return contact_fullname or member_fullname or userid
105
106
    # TODO: user_fullname is deprecated and will be removed in Bika LIMS 3.3.
107
    # Use bika.utils.user_fullnameinstead.
108
    # I was having a problem trying to import the function from bika.lims.utils
109
    # so i copied the code here.
110
    def user_email(self, userid):
111
        member = self.portal_membership.getMemberById(userid)
112
        if member is None:
113
            return userid
114
        member_email = member.getProperty('email')
115
        portal_catalog = getToolByName(self, 'portal_catalog')
116
        c = portal_catalog(portal_type='Contact', getUsername=userid)
117
        contact_email = c[0].getObject().getEmailAddress() if c else None
118
        return contact_email or member_email or ''
119
120
    def python_date_format(self, long_format=None, time_only=False):
121
        """This convert bika domain date format msgstrs to Python
122
        strftime format strings, by the same rules as ulocalized_time.
123
        XXX i18nl10n.py may change, and that is where this code is taken from.
124
        """
125
        # get msgid
126
        msgid = long_format and 'date_format_long' or 'date_format_short'
127
        if time_only:
128
            msgid = 'time_format'
129
        # get the formatstring
130
        formatstring = translate(msgid, domain="senaite.core",
131
                                 context=self.request)
132
133
        if formatstring is None or formatstring.startswith(
134
                'date_') or formatstring.startswith('time_'):
135
            self.logger.error("bika/%s/%s could not be translated" %
136
                              (self.request.get('LANGUAGE'), msgid))
137
            # msg catalog was not able to translate this msgids
138
            # use default setting
139
            if long_format:
140
                key = "Products.CMFPlone.i18nl10n.override_dateformat.date_format_long"
141
                format = api.get_registry_record(key)
142
            else:
143
                key = "Products.CMFPlone.i18nl10n.override_dateformat.time_format"
144
                format = api.get_registry_record(key)
145
            return format
146
        return formatstring.replace(r"${", '%').replace('}', '')
147
148
    @lazy_property
149
    def date_format_long(self):
150
        fmt = self.python_date_format(long_format=1)
151
        if fmt == "date_format_long":
152
            fmt = "%Y-%m-%d %I:%M %p"
153
        return fmt
154
155
    @lazy_property
156
    def date_format_short(self):
157
        fmt = self.python_date_format()
158
        if fmt == "date_format_short":
159
            fmt = "%Y-%m-%d"
160
        return fmt
161
162
    @lazy_property
163
    def time_format(self):
164
        fmt = self.python_date_format(time_only=True)
165
        if fmt == "time_format":
166
            fmt = "%I:%M %p"
167
        return fmt
168