Passed
Push — master ( 26b04a...d80550 )
by Jordi
13:49 queued 09:11
created

DepartmentsView.folderitems()   A

Complexity

Conditions 4

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nop 1
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE
4
#
5
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
import collections
9
10
from bika.lims import api
11
from bika.lims import bikaMessageFactory as _
12
from bika.lims.browser.bika_listing import BikaListingView
13
from bika.lims.config import PROJECTNAME
14
from bika.lims.interfaces import IDepartments
15
from bika.lims.utils import get_email_link
16
from bika.lims.utils import get_link
17
from plone.app.content.browser.interfaces import IFolderContentsView
18
from plone.app.folder.folder import ATFolder
19
from plone.app.folder.folder import ATFolderSchema
20
from plone.app.layout.globals.interfaces import IViewView
21
from Products.Archetypes import atapi
22
from Products.Archetypes.utils import DisplayList
23
from Products.ATContentTypes.content import schemata
24
from zope.interface.declarations import implements
25
26
27
# TODO: Separate content and view into own modules!
28
29
30
class DepartmentsView(BikaListingView):
31
    implements(IFolderContentsView, IViewView)
32
33 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...
34
        super(DepartmentsView, self).__init__(context, request)
35
36
        self.catalog = "bika_setup_catalog"
37
38
        self.contentFilter = {
39
            "portal_type": "Department",
40
            "sort_order": "ascending",
41
            "sort_on": "sortable_title"
42
        }
43
44
        self.context_actions = {
45
            _("Add"): {
46
                "url": "createObject?type_name=Department",
47
                "permission": "Add portal content",
48
                "icon": "++resource++bika.lims.images/add.png"}
49
        }
50
51
        self.title = self.context.translate(_("Lab Departments"))
52
        self.icon = "{}/{}".format(
53
            self.portal_url,
54
            "/++resource++bika.lims.images/department_big.png"
55
        )
56
57
        self.show_sort_column = False
58
        self.show_select_row = False
59
        self.show_select_column = True
60
        self.pagesize = 25
61
62
        self.columns = collections.OrderedDict((
63
            ("Title", {
64
                "title": _("Department"),
65
                "index": "sortable_title"}),
66
            ("Description", {
67
                "title": _("Description"),
68
                "index": "Description",
69
                "toggle": True}),
70
            ("Manager", {
71
                "title": _("Manager"),
72
                "index": "getManagerName",
73
                "toggle": True}),
74
            ("ManagerPhone", {
75
                "title": _("Manager Phone"),
76
                "index": "getManagerPhone",
77
                "toggle": True}),
78
            ("ManagerEmail", {
79
                "title": _("Manager Email"),
80
                "index": "getManagerEmail",
81
                "toggle": True}),
82
        ))
83
84
        self.review_states = [
85
            {
86
                "id": "default",
87
                "title": _("Active"),
88
                "contentFilter": {"inactive_state": "active"},
89
                "transitions": [{"id": "deactivate"}, ],
90
                "columns": self.columns.keys(),
91
            }, {
92
                "id": "inactive",
93
                "title": _("Dormant"),
94
                "contentFilter": {"inactive_state": "inactive"},
95
                "transitions": [{"id": "activate"}, ],
96
                "columns": self.columns.keys(),
97
            }, {
98
                "id": "all",
99
                "title": _("All"),
100
                "contentFilter": {},
101
                "columns": self.columns.keys(),
102
            },
103
        ]
104
105
    def before_render(self):
106
        """Before template render hook
107
        """
108
        # Don't allow any context actions
109
        self.request.set("disable_border", 1)
110
111
    def folderitem(self, obj, item, index):
112
        """Service triggered each time an item is iterated in folderitems.
113
        The use of this service prevents the extra-loops in child objects.
114
        :obj: the instance of the class to be foldered
115
        :item: dict containing the properties of the object to be used by
116
            the template
117
        :index: current index of the item
118
        """
119
        title = obj.Title()
120
        description = obj.Description()
121
        url = obj.absolute_url()
122
123
        item["replace"]["Title"] = get_link(url, value=title)
124
        item["Description"] = description
125
126
        manager = obj.getManager()
127
        manager_name = obj.getManagerName()
128
        manager_phone = obj.getManagerPhone()
129
        manager_email = obj.getManagerEmail()
130
131
        if manager:
132
            item["Manager"] = manager_name
133
            item["replace"]["Manager"] = get_link(
134
                manager.absolute_url(), manager_name)
135
        else:
136
            item["Manager"] = ""
137
138
        if manager_email:
139
            item["ManagerEmail"] = manager_email
140
            item["replace"]["ManagerEmail"] = get_email_link(
141
                manager_email, value=manager_email)
142
        else:
143
            item["ManagerEmail"] = ""
144
145
        item["ManagerPhone"] = manager_phone
146
147
        return item
148
149
150
schema = ATFolderSchema.copy()
151
152
153
class Departments(ATFolder):
154
    implements(IDepartments)
155
    displayContentsTab = False
156
    schema = schema
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable schema does not seem to be defined.
Loading history...
157
158
    def getContacts(self, active_only=True):
159
        catalog = api.get_tool("bika_setup_catalog")
160
        query = {
161
            "portal_type": "LabContact",
162
            "sort_on": "sortable_title",
163
            "sort_order": "ascending"
164
        }
165
        results = catalog(query)
166
167
        # XXX  Better directly filter in the catalog query as soon as we have
168
        #      the active/inactive state in the primary workflow
169
        if active_only:
170
            results = filter(api.is_active, results)
171
172
        pairs = map(
173
            lambda brain: (brain.UID, brain.Title), results)
174
175
        return DisplayList(pairs)
176
177
178
schemata.finalizeATCTSchema(schema, folderish=True, moveDiscussion=False)
179
atapi.registerType(Departments, PROJECTNAME)
180