Passed
Push — 2.x ( 4e3b0a...8119a4 )
by Jordi
07:03
created

senaite.core.browser.controlpanel.worksheettemplates.view   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 86
dl 0
loc 151
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A WorksheetTemplatesView.folderitem() 0 25 3
B WorksheetTemplatesView.__init__() 0 87 1
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-2024 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import collections
22
23
from bika.lims import api
24
from bika.lims import senaiteMessageFactory as _
25
from bika.lims.utils import get_link_for
26
from senaite.core.permissions import AddWorksheetTemplate
27
from senaite.core.i18n import translate
28
from senaite.app.listing import ListingView
29
from senaite.core.catalog import SETUP_CATALOG
30
31
32
class WorksheetTemplatesView(ListingView):
33
    """Listing View for Worksheet Templates
34
    """
35
36
    def __init__(self, context, request):
37
        super(WorksheetTemplatesView, self).__init__(context, request)
38
39
        self.catalog = SETUP_CATALOG
40
41
        self.contentFilter = {
42
            "portal_type": "WorksheetTemplate",
43
            "sort_on": "sortable_title",
44
            "sort_order": "ascending",
45
            "path": {
46
                "query": api.get_path(self.context),
47
                "depth": 1,
48
            },
49
        }
50
51
        self.context_actions = {
52
            _(u"listing_worksheettemplates_action_add", default=u"Add"): {
53
                "url": "++add++WorksheetTemplate",
54
                "permission": AddWorksheetTemplate,
55
                "icon": "senaite_theme/icon/plus"
56
            }
57
        }
58
59
        self.title = translate(_(
60
            u"listing_worksheettemplates_title",
61
            default=u"Worksheet Templates")
62
        )
63
        self.icon = api.get_icon("WorksheetTemplates", html_tag=False)
64
        self.show_select_column = True
65
66
        self.columns = collections.OrderedDict((
67
            ("Name", {
68
                "title": _(
69
                    u"listing_worksheettemplates_column_name",
70
                    default=u"Name",
71
                ),
72
                "index": "sortable_title",
73
            }),
74
            ("Description", {
75
                "title": _(
76
                    u"listing_worksheettemplates_column_description",
77
                    default=u"Description"
78
                ),
79
                "toggle": True,
80
            }),
81
            ("Method", {
82
                "title": _(
83
                    u"listing_worksheettemplates_column_method",
84
                    default=u"Method"
85
                ),
86
                "toggle": True,
87
            }),
88
            ("Instrument", {
89
                "title": _(
90
                    u"listing_worksheettemplates_column_instrument",
91
                    default=u"Instrument"
92
                ),
93
                "index": "instrument_title",
94
                "toggle": True,
95
            }),
96
        ))
97
98
        self.review_states = [
99
            {
100
                "id": "default",
101
                "title": _(
102
                    u"listing_worksheettemplates_state_active",
103
                    default=u"Active"
104
                ),
105
                "contentFilter": {"is_active": True},
106
                "columns": self.columns.keys(),
107
            }, {
108
                "id": "inactive",
109
                "title": _(
110
                    u"listing_worksheettemplates_state_inactive",
111
                    default=u"Inactive"
112
                ),
113
                "contentFilter": {'is_active': False},
114
                "columns": self.columns.keys(),
115
            }, {
116
                "id": "all",
117
                "title": _(
118
                    u"listing_worksheettemplates_state_all",
119
                    default=u"All"
120
                ),
121
                "contentFilter": {},
122
                "columns": self.columns.keys(),
123
            },
124
        ]
125
126
    def folderitem(self, obj, item, index):
127
        """Service triggered each time an item is iterated in folderitems.
128
        The use of this service prevents the extra-loops in child objects.
129
        :obj: the instance of the class to be foldered
130
        :item: dict containing the properties of the object to be used by
131
            the template
132
        :index: current index of the item
133
        """
134
        obj = api.get_object(obj)
135
        item["Description"] = api.get_description(obj)
136
        item["replace"]["Name"] = get_link_for(obj)
137
138
        instrument = obj.getInstrument()
139
        if instrument:
140
            instrument_title = api.get_title(instrument)
141
            item["Instrument"] = instrument_title
142
            item["replace"]["Instrument"] = get_link_for(instrument)
143
144
        method = obj.getRestrictToMethod()
145
        if method:
146
            method_title = api.get_title(method)
147
            item["Method"] = method_title
148
            item["replace"]["Method"] = get_link_for(method)
149
150
        return item
151