Passed
Push — master ( 70be41...e1228c )
by Ramon
11:12
created

bika.lims.controlpanel.bika_departments.Departments.getContacts()   A

Complexity

Conditions 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 18
rs 9.8
c 0
b 0
f 0
cc 3
nop 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-2019 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import collections
22
23
from Products.ATContentTypes.content import schemata
24
from Products.Archetypes import atapi
25
from Products.Archetypes.utils import DisplayList
26
from bika.lims import api
27
from bika.lims import bikaMessageFactory as _
28
from bika.lims.browser.bika_listing import BikaListingView
29
from bika.lims.config import PROJECTNAME
30
from bika.lims.interfaces import IDepartments
31
from bika.lims.permissions import AddDepartment
32
from bika.lims.utils import get_email_link
33
from bika.lims.utils import get_link
34
from plone.app.folder.folder import ATFolder
35
from plone.app.folder.folder import ATFolderSchema
36
from zope.interface.declarations import implements
37
38
39
# TODO: Separate content and view into own modules!
40
41
42
class DepartmentsView(BikaListingView):
43
44 View Code Duplication
    def __init__(self, context, request):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
45
        super(DepartmentsView, self).__init__(context, request)
46
47
        self.catalog = "bika_setup_catalog"
48
49
        self.contentFilter = {
50
            "portal_type": "Department",
51
            "sort_order": "ascending",
52
            "sort_on": "sortable_title"
53
        }
54
55
        self.context_actions = {
56
            _("Add"): {
57
                "url": "createObject?type_name=Department",
58
                "permission": AddDepartment,
59
                "icon": "++resource++bika.lims.images/add.png"}
60
        }
61
62
        self.title = self.context.translate(_("Lab Departments"))
63
        self.icon = "{}/{}".format(
64
            self.portal_url,
65
            "/++resource++bika.lims.images/department_big.png"
66
        )
67
68
        self.show_select_row = False
69
        self.show_select_column = True
70
        self.pagesize = 25
71
72
        self.columns = collections.OrderedDict((
73
            ("Title", {
74
                "title": _("Department"),
75
                "index": "sortable_title"}),
76
            ("Description", {
77
                "title": _("Description"),
78
                "index": "Description",
79
                "toggle": True}),
80
            ("Manager", {
81
                "title": _("Manager"),
82
                "toggle": True}),
83
            ("ManagerPhone", {
84
                "title": _("Manager Phone"),
85
                "toggle": True}),
86
            ("ManagerEmail", {
87
                "title": _("Manager Email"),
88
                "toggle": True}),
89
        ))
90
91
        self.review_states = [
92
            {
93
                "id": "default",
94
                "title": _("Active"),
95
                "contentFilter": {"is_active": True},
96
                "transitions": [{"id": "deactivate"}, ],
97
                "columns": self.columns.keys(),
98
            }, {
99
                "id": "inactive",
100
                "title": _("Inactive"),
101
                "contentFilter": {'is_active': False},
102
                "transitions": [{"id": "activate"}, ],
103
                "columns": self.columns.keys(),
104
            }, {
105
                "id": "all",
106
                "title": _("All"),
107
                "contentFilter": {},
108
                "columns": self.columns.keys(),
109
            },
110
        ]
111
112
    def before_render(self):
113
        """Before template render hook
114
        """
115
        # Don't allow any context actions
116
        self.request.set("disable_border", 1)
117
118
    def folderitem(self, obj, item, index):
119
        """Service triggered each time an item is iterated in folderitems.
120
        The use of this service prevents the extra-loops in child objects.
121
        :obj: the instance of the class to be foldered
122
        :item: dict containing the properties of the object to be used by
123
            the template
124
        :index: current index of the item
125
        """
126
        title = obj.Title()
127
        description = obj.Description()
128
        url = obj.absolute_url()
129
130
        item["replace"]["Title"] = get_link(url, value=title)
131
        item["Description"] = description
132
133
        item["Manager"] = ""
134
        item["ManagerPhone"] = ""
135
        item["ManagerEmail"] = ""
136
        manager = obj.getManager()
137
        if manager:
138
            manager_name = manager.getFullname()
139
            item["Manager"] = manager_name
140
141
            manager_url = manager.absolute_url()
142
            item["replace"]["Manager"] = get_link(manager_url, manager_name)
143
144
            manager_email = manager.getEmailAddress()
145
            item["replace"]["ManagerEmail"] = get_email_link(
146
                manager_email, value=manager_email)
147
148
            item["ManagerPhone"] = manager.getBusinessPhone()
149
150
        return item
151
152
153
schema = ATFolderSchema.copy()
154
155
156
class Departments(ATFolder):
157
    implements(IDepartments)
158
    displayContentsTab = False
159
    schema = schema
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable schema does not seem to be defined.
Loading history...
160
161
162
schemata.finalizeATCTSchema(schema, folderish=True, moveDiscussion=False)
163
atapi.registerType(Departments, PROJECTNAME)
164