Passed
Push — master ( 15a906...c0868d )
by Jordi
04:39
created

bika.lims.browser.controlpanel   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 52
dl 0
loc 114
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A SetupView.__init__() 0 3 1
A SetupView.__call__() 0 3 1
A SetupView.setup() 0 5 1
A SetupView.portal() 0 5 1
A SetupView.setupitems() 0 23 2
A SetupView.get_icon_url() 0 20 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A modified_cache_key() 0 4 1
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE.
4
#
5
# SENAITE.LIMS 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
21
import os
22
23
from bika.lims.utils import t
24
from plone.app.controlpanel.overview import OverviewControlPanel
25
from plone.memoize.volatile import cache
26
from plone.memoize.volatile import store_on_context
27
from Products.Five.browser import BrowserView
28
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
29
from bika.lims import api
30
31
32
def modified_cache_key(method, self, brain_or_object):
33
    """A cache key that returns the millis of the last modification time
34
    """
35
    return api.get_modification_date(brain_or_object).millis()
36
37
38
class SenaiteOverviewControlPanel(OverviewControlPanel):
39
    """Bootstrapped version of the standard Plone Control Panel
40
    """
41
    template = ViewPageTemplateFile(
42
        "templates/plone.app.controlpanel.overview.pt")
43
44
45
class SetupView(BrowserView):
46
    """Ordered overview of all Setup Items
47
    """
48
    template = ViewPageTemplateFile("templates/setupview.pt")
49
50
    def __init__(self, context, request):
51
        self.context = context
52
        self.request = request
53
54
    def __call__(self):
55
        self.request.set("disable_border", 1)
56
        return self.template()
57
58
    @property
59
    def portal(self):
60
        """Returns the Portal Object
61
        """
62
        return api.get_portal()
63
64
    @property
65
    def setup(self):
66
        """Returns the Senaite Setup Object
67
        """
68
        return api.get_setup()
69
70
    @cache(modified_cache_key, store_on_context)
71
    def get_icon_url(self, brain):
72
        """Returns the (big) icon URL for the given catalog brain
73
        """
74
        icon_url = api.get_icon(brain, html_tag=False)
75
        url, icon = icon_url.rsplit("/", 1)
76
        relative_url = url.lstrip(self.portal.absolute_url())
77
        name, ext = os.path.splitext(icon)
78
79
        # big icons endwith _big
80
        if not name.endswith("_big"):
81
            icon = "{}_big{}".format(name, ext)
82
83
        icon_big_url = "/".join([relative_url, icon])
84
85
        # fall back to a default icon if the looked up icon does not exist
86
        if self.context.restrictedTraverse(icon_big_url, None) is None:
87
            icon_big_url = "++resource++bika.lims.images/gears.png"
88
89
        return icon_big_url
90
91
    def setupitems(self):
92
        """Lookup available setup items
93
94
        :returns: catalog brains
95
        """
96
        query = {
97
            "path": {
98
                "query": api.get_path(self.setup),
99
                "depth": 1,
100
            },
101
        }
102
        items = api.search(query, "portal_catalog")
103
        # filter out items
104
        items = filter(lambda item: not item.exclude_from_nav, items)
105
106
        # sort by (translated) title
107
        def cmp_by_translated_title(brain1, brain2):
108
            title1 = t(api.get_title(brain1))
109
            title2 = t(api.get_title(brain2))
110
            # XXX: Python 3 compatibility
111
            return cmp(title1, title2)
112
113
        return sorted(items, cmp=cmp_by_translated_title)
114