Passed
Push — 2.x ( 774efd...5fe529 )
by Jordi
05:46
created

senaite.core.browser.controlpanel.setupview   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 60
dl 0
loc 117
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A SetupView.get_count() 0 15 2
A SetupView.senaite_setup() 0 5 1
A SetupView.bootstrap() 0 6 1
A SetupView.__call__() 0 3 1
A SetupView.__init__() 0 3 1
A SetupView.setup() 0 5 1
A SetupView.setupitems() 0 14 1
A SetupView.get_icon_for() 0 5 1
A SetupView.portal() 0 5 1
A SetupView.get_allowed_content_types() 0 10 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-2023 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import api
22
from bika.lims.utils import t
23
from plone.memoize.instance import memoize
24
from plone.memoize.view import memoize_contextless
25
from Products.Five.browser import BrowserView
26
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
27
from senaite.core.catalog import SETUP_CATALOG
28
from senaite.core.p3compat import cmp
29
from zope.component import getMultiAdapter
30
31
32
class SetupView(BrowserView):
33
    """Ordered overview of all Setup Items
34
    """
35
    template = ViewPageTemplateFile("templates/setupview.pt")
36
37
    def __init__(self, context, request):
38
        self.context = context
39
        self.request = request
40
41
    def __call__(self):
42
        self.request.set("disable_border", 1)
43
        return self.template()
44
45
    @property
46
    @memoize
47
    def bootstrap(self):
48
        return getMultiAdapter(
49
            (self.context, self.request),
50
            name="bootstrapview")
51
52
    @property
53
    def portal(self):
54
        """Returns the Portal Object
55
        """
56
        return api.get_portal()
57
58
    @property
59
    def setup(self):
60
        """Returns the old Setup Object
61
        """
62
        return api.get_setup()
63
64
    @property
65
    def senaite_setup(self):
66
        """Returns the new Setup Object
67
        """
68
        return api.get_senaite_setup()
69
70
    @memoize_contextless
71
    def get_icon_for(self, brain, **kw):
72
        """Returns the icon URL for the given catalog brain
73
        """
74
        return self.bootstrap.get_icon_for(brain, **kw)
75
76
    @memoize_contextless
77
    def get_allowed_content_types(self, obj):
78
        """Get the allowed content types
79
        """
80
        portal_types = api.get_tool("portal_types")
81
        fti = portal_types.getTypeInfo(api.get_portal_type(obj))
82
        allowed_types = fti.allowed_content_types
83
        if len(allowed_types) != 1:
84
            return None
85
        return allowed_types[0]
86
87
    def get_count(self, obj):
88
        """Retrieve the count of contained items
89
        """
90
        contained_types = self.get_allowed_content_types(obj)
91
92
        # fallback
93
        if contained_types is None:
94
            return len(obj.objectIds())
95
96
        query = {
97
            "portal_type": contained_types,
98
            "is_active": True,
99
        }
100
        brains = api.search(query, SETUP_CATALOG)
101
        return len(brains)
102
103
    def setupitems(self):
104
        """Lookup available setup items
105
106
        :returns: objects
107
        """
108
        items = self.setup.objectValues() + self.senaite_setup.objectValues()
109
110
        # sort by (translated) title
111
        def cmp_by_translated_title(obj1, obj2):
112
            title1 = t(api.get_title(obj1))
113
            title2 = t(api.get_title(obj2))
114
            return cmp(title1, title2)
115
116
        return sorted(items, cmp=cmp_by_translated_title)
117