|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# This file is part of SENAITE.HEALTH. |
|
4
|
|
|
# |
|
5
|
|
|
# SENAITE.HEALTH is free software: you can redistribute it and/or modify it |
|
6
|
|
|
# under the terms of the GNU General Public License as published by the Free |
|
7
|
|
|
# Software Foundation, version 2. |
|
8
|
|
|
# |
|
9
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
|
10
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
11
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
12
|
|
|
# details. |
|
13
|
|
|
# |
|
14
|
|
|
# You should have received a copy of the GNU General Public License along with |
|
15
|
|
|
# this program; if not, write to the Free Software Foundation, Inc., 51 |
|
16
|
|
|
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17
|
|
|
# |
|
18
|
|
|
# Copyright 2018-2019 by it's authors. |
|
19
|
|
|
# Some rights reserved, see README and LICENSE. |
|
20
|
|
|
|
|
21
|
|
|
from AccessControl import ClassSecurityInfo |
|
22
|
|
|
from Products.Archetypes.Registry import registerWidget |
|
23
|
|
|
from Products.CMFCore.utils import getToolByName |
|
24
|
|
|
from Products.validation import validation |
|
25
|
|
|
from Products.validation.interfaces.IValidator import IValidator |
|
26
|
|
|
from zope.interface import implements |
|
27
|
|
|
|
|
28
|
|
|
from bika.health import api |
|
29
|
|
|
from bika.health import bikaMessageFactory as _ |
|
30
|
|
|
from bika.lims.browser.widgets.analysisspecificationwidget import \ |
|
31
|
|
|
AnalysisSpecificationView as BaseView, \ |
|
32
|
|
|
AnalysisSpecificationWidget as BaseWidget |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
class AnalysisSpecificationView(BaseView): |
|
36
|
|
|
|
|
37
|
|
|
def __init__(self, context, request): |
|
38
|
|
|
super(AnalysisSpecificationView, self).__init__(context, request) |
|
39
|
|
|
|
|
40
|
|
|
self.columns['minpanic'] = {'title': _('Min panic'), 'sortable': False} |
|
41
|
|
|
self.columns['maxpanic'] = {'title': _('Max panic'), 'sortable': False} |
|
42
|
|
|
self.review_states[0]['columns'] += ['minpanic', 'maxpanic'] |
|
43
|
|
|
|
|
44
|
|
|
def folderitem(self, obj, item, index): |
|
45
|
|
|
item = super(AnalysisSpecificationView, self).folderitem(obj, item, index) |
|
46
|
|
|
obj = api.get_object(obj) |
|
47
|
|
|
keyword = obj.getKeyword() |
|
48
|
|
|
spec = self.specification.get(keyword, {}) |
|
49
|
|
|
item['minpanic'] = spec.get("minpanic", "") |
|
50
|
|
|
item['maxpanic'] = spec.get("maxpanic", "") |
|
51
|
|
|
return item |
|
52
|
|
|
|
|
53
|
|
|
def get_editable_columns(self): |
|
54
|
|
|
"""Return editable fields |
|
55
|
|
|
""" |
|
56
|
|
|
cols = super(AnalysisSpecificationView, self).get_editable_columns() |
|
57
|
|
|
cols.extend(["minpanic", "maxpanic"]) |
|
58
|
|
|
return cols |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
class AnalysisSpecificationWidget(BaseWidget): |
|
62
|
|
|
_properties = BaseWidget._properties.copy() |
|
63
|
|
|
|
|
64
|
|
|
security = ClassSecurityInfo() |
|
65
|
|
|
|
|
66
|
|
|
security.declarePublic('process_form') |
|
67
|
|
|
|
|
68
|
|
|
def process_form(self, instance, field, form, empty_marker=None, |
|
69
|
|
|
emptyReturnsMarker=False): |
|
70
|
|
|
"""Return a list of dictionaries fir for AnalysisSpecsResultsField |
|
71
|
|
|
consumption. |
|
72
|
|
|
""" |
|
73
|
|
|
values = BaseWidget.process_form(self, instance, field, form, |
|
74
|
|
|
empty_marker, emptyReturnsMarker) |
|
75
|
|
|
for value in values[0]: |
|
76
|
|
|
uid = value["uid"] |
|
77
|
|
|
value["minpanic"] = self._get_spec_value(form, uid, "minpanic") |
|
78
|
|
|
value["maxpanic"] = self._get_spec_value(form, uid, "maxpanic") |
|
79
|
|
|
return values[0], {} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
registerWidget(AnalysisSpecificationWidget, |
|
83
|
|
|
title='Analysis Specification Results', |
|
84
|
|
|
description=('Analysis Specification Results')) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
class AnalysisSpecificationPanicValidator(object): |
|
88
|
|
|
implements(IValidator) |
|
89
|
|
|
name = "analysisspecs_panic_validator" |
|
90
|
|
|
|
|
91
|
|
|
def __call__(self, value, *args, **kwargs): |
|
92
|
|
|
instance = kwargs['instance'] |
|
93
|
|
|
ts = getToolByName(instance, 'translation_service').translate |
|
94
|
|
|
|
|
95
|
|
|
if instance.REQUEST.get('validated', '') == self.name: |
|
96
|
|
|
return True |
|
97
|
|
|
else: |
|
98
|
|
|
instance.REQUEST['validated'] = self.name |
|
99
|
|
|
pmins = instance.REQUEST.get('minpanic', {}) |
|
100
|
|
|
if len(pmins) > 0: |
|
101
|
|
|
pmins = pmins[0] |
|
102
|
|
|
pmaxs = instance.REQUEST.get('maxpanic', {}) |
|
103
|
|
|
if len(pmaxs) > 0: |
|
104
|
|
|
pmaxs = pmaxs[0] |
|
105
|
|
|
uids = pmins.keys() |
|
106
|
|
|
for uid in uids: |
|
107
|
|
|
pmin = pmins.get(uid, '') == '' and '0' or pmins[uid] |
|
108
|
|
|
pmax = pmaxs.get(uid, '') == '' and '0' or pmaxs[uid] |
|
109
|
|
|
|
|
110
|
|
|
# Values must be numbers |
|
111
|
|
|
try: |
|
112
|
|
|
pmin = float(pmin) |
|
113
|
|
|
except: |
|
114
|
|
|
return ts(_("Validation failed: Panic min value must be " |
|
115
|
|
|
"numeric")) |
|
116
|
|
|
|
|
117
|
|
|
try: |
|
118
|
|
|
pmax = float(pmax) |
|
119
|
|
|
except: |
|
120
|
|
|
return ts(_("Validation failed: Panic min value must be " |
|
121
|
|
|
"numeric")) |
|
122
|
|
|
|
|
123
|
|
|
if pmin > pmax: |
|
124
|
|
|
return ts(_("Validation failed: Panic max value must be " |
|
125
|
|
|
"greater than panic min value")) |
|
126
|
|
|
return True |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
validation.register(AnalysisSpecificationPanicValidator()) |
|
130
|
|
|
|