Passed
Pull Request — 2.x (#1958)
by Jordi
05:23
created

senaite.core.browser.views.client.contacts   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 70
dl 0
loc 126
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B ClientContactsView.__init__() 0 60 1
A ClientContactsView.folderitem() 0 28 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-2022 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from collections import OrderedDict
22
from bika.lims import api
23
from bika.lims import senaiteMessageFactory as _
24
from bika.lims.utils import get_link
25
from bika.lims.utils import get_email_link
26
from Products.CMFCore.permissions import AddPortalContent
27
from senaite.app.listing import ListingView
28
29
30
class ClientContactsView(ListingView):
31
    """Displays a table listing with the available client contacts from current
32
    context (client)
33
    """
34
35
    def __init__(self, context, request):
36
        super(ClientContactsView, self).__init__(context, request)
37
38
        self.catalog = "portal_catalog"
39
        self.contentFilter = {
40
            "portal_type": "ClientContact",
41
            "sort_on": "sortable_title",
42
            "path": {
43
                "query": api.get_path(self.context),
44
                "level": 0
45
            }
46
        }
47
48
        self.context_actions = {
49
            _('Add'): {
50
                "url": "++add++ClientContact",
51
                "icon": "++resource++bika.lims.images/add.png",
52
                "permission": AddPortalContent,
53
            }}
54
55
        t = self.context.translate
56
        self.title = t(_("Contacts"))
57
        self.description = t(_(""))
58
59
        icon_path = "/++resource++bika.lims.images/client_contact_big.png"
60
        self.icon = "{}{}".format(self.portal_url, icon_path)
61
62
        self.show_select_column = True
63
        self.pagesize = 50
64
65
        self.columns = OrderedDict((
66
            ("fullname", {
67
                "title": _("Full Name"),
68
                "index": "getFullname", }),
69
            ("username", {
70
                "title": _("User Name"), }),
71
            ("email", {
72
                "title": _("Email Address"), }),
73
            ("business_phone", {
74
                "title": _("Business Phone"), }),
75
            ("mobile_phone", {
76
                "title": _("MobilePhone"), }),
77
        ))
78
79
        self.review_states = [
80
            {
81
                "id": "default",
82
                "title": _("Active"),
83
                "contentFilter": {"is_active": True},
84
                "columns": self.columns.keys(),
85
            }, {
86
                "id": "inactive",
87
                "title": _("Inactive"),
88
                "contentFilter": {'is_active': False},
89
                "columns": self.columns.keys(),
90
            }, {
91
                "id": "all",
92
                "title": _("All"),
93
                "contentFilter": {},
94
                "columns": self.columns.keys(),
95
            },
96
        ]
97
98
    def folderitem(self, obj, item, index):
99
        """Service triggered each time an item is iterated in folderitems.
100
        The use of this service prevents the extra-loops in child objects.
101
102
        :obj: the instance of the class to be foldered
103
        :item: dict containing the properties of the object to be used by
104
            the template
105
        :index: current index of the item
106
        """
107
        obj = api.get_object(obj)
108
109
        fullname = obj.getFullname()
110
        email = obj.getEmail()
111
        item.update({
112
            "fullname": fullname,
113
            "username": obj.getUsername(),
114
            "email": email,
115
            "business_phone": obj.getBusinessPhone(),
116
            "mobile_phone": obj.getMobilePhone(),
117
        })
118
119
        url = api.get_url(obj)
120
        item["replace"].update({
121
            "fullname": get_link(url, fullname),
122
            "email": get_email_link(email)
123
        })
124
125
        return item
126