Passed
Push — master ( bcbb8c...8785a0 )
by Jordi
05:37 queued 01:13
created

AddDuplicateView.folderitems()   B

Complexity

Conditions 2

Size

Total Lines 43
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 43
rs 8.968
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
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
import collections
9
from operator import itemgetter
10
11
from bika.lims import api
12
from bika.lims import bikaMessageFactory as _
13
from bika.lims.browser.bika_listing import BikaListingView
14
from bika.lims.browser.worksheet.tools import showRejectionMessage
15
from bika.lims.permissions import EditWorksheet
16
from bika.lims.permissions import ManageWorksheets
17
from bika.lims.utils import get_link
18
from plone.memoize import view
19
from plone.protect import CheckAuthenticator
20
21
22
class AddDuplicateView(BikaListingView):
23
    """Categorize Analyses by Slot/AR
24
    """
25
26
    def __init__(self, context, request):
27
        super(AddDuplicateView, self).__init__(context, request)
28
29
        self.catalog = "bika_analysis_catalog"
30
        self.contentFilter = {
31
            "portal_type": "Analysis",
32
            "getWorksheetUID": "",
33
        }
34
35
        self.icon = "{}/{}".format(
36
            self.portal_url,
37
            "/++resource++bika.lims.images/worksheet_big.png"
38
        )
39
40
        self.context_actions = {}
41
        self.title = _("Add Duplicate")
42
        self.show_sort_column = False
43
        self.show_select_row = False
44
        self.show_select_all_checkbox = False
45
        self.show_column_toggles = False
46
        self.show_select_column = True
47
        self.pagesize = 999999
48
        self.allow_edit = True
49
        self.show_search = False
50
        self.show_categories = False
51
52
        self.columns = collections.OrderedDict((
53
            ("Position", {
54
                "title": _("Position"),
55
                "sortable": False}),
56
            ("RequestID", {
57
                "title": _("Request ID"),
58
                "sortable": False}),
59
            ("Client", {
60
                "title": _("Client"),
61
                "sortable": False}),
62
            ("created", {
63
                "title": _("Date Requested"),
64
                "sortable": False}),
65
66
        ))
67
68
        self.review_states = [
69
            {
70
                "id": "default",
71
                "title": _("All"),
72
                "contentFilter": {},
73
                "transitions": [{"id": "add"}],
74
                "custom_transitions": [
75
                    {
76
                        "id": "add",
77
                        "title": _("Add"),
78
                        "url": self.__name__,
79
                    }
80
                ],
81
                "columns": self.columns.keys()
82
            },
83
        ]
84
85
    def __call__(self):
86
        template = super(AddDuplicateView, self).__call__()
87
        # TODO: Refactor Worfklow
88
        grant = self.is_edit_allowed() and self.is_manage_allowed()
89
        if not grant:
90
            redirect_url = api.get_url(self.context)
91
            return self.request.response.redirect(redirect_url)
92
        # TODO: Refactor this function call
93
        showRejectionMessage(self.context)
94
        # Handle form submission
95
        if self.request.form.get("submitted"):
96
            CheckAuthenticator(self.request)
97
            self.handle_submit()
98
        return template
99
100
    def handle_submit(self):
101
        """Handle form submission
102
        """
103
        form = self.request.form
104
        # Selected AR UIDs
105
        uids = form.get("uids")
106
        container_mapping = self.get_container_mapping()
107
        for uid in uids:
108
            src_pos = container_mapping[uid]
109
            self.context.addDuplicateAnalyses(src_pos)
110
        redirect_url = "{}/{}".format(
111
            api.get_url(self.context), "manage_results")
112
        self.request.response.redirect(redirect_url)
113
114
    @view.memoize
115
    def is_edit_allowed(self):
116
        """Check if edit is allowed
117
        """
118
        checkPermission = self.context.portal_membership.checkPermission
119
        return checkPermission(EditWorksheet, self.context)
120
121
    @view.memoize
122
    def is_manage_allowed(self):
123
        """Check if manage is allowed
124
        """
125
        checkPermission = self.context.portal_membership.checkPermission
126
        return checkPermission(ManageWorksheets, self.context)
127
128
    @view.memoize
129
    def get_container_mapping(self):
130
        """Returns a mapping of container -> postition
131
        """
132
        layout = self.context.getLayout()
133
        container_mapping = {}
134
        for slot in layout:
135
            if slot["type"] != "a":
136
                continue
137
            position = slot["position"]
138
            container_uid = slot["container_uid"]
139
            container_mapping[container_uid] = position
140
        return container_mapping
141
142
    def folderitems(self):
143
        """Custom folderitems for Worksheet ARs
144
        """
145
        items = []
146
        for ar, pos in self.get_container_mapping().items():
147
            ar = api.get_object_by_uid(ar)
148
            ar_id = api.get_id(ar)
149
            ar_uid = api.get_uid(ar)
150
            ar_url = api.get_url(ar)
151
            ar_title = api.get_title(ar)
152
            url = api.get_url(ar)
153
            client = ar.getClient()
154
            client_url = api.get_url(client)
155
            client_title = api.get_title(client)
156
157
            item = {
158
                "obj": ar,
159
                "id": ar_id,
160
                "uid": ar_uid,
161
                "title": ar_title,
162
                "type_class": "contenttype-AnalysisRequest",
163
                "url": url,
164
                "relative_url": url,
165
                "view_url": url,
166
                "Position": pos,
167
                "RequestID": ar_id,
168
                "Client": client_title,
169
                "created": self.ulocalized_time(ar.created(), long_format=1),
170
                "replace": {
171
                    "Client": get_link(client_url, value=client_title),
172
                    "RequestID": get_link(ar_url, value=ar_title),
173
                },
174
                "before": {},
175
                "after": {},
176
                "choices": {},
177
                "class": {},
178
                "state_class": "state-active",
179
                "allow_edit": [],
180
                "required": [],
181
            }
182
            items.append(item)
183
        items = sorted(items, key=itemgetter("Position"))
184
        return items
185