Passed
Push — 2.x ( 44675d...5e1af5 )
by Jordi
09:20
created

DepartmentsView.folderitem()   A

Complexity

Conditions 2

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 40
rs 9.304
c 0
b 0
f 0
cc 2
nop 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-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 bikaMessageFactory as _
25
from bika.lims.browser.bika_listing import BikaListingView
26
from bika.lims.utils import get_email_link
27
from bika.lims.utils import get_link
28
from senaite.core.catalog import SETUP_CATALOG
29
from senaite.core.permissions import AddDepartment
30
31
32
class DepartmentsView(BikaListingView):
33
    """Controlpanel Listing for Departments
34
    """
35
36
    def __init__(self, context, request):
37
        super(DepartmentsView, self).__init__(context, request)
38
39
        self.catalog = SETUP_CATALOG
40
        self.show_select_row = False
41
        self.show_select_column = True
42
        self.pagesize = 25
43
44
        self.contentFilter = {
45
            "portal_type": "Department",
46
            "sort_order": "ascending",
47
            "sort_on": "sortable_title"
48
        }
49
50
        self.context_actions = {
51
            _("Add"): {
52
                "url": "++add++Department",
53
                "permission": AddDepartment,
54
                "icon": "++resource++bika.lims.images/add.png"}
55
        }
56
57
        self.title = self.context.translate(_("Lab Departments"))
58
        self.icon = "{}/{}".format(
59
            self.portal_url,
60
            "/++resource++bika.lims.images/department_big.png"
61
        )
62
63
        self.columns = collections.OrderedDict((
64
            ("Title", {
65
                "title": _("Department"),
66
                "index": "sortable_title"}),
67
            ("DepartmentID", {
68
                "title": _("Department ID"),
69
                "index": "department_id",
70
                "toggle": False}),
71
            ("Description", {
72
                "title": _("Description"),
73
                "index": "Description",
74
                "toggle": True}),
75
            ("Manager", {
76
                "title": _("Manager"),
77
                "toggle": True}),
78
            ("ManagerPhone", {
79
                "title": _("Manager Phone"),
80
                "toggle": True}),
81
            ("ManagerEmail", {
82
                "title": _("Manager Email"),
83
                "toggle": True}),
84
        ))
85
86
        self.review_states = [
87
            {
88
                "id": "default",
89
                "title": _("Active"),
90
                "contentFilter": {"is_active": True},
91
                "columns": self.columns.keys(),
92
            }, {
93
                "id": "inactive",
94
                "title": _("Inactive"),
95
                "contentFilter": {"is_active": False},
96
                "columns": self.columns.keys(),
97
            }, {
98
                "id": "all",
99
                "title": _("All"),
100
                "contentFilter": {},
101
                "columns": self.columns.keys(),
102
            },
103
        ]
104
105
    def folderitem(self, obj, item, index):
106
        """Service triggered each time an item is iterated in folderitems.
107
        The use of this service prevents the extra-loops in child objects.
108
        :obj: the instance of the class to be foldered
109
        :item: dict containing the properties of the object to be used by
110
            the template
111
        :index: current index of the item
112
        """
113
        obj = api.get_object(obj)
114
        title = obj.Title()
115
        description = obj.Description()
116
        url = obj.absolute_url()
117
118
        # Department Title
119
        item["replace"]["Title"] = get_link(url, value=title)
120
        item["Description"] = description
121
122
        # Department ID
123
        department_id = obj.getDepartmentID()
124
        item["DepartmentID"] = department_id
125
        item["replace"]["DepartmentID"] = get_link(url, value=department_id)
126
127
        item["Manager"] = ""
128
        item["ManagerPhone"] = ""
129
        item["ManagerEmail"] = ""
130
        manager = obj.getManager()
131
        if manager:
132
            manager_name = manager.getFullname()
133
            item["Manager"] = manager_name
134
135
            manager_url = manager.absolute_url()
136
            item["replace"]["Manager"] = get_link(manager_url, manager_name)
137
138
            manager_email = manager.getEmailAddress()
139
            item["replace"]["ManagerEmail"] = get_email_link(
140
                manager_email, value=manager_email)
141
142
            item["ManagerPhone"] = manager.getBusinessPhone()
143
144
        return item
145