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

LabContactsView.before_render()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
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 bikaMessageFactory as _
11
from bika.lims.browser.bika_listing import BikaListingView
12
from bika.lims.config import PROJECTNAME
13
from bika.lims.interfaces import ILabContacts
14
from bika.lims.utils import get_email_link
15
from bika.lims.utils import get_link
16
from plone.app.content.browser.interfaces import IFolderContentsView
17
from plone.app.folder.folder import ATFolder
18
from plone.app.folder.folder import ATFolderSchema
19
from plone.app.layout.globals.interfaces import IViewView
20
from Products.Archetypes import atapi
21
from Products.ATContentTypes.content import schemata
22
from zope.interface.declarations import implements
23
24
25
# TODO: Separate content and view into own modules!
26
27
28
class LabContactsView(BikaListingView):
29
    implements(IFolderContentsView, IViewView)
30
31 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...
32
        super(LabContactsView, self).__init__(context, request)
33
34
        self.catalog = "bika_setup_catalog"
35
        self.contentFilter = {
36
            "portal_type": "LabContact",
37
            "sort_on": "sortable_title",
38
            "sort_order": "ascending",
39
        }
40
41
        self.context_actions = {
42
            _("Add"): {
43
                "url": "createObject?type_name=LabContact",
44
                "permission": "Add portal content",
45
                "icon": "++resource++bika.lims.images/add.png"}
46
        }
47
48
        self.title = self.context.translate(_("Lab Contacts"))
49
        self.icon = "{}/{}".format(
50
            self.portal_url,
51
            "/++resource++bika.lims.images/lab_contact_big.png"
52
        )
53
54
        self.show_sort_column = False
55
        self.show_select_row = False
56
        self.show_select_column = True
57
        self.pagesize = 25
58
59
        self.columns = collections.OrderedDict((
60
            # TODO: Better sort by last name (index required!)
61
            ("Fullname", {
62
                "title": _("Name"),
63
                "index": "getFullname"}),
64
            ("Department", {
65
                "title": _("Department"),
66
                "toggle": True}),
67
            ("BusinessPhone", {
68
                "title": _("Phone"),
69
                "toggle": True}),
70
            ("Fax", {
71
                "title": _("Fax"),
72
                "toggle": False}),
73
            ("MobilePhone", {
74
                "title": _("Mobile Phone"),
75
                "toggle": True}),
76
            ("EmailAddress", {
77
                "title": _("Email Address"),
78
                "toggle": True}),
79
        ))
80
81
        self.review_states = [
82
            {
83
                "id": "default",
84
                "title": _("Active"),
85
                "contentFilter": {"inactive_state": "active"},
86
                "transitions": [{"id": "deactivate"}, ],
87
                "columns": self.columns.keys(),
88
            }, {
89
                "id": "inactive",
90
                "title": _("Dormant"),
91
                "contentFilter": {"inactive_state": "inactive"},
92
                "transitions": [{"id": "activate"}, ],
93
                "columns": self.columns.keys(),
94
            }, {
95
                "id": "all",
96
                "title": _("All"),
97
                "contentFilter": {},
98
                "columns": self.columns.keys(),
99
            },
100
        ]
101
102
    def before_render(self):
103
        """Before template render hook
104
        """
105
        # Don't allow any context actions
106
        self.request.set("disable_border", 1)
107
108
    def folderitem(self, obj, item, index):
109
        """Service triggered each time an item is iterated in folderitems.
110
        The use of this service prevents the extra-loops in child objects.
111
        :obj: the instance of the class to be foldered
112
        :item: dict containing the properties of the object to be used by
113
            the template
114
        :index: current index of the item
115
        """
116
117
        fullname = obj.getFullname()
118
        if fullname:
119
            item["Fullname"] = fullname
120
            item["replace"]["Fullname"] = get_link(
121
                obj.absolute_url(), value=fullname)
122
        else:
123
            item["Fullname"] = ""
124
125
        departments = obj.getDepartments()
126
        if departments:
127
            links = map(
128
                lambda o: get_link(o.absolute_url(),
129
                                   value=o.Title(),
130
                                   css_class="link"),
131
                departments)
132
            item["replace"]["Department"] = ", ".join(links)
133
134
        email = obj.getEmailAddress()
135
        if email:
136
            item["EmailAddress"] = obj.getEmailAddress()
137
            item["replace"]["EmailAddress"] = get_email_link(
138
                email, value=email)
139
140
        item["BusinessPhone"] = obj.getBusinessPhone()
141
        item["Fax"] = obj.getBusinessFax()
142
        item["MobilePhone"] = obj.getMobilePhone()
143
144
        return item
145
146
147
schema = ATFolderSchema.copy()
148
149
150
class LabContacts(ATFolder):
151
    implements(ILabContacts)
152
    displayContentsTab = False
153
    schema = schema
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable schema does not seem to be defined.
Loading history...
154
155
156
schemata.finalizeATCTSchema(schema, folderish=True, moveDiscussion=False)
157
atapi.registerType(LabContacts, PROJECTNAME)
158