Passed
Push — master ( 7315c4...add294 )
by Jordi
08:31 queued 03:33
created

BatchFolderContentsView.__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
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
import collections
9
10
from bika.lims import api
11
from bika.lims.api.security import check_permission
12
from bika.lims import bikaMessageFactory as _
13
from bika.lims.browser.bika_listing import BikaListingView
14
from bika.lims.permissions import AddBatch
15
from bika.lims.utils import get_link
16
17
18
class BatchFolderContentsView(BikaListingView):
19
    """Listing View for all Batches in the System
20
    """
21
22
    def __init__(self, context, request):
23
        super(BatchFolderContentsView, self).__init__(context, request)
24
25
        self.catalog = "bika_catalog"
26
        self.contentFilter = {
27
            "portal_type": "Batch",
28
            "sort_on": "created",
29
            "sort_order": "descending",
30
            "cancellation_state": "active"
31
        }
32
33
        self.context_actions = {}
34
35
        self.show_select_all_checkbox = False
36
        self.show_select_column = True
37
        self.pagesize = 30
38
39
        batch_image_path = "/++resource++bika.lims.images/batch_big.png"
40
        self.icon = "{}{}".format(self.portal_url, batch_image_path)
41
        self.title = self.context.translate(_("Batches"))
42
        self.description = ""
43
44
        self.columns = collections.OrderedDict((
45
            ("Title", {
46
                "title": _("Title"),
47
                "index": "title", }),
48
            ("BatchID", {
49
                "title": _("Batch ID"),
50
                "index": "getId", }),
51
            ("Description", {
52
                "title": _("Description"),
53
                "sortable": False, }),
54
            ("BatchDate", {
55
                "title": _("Date"), }),
56
            ("Client", {
57
                "title": _("Client"),
58
                "index": "getClientTitle", }),
59
            ("ClientID", {
60
                "title": _("Client ID"),
61
                "index": "getClientID", }),
62
            ("ClientBatchID", {
63
                "title": _("Client Batch ID"),
64
                "index": "getClientBatchID", }),
65
            ("state_title", {
66
                "title": _("State"),
67
                "sortable": False, }),
68
            ("created", {
69
                "title": _("Created"),
70
                "index": "created",
71
            }),
72
        ))
73
74
        self.review_states = [
75
            {
76
                "id": "default",
77
                "contentFilter": {"review_state": "open"},
78
                "title": _("Open"),
79
                "transitions": [{"id": "close"}, {"id": "cancel"}],
80
                "columns": self.columns.keys(),
81
            }, {
82
                "id": "closed",
83
                "contentFilter": {"review_state": "closed"},
84
                "title": _("Closed"),
85
                "transitions": [{"id": "open"}],
86
                "columns": self.columns.keys(),
87
            }, {
88
                "id": "cancelled",
89
                "title": _("Cancelled"),
90
                "transitions": [{"id": "reinstate"}],
91
                "contentFilter": {"cancellation_state": "cancelled"},
92
                "columns": self.columns.keys(),
93
            }, {
94
                "id": "all",
95
                "title": _("All"),
96
                "transitions": [],
97
                "columns": self.columns.keys(),
98
            },
99
        ]
100
101
    def before_render(self):
102
        """Before template render hook
103
        """
104
        super(BatchFolderContentsView, self).before_render()
105
106
        if self.context.portal_type == "BatchFolder":
107
            self.request.set("disable_border", 1)
108
109
    def update(self):
110
        """Called before the listing renders
111
        """
112
        super(BatchFolderContentsView, self).update()
113
114
        if self.on_batch_folder() and self.can_add_batches():
115
            self.context_actions[_("Add")] = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
116
                "url": "createObject?type_name=Batch",
117
                "permission": "Add portal content",
118
                "icon": "++resource++bika.lims.images/add.png"}
119
120
    def on_batch_folder(self):
121
        """Checks if the current context is a Batch folder
122
        """
123
        return self.context.portal_type == "BatchFolder"
124
125
    def can_add_batches(self):
126
        """Checks if the current user is allowed to add batches
127
        """
128
        return check_permission(AddBatch, self.context)
129
130
    def folderitem(self, obj, item, index):
131
        """Applies new properties to the item (Batch) that is currently being
132
        rendered as a row in the list
133
134
        :param obj: client to be rendered as a row in the list
135
        :param item: dict representation of the batch, suitable for the list
136
        :param index: current position of the item within the list
137
        :type obj: ATContentType/DexterityContentType
138
        :type item: dict
139
        :type index: int
140
        :return: the dict representation of the item
141
        :rtype: dict
142
        """
143
144
        obj = api.get_object(obj)
145
        url = "{}/analysisrequests".format(api.get_url(obj))
146
        bid = api.get_id(obj)
147
        cbid = obj.getClientBatchID()
148
        title = api.get_title(obj)
149
        client = obj.getClient()
150
        created = api.get_creation_date(obj)
151
        date = obj.getBatchDate()
152
153
        item["BatchID"] = bid
154
        item["ClientBatchID"] = cbid
155
        item["replace"]["BatchID"] = get_link(url, bid)
156
        item["Title"] = title
157
        item["replace"]["Title"] = get_link(url, title)
158
        item["created"] = self.ulocalized_time(created, long_format=True)
159
        item["BatchDate"] = self.ulocalized_time(date, long_format=True)
160
161
        if client:
162
            client_url = api.get_url(client)
163
            client_name = client.getName()
164
            client_id = client.getClientID()
165
            item["Client"] = client_name
166
            item["ClientID"] = client_id
167
            item["replace"]["Client"] = get_link(client_url, client_name)
168
            item["replace"]["ClientID"] = get_link(client_url, client_id)
169
170
        return item
171