Passed
Push — 2.x ( 823daf...cad85d )
by Jordi
06:44
created

BatchFolderContentsView.can_add()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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