1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
import collections |
4
|
|
|
from string import Template as T |
5
|
|
|
|
6
|
|
|
from bika.lims import api |
7
|
|
|
from bika.lims import bikaMessageFactory as _ |
8
|
|
|
from plone.memoize import view |
9
|
|
|
from senaite.core.interfaces import ISampleTemplate |
10
|
|
|
|
11
|
|
|
from .services_widget import ServicesWidget |
12
|
|
|
|
13
|
|
|
PART_TPL = T("""<span class='badge badge-info'> |
14
|
|
|
$part_id |
15
|
|
|
</span> |
16
|
|
|
""") |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class SampleTemplateServicesWidget(ServicesWidget): |
20
|
|
|
"""Listing widget for Sample Template Services |
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
def __init__(self, field, request): |
24
|
|
|
super(SampleTemplateServicesWidget, self).__init__(field, request) |
25
|
|
|
|
26
|
|
|
def update(self): |
27
|
|
|
super(SampleTemplateServicesWidget, self).update() |
28
|
|
|
|
29
|
|
|
length = len(self.columns) |
30
|
|
|
items = list(self.columns.items()) |
31
|
|
|
items.insert(length - 1, ( |
32
|
|
|
"Partition", { |
33
|
|
|
"title": _( |
34
|
|
|
u"listing_services_column_partition", |
35
|
|
|
default=u"Partition" |
36
|
|
|
), |
37
|
|
|
"sortable": False |
38
|
|
|
})) |
39
|
|
|
self.columns = collections.OrderedDict(items) |
40
|
|
|
self.review_states[0]["columns"] = self.columns.keys() |
41
|
|
|
|
42
|
|
|
@view.memoize |
43
|
|
|
def get_editable_columns(self): |
44
|
|
|
"""Return editable fields |
45
|
|
|
""" |
46
|
|
|
columns = [] |
47
|
|
|
if self.is_edit_allowed(): |
48
|
|
|
columns = ["Partition", "Hidden"] |
49
|
|
|
return columns |
50
|
|
|
|
51
|
|
|
@view.memoize |
52
|
|
|
def get_partitions(self): |
53
|
|
|
"""Return the current stored partitions |
54
|
|
|
""" |
55
|
|
|
# No context |
56
|
|
|
if not ISampleTemplate.providedBy(self.context): |
57
|
|
|
return [] |
58
|
|
|
return self.context.getPartitions() |
59
|
|
|
|
60
|
|
|
@view.memoize |
61
|
|
|
def get_partition_choices(self): |
62
|
|
|
# default empty choice |
63
|
|
|
partition_choices = [{"ResultValue": "", "ResultText": ""}] |
64
|
|
|
# extract the partition settings from the context |
65
|
|
|
for num, part in enumerate(self.get_partitions()): |
66
|
|
|
part_id = part.get("part_id") |
67
|
|
|
partition_choices.append({ |
68
|
|
|
"ResultValue": part_id, |
69
|
|
|
"ResultText": part_id, |
70
|
|
|
}) |
71
|
|
|
return partition_choices |
72
|
|
|
|
73
|
|
|
def extract(self): |
74
|
|
|
"""Extract the value from the request for the field |
75
|
|
|
""" |
76
|
|
|
form = self.request.form |
77
|
|
|
selected = form.get(self.select_checkbox_name, []) |
78
|
|
|
|
79
|
|
|
if not selected: |
80
|
|
|
return [] |
81
|
|
|
|
82
|
|
|
# get the selected partition mapping |
83
|
|
|
partition_mapping = {} |
84
|
|
|
# Note: Partition comes in as a list with one dict from the form |
85
|
|
|
map(lambda m: partition_mapping.update(m), form.get("Partition", [])) |
|
|
|
|
86
|
|
|
|
87
|
|
|
# extract the data from the form for the field |
88
|
|
|
records = [] |
89
|
|
|
hidden_services = form.get("Hidden", {}) |
90
|
|
|
|
91
|
|
|
for uid in selected: |
92
|
|
|
records.append({ |
93
|
|
|
"uid": uid, |
94
|
|
|
"hidden": hidden_services.get(uid) == "on", |
95
|
|
|
"part_id": api.safe_unicode(partition_mapping.get(uid, u"")), |
96
|
|
|
}) |
97
|
|
|
|
98
|
|
|
return records |
99
|
|
|
|
100
|
|
|
def folderitem(self, obj, item, index): |
101
|
|
|
"""Service triggered each time an item is iterated in folderitems. |
102
|
|
|
|
103
|
|
|
:obj: the instance of the class to be foldered |
104
|
|
|
:item: dict containing the properties of the object to be used by |
105
|
|
|
the template |
106
|
|
|
:index: current index of the item |
107
|
|
|
""" |
108
|
|
|
item = super(SampleTemplateServicesWidget, self).folderitem( |
109
|
|
|
obj, item, index) |
110
|
|
|
obj = api.get_object(obj) |
111
|
|
|
uid = api.get_uid(obj) |
112
|
|
|
record = self.records.get(uid, {}) or {} |
113
|
|
|
|
114
|
|
|
# get the partition setting |
115
|
|
|
part_id = None |
116
|
|
|
if record: |
117
|
|
|
part_id = record.get("part_id", "") |
118
|
|
|
|
119
|
|
|
item["allow_edit"] = self.get_editable_columns() |
120
|
|
|
item["Partition"] = part_id |
121
|
|
|
if part_id: |
122
|
|
|
item["replace"]["Partition"] = PART_TPL.substitute(part_id=part_id) |
123
|
|
|
item["choices"]["Partition"] = self.get_partition_choices() |
124
|
|
|
|
125
|
|
|
return item |
126
|
|
|
|