Passed
Push — 2.x ( 433576...7dbd5c )
by Ramon
08:58 queued 03:34
created

getWorksheetLayouts()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nop 0
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 bikaMessageFactory as _
22
from bika.lims.interfaces import IWorksheetLayouts
23
24
from zope.component import getUtilitiesFor
25
26
from Products.CMFCore.utils import getToolByName
27
from Products.Archetypes.public import DisplayList
28
29
30
def checkUserAccess(worksheet, request, redirect=True):
31
    """ Checks if the current user has granted access to the worksheet.
32
        If the user is an analyst without LabManager, LabClerk and
33
        RegulatoryInspector roles and the option 'Allow analysts
34
        only to access to the Worksheets on which they are assigned' is
35
        ticked and the above condition is true, it will redirect to
36
        the main Worksheets view.
37
        Returns False if the user has no access, otherwise returns True
38
    """
39
    # Deny access to foreign analysts
40
    allowed = worksheet.checkUserAccess()
41
    if allowed == False and redirect == True:
42
        msg =  _('You do not have sufficient privileges to view '
43
                 'the worksheet ${worksheet_title}.',
44
                 mapping={"worksheet_title": worksheet.Title()})
45
        worksheet.plone_utils.addPortalMessage(msg, 'warning')
46
        # Redirect to WS list
47
        portal = getToolByName(worksheet, 'portal_url').getPortalObject()
48
        destination_url = portal.absolute_url() + "/worksheets"
49
        request.response.redirect(destination_url)
50
51
    return allowed
52
53
def checkUserManage(worksheet, request, redirect=True):
54
    """ Checks if the current user has granted access to the worksheet
55
        and if has also privileges for managing it. If the user has no
56
        granted access and redirect's value is True, redirects to
57
        /manage_results view. Otherwise, does nothing
58
    """
59
    allowed = worksheet.checkUserManage()
60
    if allowed == False and redirect == True:
61
        # Redirect to /manage_results view
62
        destination_url = worksheet.absolute_url() + "/manage_results"
63
        request.response.redirect(destination_url)
64
65
def showRejectionMessage(worksheet):
66
    """ Adds a portalMessage if
67
        a) the worksheet has been rejected and replaced by another or
68
        b) if the worksheet is the replacement of a rejected worksheet.
69
        Otherwise, does nothing.
70
    """
71
    if hasattr(worksheet, 'replaced_by'):
72
        uc = getToolByName(worksheet, 'uid_catalog')
73
        uid = getattr(worksheet, 'replaced_by')
74
        _ws = uc(UID=uid)[0].getObject()
75
        msg = _("This worksheet has been rejected.  The replacement worksheet is ${ws_id}",
76
                mapping={'ws_id':_ws.getId()})
77
        worksheet.plone_utils.addPortalMessage(msg)
78
    if hasattr(worksheet, 'replaces_rejected_worksheet'):
79
        uc = getToolByName(worksheet, 'uid_catalog')
80
        uid = getattr(worksheet, 'replaces_rejected_worksheet')
81
        _ws = uc(UID=uid)[0].getObject()
82
        msg = _("This worksheet has been created to replace the rejected "
83
                "worksheet at ${ws_id}",
84
                mapping={'ws_id':_ws.getId()})
85
        worksheet.plone_utils.addPortalMessage(msg)
86
87
88
def getWorksheetLayouts():
89
    """ Getting additional layouts for Worksheet
90
    """
91
    layouts = []
92
    for name, layout_utility in getUtilitiesFor(IWorksheetLayouts):
93
        map(lambda entry: layouts.append(entry), layout_utility.getLayouts())
94
95
    return DisplayList(tuple(layouts))
96