Passed
Push — 2.x ( 6e01c2...52d1e5 )
by Jordi
09:34
created

bika.lims.browser.batchfolder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 140
dl 0
loc 224
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A BatchFolderContentsView.to_pretty_label() 0 8 1
A BatchFolderContentsView.update() 0 15 2
A BatchFolderContentsView.folderitem() 0 53 3
A BatchFolderContentsView.get_add_url() 0 6 1
B BatchFolderContentsView.__init__() 0 81 1
A BatchFolderContentsView.get_batches_container() 0 5 1
A BatchFolderContentsView.can_add() 0 8 2
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 senaite.core.permissions import AddBatch
26
from bika.lims.utils import get_link
27
from bika.lims.utils import get_progress_bar_html
28
from plone.memoize import view
29
from Products.CMFCore.permissions import View
30
from senaite.app.listing import ListingView
31
from senaite.core.catalog import SENAITE_CATALOG
32
33
34
class BatchFolderContentsView(ListingView):
35
    """Listing view for Batches
36
    """
37
38
    def __init__(self, context, request):
39
        super(BatchFolderContentsView, self).__init__(context, request)
40
41
        self.catalog = SENAITE_CATALOG
42
        self.contentFilter = {
43
            "portal_type": "Batch",
44
            "sort_on": "created",
45
            "sort_order": "descending",
46
            "is_active": True,
47
        }
48
49
        self.title = self.context.translate(_("Batches"))
50
        self.description = ""
51
52
        self.show_select_column = True
53
        self.form_id = "batches"
54
        self.context_actions = {}
55
        self.icon = "{}{}".format(self.portal_url, "/senaite_theme/icon/batch")
56
57
        self.columns = collections.OrderedDict((
58
            ("Title", {
59
                "title": _("Title"),
60
                "index": "title", }),
61
            ("Progress", {
62
                "title": _("Progress"),
63
                "index": "getProgress",
64
                "sortable": True,
65
                "toggle": True}),
66
            ("BatchID", {
67
                "title": _("Batch ID"),
68
                "index": "getId", }),
69
            ("BatchLabels", {
70
                "title": _("Batch Labels"),
71
                "sortable": False, }),
72
            ("Description", {
73
                "title": _("Description"),
74
                "sortable": False, }),
75
            ("BatchDate", {
76
                "title": _("Date"), }),
77
            ("Client", {
78
                "title": _("Client"),
79
                "index": "getClientTitle", }),
80
            ("ClientID", {
81
                "title": _("Client ID"),
82
                "index": "getClientID", }),
83
            ("ClientBatchID", {
84
                "title": _("Client Batch ID"),
85
                "index": "getClientBatchID", }),
86
            ("state_title", {
87
                "title": _("State"),
88
                "sortable": False, }),
89
            ("created", {
90
                "title": _("Created"),
91
                "index": "created",
92
            }),
93
        ))
94
95
        self.review_states = [
96
            {
97
                "id": "default",
98
                "contentFilter": {"review_state": "open"},
99
                "title": _("Open"),
100
                "transitions": [],
101
                "columns": self.columns.keys(),
102
            }, {
103
                "id": "closed",
104
                "contentFilter": {"review_state": "closed"},
105
                "title": _("Closed"),
106
                "transitions": [],
107
                "columns": self.columns.keys(),
108
            }, {
109
                "id": "cancelled",
110
                "title": _("Cancelled"),
111
                "transitions": [],
112
                "contentFilter": {"is_active": False},
113
                "columns": self.columns.keys(),
114
            }, {
115
                "id": "all",
116
                "title": _("All"),
117
                "transitions": [],
118
                "columns": self.columns.keys(),
119
            },
120
        ]
121
122
    def update(self):
123
        """Before template render hook
124
        """
125
        super(BatchFolderContentsView, self).update()
126
127
        if self.can_add():
128
            # Add button. Note we set "View" as the permission, cause when no
129
            # permission is set, system fallback to "Add portal content" for
130
            # current context
131
            add_ico = "{}{}".format(self.portal_url, "/senaite_theme/icon/plus")
132
            self.context_actions = {
133
                _("Add"): {
134
                    "url": self.get_add_url(),
135
                    "permission": View,
136
                    "icon": add_ico
137
                }
138
            }
139
140
    @view.memoize
141
    def get_add_url(self):
142
        """Return the batch add URL
143
        """
144
        container = self.get_batches_container()
145
        return "{}/createObject?type_name=Batch".format(api.get_url(container))
146
147
    @view.memoize
148
    def get_batches_container(self):
149
        """Returns the container object where new batches will be added
150
        """
151
        return api.get_current_client() or self.context
152
153
    @view.memoize
154
    def can_add(self):
155
        """Returns whether the current user can add new batches or not
156
        """
157
        container = self.get_batches_container()
158
        if not api.security.check_permission(AddBatch, container):
159
            return False
160
        return True
161
162
    def to_pretty_label(self, label):
163
        """Make a pretty label for the given label string
164
165
        :param label: text batch label
166
        :returns: Bootstrap HTML batch label
167
        """
168
        tpl = u"<span class='badge badge-secondary mr-1'>{}</span>"
169
        return tpl.format(api.safe_unicode(label))
170
171
    def folderitem(self, obj, item, index):
172
        """Applies new properties to the item (Batch) that is currently being
173
        rendered as a row in the list
174
175
        :param obj: client to be rendered as a row in the list
176
        :param item: dict representation of the batch, suitable for the list
177
        :param index: current position of the item within the list
178
        :type obj: ATContentType/DexterityContentType
179
        :type item: dict
180
        :type index: int
181
        :return: the dict representation of the item
182
        :rtype: dict
183
        """
184
185
        obj = api.get_object(obj)
186
        url = "{}/analysisrequests".format(api.get_url(obj))
187
        bid = api.get_id(obj)
188
        cbid = obj.getClientBatchID()
189
        title = api.get_title(obj)
190
        client = obj.getClient()
191
        created = api.get_creation_date(obj)
192
        date = obj.getBatchDate()
193
        batch_labels = obj.getLabelNames()
194
195
        # total sample progress
196
        progress = obj.getProgress()
197
        item["Progress"] = progress
198
        item["replace"]["Progress"] = get_progress_bar_html(progress)
199
200
        item["BatchID"] = bid
201
        item["ClientBatchID"] = cbid
202
        item["replace"]["BatchID"] = get_link(url, bid)
203
        item["Title"] = title
204
        item["replace"]["Title"] = get_link(url, title)
205
        item["created"] = self.ulocalized_time(created, long_format=True)
206
        item["BatchDate"] = self.ulocalized_time(date, long_format=True)
207
        item["BatchLabels"] = ""
208
209
        if batch_labels:
210
            item["BatchLabels"] = ",".join(batch_labels)
211
            item["replace"]["BatchLabels"] = "".join(map(
212
                self.to_pretty_label, batch_labels))
213
214
        if client:
215
            client_url = api.get_url(client)
216
            client_name = client.getName()
217
            client_id = client.getClientID()
218
            item["Client"] = client_name
219
            item["ClientID"] = client_id
220
            item["replace"]["Client"] = get_link(client_url, client_name)
221
            item["replace"]["ClientID"] = get_link(client_url, client_id)
222
223
        return item
224