Passed
Push — 2.x ( 2a1b20...d626d0 )
by Jordi
07:27
created

ContactsView.__init__()   A

Complexity

Conditions 4

Size

Total Lines 51
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 51
rs 9.0399
c 0
b 0
f 0
cc 4
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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-2025 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from collections import OrderedDict
22
23
from bika.lims import api
24
from bika.lims import bikaMessageFactory as _
25
from bika.lims.utils import get_link
26
from senaite.core.browser.clients.client.contacts.view import \
27
    ClientContactsView
28
29
30
class ContactsView(ClientContactsView):
31
    """Global Contacts listing view showing all system contacts
32
    """
33
34
    def __init__(self, context, request):
35
        super(ContactsView, self).__init__(context, request)
36
37
        self.contentFilter = {
38
            "portal_type": "Contact",
39
            "sort_on": "sortable_title",
40
        }
41
42
        global_contacts_path = "/".join(self.context.getPhysicalPath())
43
44
        # Override the content filter based on the cookie
45
        if self.include_client_contacts:
46
            self.contentFilter.pop("path", None)
47
        else:
48
            # Show only global contacts
49
            self.contentFilter = {
50
                "portal_type": "Contact",
51
                "sort_on": "sortable_title",
52
                "path": {"query": global_contacts_path, "level": 0}
53
            }
54
55
        # Add Location column after Full Name
56
        self.columns = OrderedDict((
57
            ("getFullname", {
58
                "title": _("Full Name"),
59
                "index": "getFullname",
60
                "sortable": True, }),
61
            ("Username", {
62
                "title": _("User Name"), }),
63
            ("getEmailAddress", {
64
                "title": _("Email Address"), }),
65
            ("getBusinessPhone", {
66
                "title": _("Business Phone"), }),
67
            ("getMobilePhone", {
68
                "title": _("MobilePhone"), }),
69
            ("Location", {
70
                "title": _("Location"),
71
                "sortable": False, }),
72
        ))
73
74
        for review_state in self.review_states:
75
            # ensure all columns are included
76
            review_state["columns"] = self.columns.keys()
77
            if self.include_client_contacts:
78
                # remove the path query
79
                review_state["contentFilter"].pop("path", None)
80
            else:
81
                # add the path query
82
                review_state["contentFilter"]["path"] = {
83
                    "query": global_contacts_path,
84
                    "level": 0
85
                }
86
87
    def update(self):
88
        """Update hook
89
        """
90
        super(ContactsView, self).update()
91
92
    @property
93
    def include_client_contacts(self):
94
        """Returns if client contacts should be included or not
95
        """
96
        return self.request.cookies.get("include_client_contacts", "") == "1"
97
98
    def folderitem(self, obj, item, index):
99
        """Augment folder item with Location information
100
        """
101
        item = super(ContactsView, self).folderitem(obj, item, index)
102
103
        # Get the contact object
104
        contact = api.get_object(obj)
105
        parent = api.get_parent(contact)
106
107
        # Set the parent location (either Client or global)
108
        location = parent.Title()
109
        item["Location"] = location
110
111
        # Make the location a clickable link
112
        parent_url = api.get_url(parent)
113
        item["replace"]["Location"] = get_link(parent_url, location)
114
115
        return item
116