Passed
Pull Request — 2.x (#1740)
by Jordi
05:25 queued 45s
created

ClientsView.__init__()   B

Complexity

Conditions 1

Size

Total Lines 76
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 76
rs 8.2109
c 0
b 0
f 0
cc 1
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-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 senaiteMessageFactory as _
25
from bika.lims.utils import get_email_link
26
from bika.lims.utils import get_link
27
from Products.CMFCore.permissions import AddPortalContent
28
from senaite.app.listing import ListingView
29
30
31
class ClientsView(ListingView):
32
    """Listing view for Clients
33
    """
34
35
    def __init__(self, context, request):
36
        super(ClientsView, self).__init__(context, request)
37
38
        self.contentFilter = {
39
            "portal_type": "Client",
40
            "sort_on": "sortable_title",
41
            "sort_order": "ascending"
42
        }
43
44
        self.title = self.context.translate(_("Clients"))
45
46
        self.show_select_column = True
47
        self.show_select_all_checkbox = False
48
49
        self.context_actions = {
50
            _("Add"): {
51
                "url": "createObject?type_name=Client",
52
                'permission': AddPortalContent,
53
                "icon": "add.png"
54
            }
55
        }
56
57
        self.columns = collections.OrderedDict((
58
            ("title", {
59
                "title": _("Name"),
60
                "index": "sortable_title"},),
61
            ("getClientID", {
62
                "title": _("Client ID")}),
63
            ("EmailAddress", {
64
                "title": _("Email Address"),
65
                "sortable": False}),
66
            ("getCountry", {
67
                "toggle": False,
68
                "sortable": False,
69
                "title": _("Country")}),
70
            ("getProvince", {
71
                "toggle": False,
72
                "sortable": False,
73
                "title": _("Province")}),
74
            ("getDistrict", {
75
                "toggle": False,
76
                "sortable": False,
77
                "title": _("District")}),
78
            ("Phone", {
79
                "title": _("Phone"),
80
                "sortable": False}),
81
            ("Fax", {
82
                "toggle": False,
83
                "sortable": False,
84
                "title": _("Fax")}),
85
            ("BulkDiscount", {
86
                "toggle": False,
87
                "sortable": False,
88
                "title": _("Bulk Discount")}),
89
            ("MemberDiscountApplies", {
90
                "toggle": False,
91
                "sortable": False,
92
                "title": _("Member Discount")}),
93
        ))
94
95
        self.review_states = [
96
            {
97
                "id": "default",
98
                "contentFilter": {"review_state": "active"},
99
                "title": _("Active"),
100
                "columns": self.columns.keys(),
101
            }, {
102
                "id": "inactive",
103
                "title": _("Inactive"),
104
                "contentFilter": {"review_state": "inactive"},
105
                "columns": self.columns.keys(),
106
            }, {
107
                "id": "all",
108
                "title": _("All"),
109
                "contentFilter": {},
110
                "columns": self.columns.keys(),
111
            },
112
        ]
113
114
    def folderitem(self, obj, item, index):
115
        """Applies new properties to the item (Client) that is currently being
116
        rendered as a row in the list
117
118
        :param obj: client to be rendered as a row in the list
119
        :param item: dict representation of the client, suitable for the list
120
        :param index: current position of the item within the list
121
        :type obj: CatalogBrain
122
        :type item: dict
123
        :type index: int
124
        :return: the dict representation of the item
125
        :rtype: dict
126
        """
127
        obj = api.get_object(obj)
128
        obj_url = self.get_client_url(obj)
129
        phone = obj.getPhone()
130
        item["replace"].update({
131
            "title": get_link(obj_url, api.get_title(obj)),
132
            "getClientID": get_link(obj_url, obj.getClientID()),
133
            "EmailAddress": get_email_link(obj.getEmailAddress()),
134
            "BulkDiscount": obj.getBulkDiscount(),
135
            "MemberDiscountApplies": obj.getMemberDiscountApplies(),
136
            "Phone": phone and get_link("tel:{}".format(phone), phone) or "",
137
        })
138
        return item
139
140
    def get_client_url(self, client):
141
        """Returns the url to the client's landing page
142
        """
143
        landing = self.get_client_landing_page()
144
        return "{}/{}".format(api.get_url(client), landing)
145
146
    def get_client_landing_page(self):
147
        """Returns the id of the view from inside Client to which the user has
148
        to be redirected when clicking to the link of a Client
149
        """
150
        record_key = "bika.lims.client.default_landing_page"
151
        return api.get_registry_record(record_key, default="analysisrequests")
152