|
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 import bikaMessageFactory as _ |
|
12
|
|
|
from bika.lims.browser.bika_listing import BikaListingView |
|
13
|
|
|
from bika.lims.browser.worksheet.tools import showRejectionMessage |
|
14
|
|
|
from bika.lims.catalog import CATALOG_ANALYSIS_LISTING |
|
15
|
|
|
from bika.lims.config import PRIORITIES |
|
16
|
|
|
from bika.lims.permissions import EditWorksheet |
|
17
|
|
|
from bika.lims.permissions import ManageWorksheets |
|
18
|
|
|
from bika.lims.utils import get_image |
|
19
|
|
|
from bika.lims.utils import t |
|
20
|
|
|
from bika.lims.vocabularies import CatalogVocabulary |
|
21
|
|
|
from DateTime import DateTime |
|
22
|
|
|
from plone.memoize import view |
|
23
|
|
|
from plone.protect import CheckAuthenticator |
|
24
|
|
|
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class AddAnalysesView(BikaListingView): |
|
28
|
|
|
"""Assign Analyses View for Worksheets |
|
29
|
|
|
""" |
|
30
|
|
|
template = ViewPageTemplateFile("../templates/add_analyses.pt") |
|
31
|
|
|
|
|
32
|
|
|
def __init__(self, context, request): |
|
33
|
|
|
super(AddAnalysesView, self).__init__(context, request) |
|
34
|
|
|
|
|
35
|
|
|
self.catalog = CATALOG_ANALYSIS_LISTING |
|
36
|
|
|
|
|
37
|
|
|
self.contentFilter = { |
|
38
|
|
|
"portal_type": "Analysis", |
|
39
|
|
|
"review_state": "unassigned", |
|
40
|
|
|
"isSampleReceived": True, |
|
41
|
|
|
"sort_on": "getPrioritySortkey", |
|
42
|
|
|
"cancellation_state": "active", |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
self.icon = "{}/{}".format( |
|
46
|
|
|
self.portal_url, |
|
47
|
|
|
"++resource++bika.lims.images/worksheet_big.png" |
|
48
|
|
|
) |
|
49
|
|
|
|
|
50
|
|
|
self.title = self.context.translate(_("Add Analyses")) |
|
51
|
|
|
self.context_actions = {} |
|
52
|
|
|
|
|
53
|
|
|
# initial review state for first form display of the worksheet |
|
54
|
|
|
# add_analyses search view - first batch of analyses, latest first. |
|
55
|
|
|
self.show_sort_column = False |
|
56
|
|
|
self.show_select_row = False |
|
57
|
|
|
self.show_select_column = True |
|
58
|
|
|
self.pagesize = 50 |
|
59
|
|
|
|
|
60
|
|
|
self.columns = collections.OrderedDict(( |
|
61
|
|
|
("Priority", { |
|
62
|
|
|
"title": "", |
|
63
|
|
|
"sortable": True, |
|
64
|
|
|
"index": "getPrioritySortkey"}), |
|
65
|
|
|
("Client", { |
|
66
|
|
|
"title": _("Client"), |
|
67
|
|
|
"attr": "getClientTitle", |
|
68
|
|
|
"replace_url": "getClientURL", |
|
69
|
|
|
"index": "getClientTitle"}), |
|
70
|
|
|
("getClientOrderNumber", { |
|
71
|
|
|
"title": _("Order"), |
|
72
|
|
|
"toggle": False, |
|
73
|
|
|
"index": "getClientOrderNumber"}), |
|
74
|
|
|
("getRequestID", { |
|
75
|
|
|
"title": _("Request ID"), |
|
76
|
|
|
"attr": "getRequestID", |
|
77
|
|
|
"replace_url": "getRequestURL", |
|
78
|
|
|
"index": "getRequestID"}), |
|
79
|
|
|
("getCategoryTitle", { |
|
80
|
|
|
"title": _("Category"), |
|
81
|
|
|
"attr": "getCategoryTitle"}), |
|
82
|
|
|
("Title", { |
|
83
|
|
|
"title": _("Analysis"), |
|
84
|
|
|
"index": "getId"}), |
|
85
|
|
|
("getDateReceived", { |
|
86
|
|
|
"title": _("Date Received"), |
|
87
|
|
|
"index": "getDateReceived"}), |
|
88
|
|
|
("getDueDate", { |
|
89
|
|
|
"title": _("Due Date"), |
|
90
|
|
|
"index": "getDueDate"}), |
|
91
|
|
|
)) |
|
92
|
|
|
|
|
93
|
|
|
self.review_states = [ |
|
94
|
|
|
{ |
|
95
|
|
|
"id": "default", |
|
96
|
|
|
"title": _("All"), |
|
97
|
|
|
"contentFilter": {}, |
|
98
|
|
|
"transitions": [{"id": "assign"}, ], |
|
99
|
|
|
"columns": self.columns.keys(), |
|
100
|
|
|
}, |
|
101
|
|
|
] |
|
102
|
|
|
|
|
103
|
|
|
def __call__(self): |
|
104
|
|
|
super(AddAnalysesView, self).__call__() |
|
105
|
|
|
|
|
106
|
|
|
# TODO: Refactor Worfklow |
|
107
|
|
|
grant = self.is_edit_allowed() and self.is_manage_allowed() |
|
108
|
|
|
if not grant: |
|
109
|
|
|
redirect_url = api.get_url(self.context) |
|
110
|
|
|
return self.request.response.redirect(redirect_url) |
|
111
|
|
|
|
|
112
|
|
|
# TODO: Refactor this function call |
|
113
|
|
|
showRejectionMessage(self.context) |
|
114
|
|
|
|
|
115
|
|
|
# Handle form submission |
|
116
|
|
|
if self.request.form.get("submitted"): |
|
117
|
|
|
CheckAuthenticator(self.request) |
|
118
|
|
|
success = self.handle_submit() |
|
119
|
|
|
if success: |
|
120
|
|
|
self.add_status_message(_("Changes saved.")) |
|
121
|
|
|
redirect_url = "{}/{}".format( |
|
122
|
|
|
api.get_url(self.context), "manage_results") |
|
123
|
|
|
self.request.response.redirect(redirect_url) |
|
124
|
|
|
else: |
|
125
|
|
|
self.add_status_message( |
|
126
|
|
|
_("No analyses were added to this worksheet."), |
|
127
|
|
|
level="warning") |
|
128
|
|
|
return self.template() |
|
129
|
|
|
|
|
130
|
|
|
# handle subpath calls |
|
131
|
|
|
if len(self.traverse_subpath) > 0: |
|
132
|
|
|
return self.handle_subpath() |
|
133
|
|
|
|
|
134
|
|
|
return self.template() |
|
135
|
|
|
|
|
136
|
|
|
def update(self): |
|
137
|
|
|
"""Update hook |
|
138
|
|
|
""" |
|
139
|
|
|
super(AddAnalysesView, self).update() |
|
140
|
|
|
|
|
141
|
|
|
def handle_submit(self): |
|
142
|
|
|
"""Handle form submission |
|
143
|
|
|
""" |
|
144
|
|
|
wst_uid = self.request.form.get("getWorksheetTemplate") |
|
145
|
|
|
if not wst_uid: |
|
146
|
|
|
return False |
|
147
|
|
|
|
|
148
|
|
|
layout = self.context.getLayout() |
|
149
|
|
|
wst = api.get_object_by_uid(wst_uid) |
|
150
|
|
|
|
|
151
|
|
|
self.request["context_uid"] = api.get_uid(self.context) |
|
152
|
|
|
self.context.applyWorksheetTemplate(wst) |
|
153
|
|
|
|
|
154
|
|
|
if len(self.context.getLayout()) == len(layout): |
|
155
|
|
|
return False |
|
156
|
|
|
return True |
|
157
|
|
|
|
|
158
|
|
|
@property |
|
159
|
|
|
def worksheet_template_setup_url(self): |
|
160
|
|
|
"""Returns the Worksheet Template Setup URL |
|
161
|
|
|
""" |
|
162
|
|
|
setup = api.get_setup() |
|
163
|
|
|
return "{}/{}".format(api.get_url(setup), "bika_worksheettemplates") |
|
164
|
|
|
|
|
165
|
|
|
@view.memoize |
|
166
|
|
|
def is_edit_allowed(self): |
|
167
|
|
|
"""Check if edit is allowed |
|
168
|
|
|
""" |
|
169
|
|
|
checkPermission = self.context.portal_membership.checkPermission |
|
170
|
|
|
return checkPermission(EditWorksheet, self.context) |
|
171
|
|
|
|
|
172
|
|
|
@view.memoize |
|
173
|
|
|
def is_manage_allowed(self): |
|
174
|
|
|
"""Check if manage is allowed |
|
175
|
|
|
""" |
|
176
|
|
|
checkPermission = self.context.portal_membership.checkPermission |
|
177
|
|
|
return checkPermission(ManageWorksheets, self.context) |
|
178
|
|
|
|
|
179
|
|
|
def add_status_message(self, message, level="info"): |
|
180
|
|
|
"""Set a portal status message |
|
181
|
|
|
""" |
|
182
|
|
|
return self.context.plone_utils.addPortalMessage(message, level) |
|
183
|
|
|
|
|
184
|
|
|
def folderitems(self): |
|
185
|
|
|
"""Return folderitems as brains |
|
186
|
|
|
""" |
|
187
|
|
|
items = super(AddAnalysesView, self).folderitems(classic=False) |
|
188
|
|
|
return items |
|
189
|
|
|
|
|
190
|
|
|
def folderitem(self, obj, item, index): |
|
191
|
|
|
"""Service triggered each time an item is iterated in folderitems. |
|
192
|
|
|
|
|
193
|
|
|
The use of this service prevents the extra-loops in child objects. |
|
194
|
|
|
|
|
195
|
|
|
:obj: the instance of the class to be foldered |
|
196
|
|
|
:item: dict containing the properties of the object to be used by |
|
197
|
|
|
the template |
|
198
|
|
|
:index: current index of the item |
|
199
|
|
|
""" |
|
200
|
|
|
DueDate = obj.getDueDate |
|
201
|
|
|
|
|
202
|
|
|
item["getDateReceived"] = self.ulocalized_time(obj.getDateReceived) |
|
203
|
|
|
item["getDueDate"] = self.ulocalized_time(DueDate) |
|
204
|
|
|
|
|
205
|
|
|
if DueDate and DueDate < DateTime(): |
|
206
|
|
|
item["after"]["DueDate"] = get_image( |
|
207
|
|
|
"late.png", title=t(_("Late Analysis"))) |
|
208
|
|
|
|
|
209
|
|
|
# Add Priority column |
|
210
|
|
|
priority_sort_key = obj.getPrioritySortkey |
|
211
|
|
|
if not priority_sort_key: |
|
212
|
|
|
# Default priority is Medium = 3. |
|
213
|
|
|
# The format of PrioritySortKey is <priority>.<created> |
|
214
|
|
|
priority_sort_key = "3.%s" % obj.created.ISO8601() |
|
215
|
|
|
|
|
216
|
|
|
priority = priority_sort_key.split(".")[0] |
|
217
|
|
|
priority_text = t(PRIORITIES.getValue(priority)) |
|
218
|
|
|
html = "<div title='{}' class='priority-ico priority-{}'><div>" |
|
219
|
|
|
item["replace"]["Priority"] = html.format(priority_text, priority) |
|
220
|
|
|
|
|
221
|
|
|
return item |
|
222
|
|
|
|
|
223
|
|
|
def getWorksheetTemplates(self): |
|
224
|
|
|
"""Return WS Templates |
|
225
|
|
|
""" |
|
226
|
|
|
vocabulary = CatalogVocabulary(self) |
|
227
|
|
|
vocabulary.catalog = "bika_setup_catalog" |
|
228
|
|
|
return vocabulary( |
|
229
|
|
|
portal_type="WorksheetTemplate", sort_on="sortable_title") |
|
230
|
|
|
|