|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
import collections |
|
4
|
|
|
|
|
5
|
|
|
import six |
|
6
|
|
|
|
|
7
|
|
|
from bika.lims import api |
|
8
|
|
|
from bika.lims.browser import BrowserView |
|
9
|
|
|
from bika.lims.interfaces import IAnalysisRequest |
|
10
|
|
|
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
|
11
|
|
|
from senaite.core import logger |
|
12
|
|
|
|
|
13
|
|
|
OFF_VALUES = ["0", "off", "no"] |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class MultiResultsView(BrowserView): |
|
17
|
|
|
"""Allows to edit results of multiple samples |
|
18
|
|
|
""" |
|
19
|
|
|
template = ViewPageTemplateFile("templates/multi_results.pt") |
|
20
|
|
|
|
|
21
|
|
|
def __init__(self, context, request): |
|
22
|
|
|
super(MultiResultsView, self).__init__(context, request) |
|
23
|
|
|
self.context = context |
|
24
|
|
|
self.request = request |
|
25
|
|
|
self.portal = api.get_portal() |
|
26
|
|
|
|
|
27
|
|
|
def __call__(self): |
|
28
|
|
|
return self.template() |
|
29
|
|
|
|
|
30
|
|
|
@property |
|
31
|
|
|
def context_state(self): |
|
32
|
|
|
return api.get_view( |
|
33
|
|
|
"plone_context_state", |
|
34
|
|
|
context=self.context, request=self.request) |
|
35
|
|
|
|
|
36
|
|
|
def contents_table(self, sample, poc): |
|
37
|
|
|
view_name = "table_{}_analyses".format(poc) |
|
38
|
|
|
view = api.get_view(view_name, context=sample, request=self.request) |
|
39
|
|
|
# Inject additional hidden field in the listing form for redirect |
|
40
|
|
|
# https://github.com/senaite/senaite.app.listing/pull/80 |
|
41
|
|
|
view.additional_hidden_fields = [{ |
|
42
|
|
|
"name": "redirect_url", |
|
43
|
|
|
"value": self.context_state.current_page_url(), |
|
44
|
|
|
}] |
|
45
|
|
|
view.update() |
|
46
|
|
|
view.before_render() |
|
47
|
|
|
return view.contents_table() |
|
48
|
|
|
|
|
49
|
|
|
def show_lab_analyses(self, sample): |
|
50
|
|
|
"""Show/Hide lab analyses |
|
51
|
|
|
""" |
|
52
|
|
|
analyses = sample.getAnalyses(getPointOfCapture="lab") |
|
53
|
|
|
if len(analyses) == 0: |
|
54
|
|
|
return False |
|
55
|
|
|
lab_analyses = self.request.get("lab_analyses") |
|
56
|
|
|
if lab_analyses in OFF_VALUES: |
|
57
|
|
|
return False |
|
58
|
|
|
return True |
|
59
|
|
|
|
|
60
|
|
|
def show_field_analyses(self, sample): |
|
61
|
|
|
"""Show/Hide field analyses |
|
62
|
|
|
""" |
|
63
|
|
|
analyses = sample.getAnalyses(getPointOfCapture="field") |
|
64
|
|
|
if len(analyses) == 0: |
|
65
|
|
|
return False |
|
66
|
|
|
field_analyses = self.request.get("field_analyses", True) |
|
67
|
|
|
if field_analyses in OFF_VALUES: |
|
68
|
|
|
return False |
|
69
|
|
|
return True |
|
70
|
|
|
|
|
71
|
|
|
def get_samples(self): |
|
72
|
|
|
"""Extract the samples from the request UIDs |
|
73
|
|
|
|
|
74
|
|
|
This might be either a samples container or a sample context |
|
75
|
|
|
""" |
|
76
|
|
|
|
|
77
|
|
|
# fetch objects from request |
|
78
|
|
|
objs = self.get_objects_from_request() |
|
79
|
|
|
|
|
80
|
|
|
samples = [] |
|
81
|
|
|
|
|
82
|
|
|
for obj in objs: |
|
83
|
|
|
# when coming from the samples listing |
|
84
|
|
|
if IAnalysisRequest.providedBy(obj): |
|
85
|
|
|
samples.append(obj) |
|
86
|
|
|
|
|
87
|
|
|
# when coming from the WF menu inside a sample |
|
88
|
|
|
if IAnalysisRequest.providedBy(self.context): |
|
89
|
|
|
samples.append(self.context) |
|
90
|
|
|
|
|
91
|
|
|
return self.uniquify_items(samples) |
|
92
|
|
|
|
|
93
|
|
|
def uniquify_items(self, items): |
|
94
|
|
|
"""Uniquify the items with sort order |
|
95
|
|
|
""" |
|
96
|
|
|
unique = [] |
|
97
|
|
|
for item in items: |
|
98
|
|
|
if item in unique: |
|
99
|
|
|
continue |
|
100
|
|
|
unique.append(item) |
|
101
|
|
|
return unique |
|
102
|
|
|
|
|
103
|
|
|
def get_objects_from_request(self): |
|
104
|
|
|
"""Returns a list of objects coming from the "uids" request parameter |
|
105
|
|
|
""" |
|
106
|
|
|
unique_uids = self.get_uids_from_request() |
|
107
|
|
|
return filter(None, map(self.get_object_by_uid, unique_uids)) |
|
108
|
|
|
|
|
109
|
|
|
def get_uids_from_request(self): |
|
110
|
|
|
"""Return a list of uids from the request |
|
111
|
|
|
""" |
|
112
|
|
|
uids = self.request.form.get("uids", "") |
|
113
|
|
|
if isinstance(uids, six.string_types): |
|
114
|
|
|
uids = uids.split(",") |
|
115
|
|
|
unique_uids = collections.OrderedDict().fromkeys(uids).keys() |
|
116
|
|
|
return filter(api.is_uid, unique_uids) |
|
117
|
|
|
|
|
118
|
|
|
def get_object_by_uid(self, uid): |
|
119
|
|
|
"""Get the object by UID |
|
120
|
|
|
""" |
|
121
|
|
|
logger.debug("get_object_by_uid::UID={}".format(uid)) |
|
122
|
|
|
obj = api.get_object_by_uid(uid, None) |
|
123
|
|
|
if obj is None: |
|
124
|
|
|
logger.warn("!! No object found for UID #{} !!") |
|
125
|
|
|
return obj |
|
126
|
|
|
|