Passed
Push — 2.x ( 8942fe...e5fb61 )
by Jordi
05:53
created

ClientFolderContentsView.folderitem()   B

Complexity

Conditions 6

Size

Total Lines 56
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 56
rs 8.2986
c 0
b 0
f 0
cc 6
nop 4

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-2021 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import collections
22
23
from bika.lims import api
24
from bika.lims import bikaMessageFactory as _
25
from bika.lims.utils import check_permission
26
from bika.lims.utils import get_email_link
27
from bika.lims.utils import get_link
28
from bika.lims.utils import get_registry_value
29
from Products.CMFCore.permissions import ModifyPortalContent
30
from senaite.app.listing import ListingView
31
from senaite.core.catalog import CLIENT_CATALOG
32
from senaite.core.permissions import AddClient
33
from senaite.core.permissions import ManageAnalysisRequests
34
35
36
class ClientFolderContentsView(ListingView):
37
    """Listing view for all Clients
38
    """
39
40
    _LANDING_PAGE_REGISTRY_KEY = "bika.lims.client.default_landing_page"
41
    _DEFAULT_LANDING_PAGE = "analysisrequests"
42
43
    def __init__(self, context, request):
44
        super(ClientFolderContentsView, self).__init__(context, request)
45
46
        self.title = self.context.translate(_("Clients"))
47
        self.description = ""
48
        self.form_id = "list_clientsfolder"
49
        self.sort_on = "sortable_title"
50
        # Landing page to be added to the link of each client from the list
51
        self.landing_page = get_registry_value(
52
            self._LANDING_PAGE_REGISTRY_KEY, self._DEFAULT_LANDING_PAGE)
53
54
        self.catalog = CLIENT_CATALOG
55
        self.contentFilter = {
56
            "portal_type": "Client",
57
            "sort_on": "sortable_title",
58
            "sort_order": "ascending"
59
        }
60
61
        self.icon = "{}/{}".format(
62
            self.portal_url, "++resource++bika.lims.images/client_big.png")
63
64
        self.columns = collections.OrderedDict((
65
            ("title", {
66
                "title": _("Name"),
67
                "index": "sortable_title"},),
68
            ("ClientID", {
69
                "title": _("Client ID")}),
70
            ("EmailAddress", {
71
                "title": _("Email Address"),
72
                "sortable": False}),
73
            ("Country", {
74
                "toggle": False,
75
                "sortable": False,
76
                "title": _("Country")}),
77
            ("Province", {
78
                "toggle": False,
79
                "sortable": False,
80
                "title": _("Province")}),
81
            ("District", {
82
                "toggle": False,
83
                "sortable": False,
84
                "title": _("District")}),
85
            ("Phone", {
86
                "title": _("Phone"),
87
                "sortable": False}),
88
            ("Fax", {
89
                "toggle": False,
90
                "sortable": False,
91
                "title": _("Fax")}),
92
            ("BulkDiscount", {
93
                "toggle": False,
94
                "sortable": False,
95
                "title": _("Bulk Discount")}),
96
            ("MemberDiscount", {
97
                "toggle": False,
98
                "sortable": False,
99
                "title": _("Member Discount")}),
100
        ))
101
102
        self.review_states = [
103
            {
104
                "id": "default",
105
                "contentFilter": {"review_state": "active"},
106
                "title": _("Active"),
107
                "transitions": [{"id": "deactivate"}, ],
108
                "columns": self.columns.keys(),
109
            }, {
110
                "id": "inactive",
111
                "title": _("Inactive"),
112
                "contentFilter": {"review_state": "inactive"},
113
                "transitions": [{"id": "activate"}, ],
114
                "columns": self.columns.keys(),
115
            }, {
116
                "id": "all",
117
                "title": _("All"),
118
                "contentFilter": {},
119
                "transitions": [],
120
                "columns": self.columns.keys(),
121
            },
122
        ]
123
124
    def before_render(self):
125
        """Before template render hook
126
        """
127
        # Call `before_render` from the base class
128
        super(ClientFolderContentsView, self).before_render()
129
130
        # Render the Add button if the user has the AddClient permission
131
        if check_permission(AddClient, self.context):
132
            self.context_actions[_("Add")] = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
133
                "url": "createObject?type_name=Client",
134
                "icon": "++resource++bika.lims.images/add.png"
135
            }
136
137
        # Display a checkbox next to each client in the list if the user has
138
        # rights for ModifyPortalContent
139
        self.show_select_column = check_permission(ModifyPortalContent,
140
                                                   self.context)
141
142
    def isItemAllowed(self, obj):
143
        """Returns true if the current user has Manage AR rights for the
144
        current Client (item) to be rendered.
145
146
        :param obj: client to be rendered as a row in the list
147
        :type obj: ATContentType/DexterityContentType
148
        :return: True if the current user can see this Client. Otherwise, False.
149
        :rtype: bool
150
        """
151
        return check_permission(ManageAnalysisRequests, obj)
152
153
    def folderitem(self, obj, item, index):
154
        """Applies new properties to the item (Client) that is currently being
155
        rendered as a row in the list
156
157
        :param obj: client to be rendered as a row in the list
158
        :param item: dict representation of the client, suitable for the list
159
        :param index: current position of the item within the list
160
        :type obj: CatalogBrain
161
        :type item: dict
162
        :type index: int
163
        :return: the dict representation of the item
164
        :rtype: dict
165
        """
166
        obj = api.get_object(obj)
167
168
        # render a link to the defined start page
169
        link_url = "{}/{}".format(item["url"], self.landing_page)
170
        item["replace"]["title"] = get_link(link_url, item["title"])
171
172
        # Client ID
173
        client_id = obj.getClientID()
174
        item["ClientID"] = client_id
175
        if client_id:
176
            item["replace"]["ClientID"] = get_link(link_url, client_id)
177
178
        # Email address
179
        email = obj.getEmailAddress()
180
        item["EmailAddress"] = get_email_link(email)
181
        if email:
182
            item["replace"]["EmailAddress"] = get_email_link(email)
183
184
        # Country, Province, District
185
        item["Country"] = obj.getCountry()
186
        item["Province"] = obj.getProvince()
187
        item["District"] = obj.getDistrict()
188
189
        # Phone
190
        phone = obj.getPhone()
191
        item["Phone"] = phone
192
        if phone:
193
            item["replace"]["Phone"] = get_link("tel:{}".format(phone), phone)
194
195
        # Fax
196
        item["Fax"] = obj.getFax()
197
198
        # Bulk Discount
199
        bulk_discount = obj.getBulkDiscount()
200
        bulk_discount_value = _("Yes") if bulk_discount else _("No")
201
        item["replace"]["BulkDiscount"] = bulk_discount_value
202
203
        # Member Discount
204
        member_discount = obj.getMemberDiscountApplies()
205
        member_discount_value = _("Yes") if member_discount else _("No")
206
        item["replace"]["MemberDiscount"] = member_discount_value
207
208
        return item
209