Passed
Push — 2.x ( 4f65d0...ad582b )
by Jordi
06:22
created

SuppliersView.__init__()   B

Complexity

Conditions 1

Size

Total Lines 86
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 86
rs 8.309
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-2024 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_link_for
26
from bika.lims.utils import get_email_link
27
from bika.lims.utils import get_phone_link
28
from senaite.core.i18n import translate
29
from senaite.app.listing import ListingView
30
from senaite.core.catalog import SETUP_CATALOG
31
from senaite.core.permissions import AddSupplier
32
33
34
class SuppliersView(ListingView):
35
36
    def __init__(self, context, request):
37
        super(SuppliersView, self).__init__(context, request)
38
39
        self.catalog = SETUP_CATALOG
40
41
        self.contentFilter = {
42
            "portal_type": "Supplier",
43
            "sort_on": "sortable_title",
44
            "sort_order": "ascending",
45
            "path": {
46
                "query": api.get_path(self.context),
47
                "depth": 1,
48
            },
49
        }
50
51
        self.context_actions = {
52
            _("listing_suppliers_action_add", default="Add"): {
53
                "url": "++add++Supplier",
54
                "permission": AddSupplier,
55
                "icon": "senaite_theme/icon/plus"
56
            }
57
        }
58
59
        self.title = translate(_(
60
            "listing_suppliers_title",
61
            default="Suppliers")
62
        )
63
        self.icon = api.get_icon("Suppliers", html_tag=False)
64
        self.show_select_column = True
65
66
        self.columns = collections.OrderedDict((
67
            ("Name", {
68
                "title": _(
69
                    u"listing_suppliers_column_name",
70
                    default=u"Name",
71
                ),
72
                "index": "sortable_title",
73
            }),
74
            ("Email", {
75
                "title": _(
76
                    u"listing_suppliers_column_email",
77
                    default=u"Email"
78
                ),
79
                "toggle": True,
80
            }),
81
            ("Phone", {
82
                "title": _(
83
                    u"listing_suppliers_column_phone",
84
                    default=u"Phone"
85
                ),
86
                "toggle": True,
87
            }),
88
            ("Fax", {
89
                "title": _(
90
                    u"listing_suppliers_column_fax",
91
                    default=u"Fax"
92
                ),
93
                "toggle": True,
94
            }),
95
        ))
96
97
        self.review_states = [
98
            {
99
                "id": "default",
100
                "title": _(
101
                    u"listing_suppliers_state_active",
102
                    default=u"Active"
103
                ),
104
                "contentFilter": {"is_active": True},
105
                "columns": self.columns.keys(),
106
            }, {
107
                "id": "inactive",
108
                "title": _(
109
                    u"listing_suppliers_state_inactive",
110
                    default=u"Inactive"
111
                ),
112
                "contentFilter": {'is_active': False},
113
                "columns": self.columns.keys(),
114
            }, {
115
                "id": "all",
116
                "title": _(
117
                    u"listing_suppliers_state_all",
118
                    default=u"All"
119
                ),
120
                "contentFilter": {},
121
                "columns": self.columns.keys(),
122
            },
123
        ]
124
125
    def folderitem(self, obj, item, index):
126
        obj = api.get_object(obj)
127
        email = obj.getEmail()
128
        phone = obj.getPhone()
129
130
        item["Name"] = api.get_title(obj)
131
        item["Fax"] = obj.getFax()
132
        item["Email"] = email
133
        item["Phone"] = phone
134
135
        item["replace"]["Name"] = get_link_for(obj)
136
        item["replace"]["Email"] = get_email_link(email)
137
        item["replace"]["Phone"] = get_phone_link(phone)
138
139
        return item
140