Passed
Push — master ( 0b6604...4c3d35 )
by Ramon
04:28
created

()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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-2019 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.api.security import check_permission
25
from bika.lims import bikaMessageFactory as _
26
from bika.lims.browser.bika_listing import BikaListingView
27
from bika.lims.interfaces import IClient
28
from bika.lims.permissions import AddBatch
29
from bika.lims.utils import get_link
30
31
32
class BatchFolderContentsView(BikaListingView):
33
    """Listing View for all Batches in the System
34
    """
35
36
    def __init__(self, context, request):
37
        super(BatchFolderContentsView, self).__init__(context, request)
38
39
        self.catalog = "bika_catalog"
40
        self.contentFilter = {
41
            "portal_type": "Batch",
42
            "sort_on": "created",
43
            "sort_order": "descending",
44
            "is_active": True
45
        }
46
47
        self.context_actions = {}
48
49
        self.show_select_all_checkbox = False
50
        self.show_select_column = True
51
        self.pagesize = 30
52
53
        batch_image_path = "/++resource++bika.lims.images/batch_big.png"
54
        self.icon = "{}{}".format(self.portal_url, batch_image_path)
55
        self.title = self.context.translate(_("Batches"))
56
        self.description = ""
57
58
        self.columns = collections.OrderedDict((
59
            ("Title", {
60
                "title": _("Title"),
61
                "index": "title", }),
62
            ("BatchID", {
63
                "title": _("Batch ID"),
64
                "index": "getId", }),
65
            ("Description", {
66
                "title": _("Description"),
67
                "sortable": False, }),
68
            ("BatchDate", {
69
                "title": _("Date"), }),
70
            ("Client", {
71
                "title": _("Client"),
72
                "index": "getClientTitle", }),
73
            ("ClientID", {
74
                "title": _("Client ID"),
75
                "index": "getClientID", }),
76
            ("ClientBatchID", {
77
                "title": _("Client Batch ID"),
78
                "index": "getClientBatchID", }),
79
            ("state_title", {
80
                "title": _("State"),
81
                "sortable": False, }),
82
            ("created", {
83
                "title": _("Created"),
84
                "index": "created",
85
            }),
86
        ))
87
88
        self.review_states = [
89
            {
90
                "id": "default",
91
                "contentFilter": {"review_state": "open"},
92
                "title": _("Open"),
93
                "transitions": [{"id": "close"}, {"id": "cancel"}],
94
                "columns": self.columns.keys(),
95
            }, {
96
                "id": "closed",
97
                "contentFilter": {"review_state": "closed"},
98
                "title": _("Closed"),
99
                "transitions": [{"id": "open"}],
100
                "columns": self.columns.keys(),
101
            }, {
102
                "id": "cancelled",
103
                "title": _("Cancelled"),
104
                "transitions": [{"id": "reinstate"}],
105
                "contentFilter": {"is_active": False},
106
                "columns": self.columns.keys(),
107
            }, {
108
                "id": "all",
109
                "title": _("All"),
110
                "transitions": [],
111
                "columns": self.columns.keys(),
112
            },
113
        ]
114
115
    def before_render(self):
116
        """Before template render hook
117
        """
118
        super(BatchFolderContentsView, self).before_render()
119
120
        if self.context.portal_type == "BatchFolder":
121
            self.request.set("disable_border", 1)
122
123
        # By default, only users with AddBatch permissions for the current
124
        # context can add batches.
125
        self.context_actions = {
126
            _("Add"): {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
127
                "url": "createObject?type_name=Batch",
128
                "permission": AddBatch,
129
                "icon": "++resource++bika.lims.images/add.png"
130
            }
131
        }
132
133
        # If current user is a client contact and current context is not a
134
        # Client, then modify the url for Add action so the Batch gets created
135
        # inside the Client object to which the current user belongs. The
136
        # reason is that Client contacts do not have privileges to create
137
        # Batches inside portal/batches
138
        if not IClient.providedBy(self.context):
139
            # Get the client the current user belongs to
140
            client = api.get_current_client()
141
            if client and check_permission(AddBatch, client):
142
                add_url = self.context_actions[_("Add")]["url"]
143
                add_url = "{}/{}".format(api.get_url(client), add_url)
144
                self.context_actions[_("Add")]["url"] = add_url
145
                del(self.context_actions[_("Add")]["permission"])
146
147
    def folderitem(self, obj, item, index):
148
        """Applies new properties to the item (Batch) that is currently being
149
        rendered as a row in the list
150
151
        :param obj: client to be rendered as a row in the list
152
        :param item: dict representation of the batch, suitable for the list
153
        :param index: current position of the item within the list
154
        :type obj: ATContentType/DexterityContentType
155
        :type item: dict
156
        :type index: int
157
        :return: the dict representation of the item
158
        :rtype: dict
159
        """
160
161
        obj = api.get_object(obj)
162
        url = "{}/analysisrequests".format(api.get_url(obj))
163
        bid = api.get_id(obj)
164
        cbid = obj.getClientBatchID()
165
        title = api.get_title(obj)
166
        client = obj.getClient()
167
        created = api.get_creation_date(obj)
168
        date = obj.getBatchDate()
169
170
        item["BatchID"] = bid
171
        item["ClientBatchID"] = cbid
172
        item["replace"]["BatchID"] = get_link(url, bid)
173
        item["Title"] = title
174
        item["replace"]["Title"] = get_link(url, title)
175
        item["created"] = self.ulocalized_time(created, long_format=True)
176
        item["BatchDate"] = self.ulocalized_time(date, long_format=True)
177
178
        if client:
179
            client_url = api.get_url(client)
180
            client_name = client.getName()
181
            client_id = client.getClientID()
182
            item["Client"] = client_name
183
            item["ClientID"] = client_id
184
            item["replace"]["Client"] = get_link(client_url, client_name)
185
            item["replace"]["ClientID"] = get_link(client_url, client_id)
186
187
        return item
188