Passed
Push — master ( 3a0253...289bce )
by Ramon
03:50
created

bika.lims.browser.viewlets.path_bar   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 48
dl 0
loc 99
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A PathBarViewlet.update() 0 5 1
A PathBarViewlet.get_breadcrumbs() 0 16 4
A PathBarViewlet.to_breadcrumb() 0 5 1
A PathBarViewlet.get_portal_type_title() 0 9 2
A PathBarViewlet.get_obj_url() 0 8 2
A PathBarViewlet.get_obj_title() 0 17 4
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-2020 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
from Products.CMFCore.permissions import View
21
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
22
from plone.app.layout.viewlets.common import PathBarViewlet as Base
23
24
from bika.lims import api
25
from bika.lims import senaiteMessageFactory as _
26
from bika.lims.api.security import check_permission
27
from bika.lims.interfaces import IClient
28
29
30
class PathBarViewlet(Base):
31
    index = ViewPageTemplateFile(
32
        "templates/plone.app.layout.viewlets.path_bar.pt")
33
34
    def update(self):
35
        super(PathBarViewlet, self).update()
36
37
        # Hide breadcrumbs for which current user does not have "View" perm
38
        self.breadcrumbs = self.get_breadcrumbs()
39
40
    def get_breadcrumbs(self):
41
        """Generates the breadcrumbs. Items for which current user does not
42
        have the View permission granted are omitted
43
        """
44
        hierarchy = []
45
        current = self.context
46
        while not api.is_portal(current):
47
            if api.is_object(current):
48
                if check_permission(View, current):
49
                    hierarchy.append(current)
50
            else:
51
                # Some objects (e.g. portal_registry) are not supported
52
                hierarchy.append(current)
53
            current = current.aq_parent
54
        hierarchy = sorted(hierarchy, reverse=True)
55
        return map(self.to_breadcrumb, hierarchy)
56
57
    def to_breadcrumb(self, obj):
58
        """Converts the object to a breadcrumb for the template consumption
59
        """
60
        return {"Title": self.get_obj_title(obj),
61
                "absolute_url": self.get_obj_url(obj)}
62
63
    def get_obj_title(self, obj):
64
        """Returns the title of the object to be displayed as breadcrumb
65
        """
66
        if not api.is_object(obj):
67
            # Some objects (e.g. portal_registry) are not supported
68
            return obj.Title()
69
70
        title = api.get_title(obj)
71
        if IClient.providedBy(obj.aq_parent):
72
            # Objects from inside Client folder are always stored directly, w/o
73
            # subfolders, making it difficult for user to know if what is
74
            # looking at is a Sample, a Batch or a Contact. Append the name of
75
            # the portal type
76
            pt_title = self.get_portal_type_title(obj)
77
            if pt_title:
78
                title = "{} ({})".format(title, _(pt_title))
79
        return title
80
81
    def get_obj_url(self, obj):
82
        """Returns the absolute url of the object passed-in
83
        """
84
        if not api.is_object(obj):
85
            # Some objects (e.g. portal_registry) are not supported
86
            return obj.absolute_url()
87
88
        return api.get_url(obj)
89
90
    def get_portal_type_title(self, obj):
91
        """Returns the title of the portal type of the obj passed-in
92
        """
93
        portal = api.get_portal()
94
        portal_type = api.get_portal_type(obj)
95
        portal_type = portal.portal_types.getTypeInfo(portal_type)
96
        if portal_type:
97
            return portal_type.title
98
        return None
99