|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
import collections |
|
4
|
|
|
|
|
5
|
|
|
from bika.lims import api |
|
6
|
|
|
from bika.lims import bikaMessageFactory as _ |
|
7
|
|
|
from bika.lims.api.security import check_permission |
|
8
|
|
|
from bika.lims.utils import format_supsub |
|
9
|
|
|
from bika.lims.utils import get_image |
|
10
|
|
|
from bika.lims.utils import get_link |
|
11
|
|
|
from plone.memoize import view |
|
12
|
|
|
from senaite.core.catalog import SETUP_CATALOG |
|
13
|
|
|
from senaite.core.permissions import FieldEditProfiles |
|
14
|
|
|
from senaite.core.z3cform.widgets.listing.view import DefaultListingWidget |
|
15
|
|
|
from zope.i18n.locales import locales |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class AnalysisProfilesWidget(DefaultListingWidget): |
|
19
|
|
|
"""Listing widget for Analysis Profiles |
|
20
|
|
|
""" |
|
21
|
|
|
def __init__(self, field, request): |
|
22
|
|
|
super(AnalysisProfilesWidget, self).__init__(field, request) |
|
23
|
|
|
|
|
24
|
|
|
self.catalog = SETUP_CATALOG |
|
25
|
|
|
self.contentFilter = { |
|
26
|
|
|
"portal_type": "AnalysisService", |
|
27
|
|
|
"sort_on": "sortable_title", |
|
28
|
|
|
"sort_order": "ascending", |
|
29
|
|
|
"is_active": True, |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
# group the current records by UID |
|
33
|
|
|
self.records = {} |
|
34
|
|
|
for record in self.get_value(): |
|
35
|
|
|
uid = record.get("uid") |
|
36
|
|
|
self.records[uid] = record |
|
37
|
|
|
|
|
38
|
|
|
# listing config |
|
39
|
|
|
self.allow_edit = True |
|
40
|
|
|
self.context_actions = {} |
|
41
|
|
|
self.fetch_transitions_on_select = False |
|
42
|
|
|
self.omit_form = True |
|
43
|
|
|
self.pagesize = 999999 |
|
44
|
|
|
self.show_column_toggles = False |
|
45
|
|
|
self.show_search = True |
|
46
|
|
|
self.show_select_all_checkbox = False |
|
47
|
|
|
self.show_select_column = True |
|
48
|
|
|
self.show_table_footer = False |
|
49
|
|
|
|
|
50
|
|
|
# Categories |
|
51
|
|
|
if self.show_categories_enabled(): |
|
52
|
|
|
self.categories = [] |
|
53
|
|
|
self.show_categories = True |
|
54
|
|
|
self.expand_all_categories = False |
|
55
|
|
|
|
|
56
|
|
|
self.columns = collections.OrderedDict(( |
|
57
|
|
|
("Title", { |
|
58
|
|
|
"title": _( |
|
59
|
|
|
u"listing_analysisprofileswidget_column_title", |
|
60
|
|
|
default=u"Service" |
|
61
|
|
|
), |
|
62
|
|
|
"index": "sortable_title", |
|
63
|
|
|
"sortable": False |
|
64
|
|
|
}), |
|
65
|
|
|
("Keyword", { |
|
66
|
|
|
"title": _( |
|
67
|
|
|
u"listing_analysisprofileswidget_column_keyword", |
|
68
|
|
|
default=u"Keyword" |
|
69
|
|
|
), |
|
70
|
|
|
"sortable": False |
|
71
|
|
|
}), |
|
72
|
|
|
("Methods", { |
|
73
|
|
|
"title": _( |
|
74
|
|
|
u"listing_analysisprofileswidget_column_methods", |
|
75
|
|
|
default=u"Methods" |
|
76
|
|
|
), |
|
77
|
|
|
"sortable": False |
|
78
|
|
|
}), |
|
79
|
|
|
("Unit", { |
|
80
|
|
|
"title": _( |
|
81
|
|
|
u"listing_analysisprofileswidget_column_unit", |
|
82
|
|
|
default=u"Unit" |
|
83
|
|
|
), |
|
84
|
|
|
"sortable": False |
|
85
|
|
|
}), |
|
86
|
|
|
("Price", { |
|
87
|
|
|
"title": _( |
|
88
|
|
|
u"listing_analysisprofileswidget_column_price", |
|
89
|
|
|
default=u"Price" |
|
90
|
|
|
), |
|
91
|
|
|
"sortable": False, |
|
92
|
|
|
}), |
|
93
|
|
|
("Hidden", { |
|
94
|
|
|
"title": _( |
|
95
|
|
|
u"listing_analysisprofileswidget_column_hidden", |
|
96
|
|
|
default=u"Hidden" |
|
97
|
|
|
), |
|
98
|
|
|
"sortable": False, |
|
99
|
|
|
}), |
|
100
|
|
|
)) |
|
101
|
|
|
|
|
102
|
|
|
cols = self.columns.keys() |
|
103
|
|
|
if not self.show_prices(): |
|
104
|
|
|
cols.remove("Price") |
|
105
|
|
|
|
|
106
|
|
|
self.review_states = [ |
|
107
|
|
|
{ |
|
108
|
|
|
"id": "default", |
|
109
|
|
|
"title": _( |
|
110
|
|
|
u"listing_analysisprofileswidget_state_all", |
|
111
|
|
|
default=u"All" |
|
112
|
|
|
), |
|
113
|
|
|
"contentFilter": {}, |
|
114
|
|
|
"transitions": [{"id": "disallow-all-possible-transitions"}], |
|
115
|
|
|
"columns": cols, |
|
116
|
|
|
}, |
|
117
|
|
|
] |
|
118
|
|
|
|
|
119
|
|
|
@view.memoize |
|
120
|
|
|
def show_categories_enabled(self): |
|
121
|
|
|
"""Check in the setup if categories are enabled |
|
122
|
|
|
""" |
|
123
|
|
|
return self.context.bika_setup.getCategoriseAnalysisServices() |
|
124
|
|
|
|
|
125
|
|
|
@view.memoize |
|
126
|
|
|
def show_prices(self): |
|
127
|
|
|
"""Checks if prices should be shown or not |
|
128
|
|
|
""" |
|
129
|
|
|
setup = api.get_setup() |
|
130
|
|
|
return setup.getShowPrices() |
|
131
|
|
|
|
|
132
|
|
|
@view.memoize |
|
133
|
|
|
def get_currency_symbol(self): |
|
134
|
|
|
"""Get the currency Symbol |
|
135
|
|
|
""" |
|
136
|
|
|
locale = locales.getLocale("en") |
|
137
|
|
|
setup = api.get_setup() |
|
138
|
|
|
currency = setup.getCurrency() |
|
139
|
|
|
return locale.numbers.currencies[currency].symbol |
|
140
|
|
|
|
|
141
|
|
|
@view.memoize |
|
142
|
|
|
def get_decimal_mark(self): |
|
143
|
|
|
"""Returns the decimal mark |
|
144
|
|
|
""" |
|
145
|
|
|
setup = api.get_setup() |
|
146
|
|
|
return setup.getDecimalMark() |
|
147
|
|
|
|
|
148
|
|
|
@view.memoize |
|
149
|
|
|
def format_price(self, price): |
|
150
|
|
|
"""Formats the price with the set decimal mark and correct currency |
|
151
|
|
|
""" |
|
152
|
|
|
return u"{} {}{}{:02d}".format( |
|
153
|
|
|
self.get_currency_symbol(), |
|
154
|
|
|
price[0], |
|
155
|
|
|
self.get_decimal_mark(), |
|
156
|
|
|
price[1], |
|
157
|
|
|
) |
|
158
|
|
|
|
|
159
|
|
|
@view.memoize |
|
160
|
|
|
def is_edit_allowed(self): |
|
161
|
|
|
"""Check if edit is allowed |
|
162
|
|
|
""" |
|
163
|
|
|
return check_permission(FieldEditProfiles, self.context) |
|
164
|
|
|
|
|
165
|
|
|
@view.memoize |
|
166
|
|
|
def get_editable_columns(self): |
|
167
|
|
|
"""Return editable fields |
|
168
|
|
|
""" |
|
169
|
|
|
columns = [] |
|
170
|
|
|
if self.is_edit_allowed(): |
|
171
|
|
|
columns = ["Hidden"] |
|
172
|
|
|
return columns |
|
173
|
|
|
|
|
174
|
|
|
def extract(self): |
|
175
|
|
|
"""Extract the value from the request for the field |
|
176
|
|
|
""" |
|
177
|
|
|
form = self.request.form |
|
178
|
|
|
selected = form.get(self.select_checkbox_name, []) |
|
179
|
|
|
|
|
180
|
|
|
if not selected: |
|
181
|
|
|
return [] |
|
182
|
|
|
|
|
183
|
|
|
# extract the data from the form for the field |
|
184
|
|
|
records = [] |
|
185
|
|
|
hidden_services = form.get("Hidden", {}) |
|
186
|
|
|
for uid in selected: |
|
187
|
|
|
records.append({ |
|
188
|
|
|
"uid": uid, |
|
189
|
|
|
"hidden": hidden_services.get(uid) == "on", |
|
190
|
|
|
}) |
|
191
|
|
|
|
|
192
|
|
|
return records |
|
193
|
|
|
|
|
194
|
|
|
def folderitems(self): |
|
195
|
|
|
items = super(AnalysisProfilesWidget, self).folderitems() |
|
196
|
|
|
self.categories.sort() |
|
197
|
|
|
return items |
|
198
|
|
|
|
|
199
|
|
|
def folderitem(self, obj, item, index): |
|
200
|
|
|
"""Service triggered each time an item is iterated in folderitems. |
|
201
|
|
|
|
|
202
|
|
|
The use of this service prevents the extra-loops in child objects. |
|
203
|
|
|
|
|
204
|
|
|
:obj: the instance of the class to be foldered |
|
205
|
|
|
:item: dict containing the properties of the object to be used by |
|
206
|
|
|
the template |
|
207
|
|
|
:index: current index of the item |
|
208
|
|
|
""" |
|
209
|
|
|
item = super(AnalysisProfilesWidget, self).folderitem(obj, item, index) |
|
210
|
|
|
|
|
211
|
|
|
# ensure we have an object and not a brain |
|
212
|
|
|
obj = api.get_object(obj) |
|
213
|
|
|
uid = api.get_uid(obj) |
|
214
|
|
|
url = api.get_url(obj) |
|
215
|
|
|
title = api.get_title(obj) |
|
216
|
|
|
|
|
217
|
|
|
# get the category |
|
218
|
|
|
if self.show_categories_enabled(): |
|
219
|
|
|
category = obj.getCategoryTitle() |
|
220
|
|
|
if category not in self.categories: |
|
221
|
|
|
self.categories.append(category) |
|
222
|
|
|
item["category"] = category |
|
223
|
|
|
|
|
224
|
|
|
hidden = False |
|
225
|
|
|
# get the hidden setting from the records |
|
226
|
|
|
if self.records.get(uid): |
|
227
|
|
|
record = self.records.get(uid, {}) or {} |
|
228
|
|
|
hidden = record.get("hidden", False) |
|
229
|
|
|
else: |
|
230
|
|
|
# get the default value from the service |
|
231
|
|
|
hidden = obj.getHidden() |
|
232
|
|
|
|
|
233
|
|
|
item["replace"]["Title"] = get_link(url, value=title) |
|
234
|
|
|
item["Price"] = self.format_price(obj.Price) |
|
235
|
|
|
item["allow_edit"] = self.get_editable_columns() |
|
236
|
|
|
item["selected"] = False |
|
237
|
|
|
item["Hidden"] = hidden |
|
238
|
|
|
item["replace"]["Hidden"] = _("Yes") if hidden else _("No") |
|
239
|
|
|
item["selected"] = uid in self.records |
|
240
|
|
|
item["Keyword"] = obj.getKeyword() |
|
241
|
|
|
|
|
242
|
|
|
# Add methods |
|
243
|
|
|
methods = obj.getMethods() |
|
244
|
|
|
if methods: |
|
245
|
|
|
links = map( |
|
246
|
|
|
lambda m: get_link( |
|
247
|
|
|
m.absolute_url(), value=m.Title(), css_class="link"), |
|
248
|
|
|
methods) |
|
249
|
|
|
item["replace"]["Methods"] = ", ".join(links) |
|
250
|
|
|
else: |
|
251
|
|
|
item["methods"] = "" |
|
252
|
|
|
|
|
253
|
|
|
# Unit |
|
254
|
|
|
unit = obj.getUnit() |
|
255
|
|
|
item["Unit"] = unit or "" |
|
256
|
|
|
item["replace"]["Unit"] = unit and format_supsub(unit) or "" |
|
257
|
|
|
|
|
258
|
|
|
# Icons |
|
259
|
|
|
after_icons = "" |
|
260
|
|
|
if obj.getAccredited(): |
|
261
|
|
|
after_icons += get_image( |
|
262
|
|
|
"accredited.png", title=_("Accredited")) |
|
263
|
|
|
if obj.getAttachmentRequired(): |
|
264
|
|
|
after_icons += get_image( |
|
265
|
|
|
"attach_reqd.png", title=_("Attachment required")) |
|
266
|
|
|
if after_icons: |
|
267
|
|
|
item["after"]["Title"] = after_icons |
|
268
|
|
|
|
|
269
|
|
|
return item |
|
270
|
|
|
|