|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# This file is part of SENAITE.CORE. |
|
4
|
|
|
# |
|
5
|
|
|
# SENAITE.CORE is free software: you can redistribute it and/or modify it under |
|
6
|
|
|
# the terms of the GNU General Public License as published by the Free Software |
|
7
|
|
|
# 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-2022 by it's authors. |
|
19
|
|
|
# Some rights reserved, see README and LICENSE. |
|
20
|
|
|
|
|
21
|
|
|
import copy |
|
22
|
|
|
|
|
23
|
|
|
from bika.lims import api |
|
24
|
|
|
from bika.lims import FieldEditAnalysisConditions |
|
25
|
|
|
from bika.lims import senaiteMessageFactory as _ |
|
26
|
|
|
from bika.lims.api.security import check_permission |
|
27
|
|
|
from bika.lims.interfaces.analysis import IRequestAnalysis |
|
28
|
|
|
from Products.CMFPlone.utils import safe_unicode |
|
29
|
|
|
from Products.Five.browser import BrowserView |
|
30
|
|
|
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class SetAnalysisConditionsView(BrowserView): |
|
34
|
|
|
"""View for the update of analysis conditions |
|
35
|
|
|
""" |
|
36
|
|
|
template = ViewPageTemplateFile("templates/set_analysis_conditions.pt") |
|
37
|
|
|
|
|
38
|
|
|
def __call__(self): |
|
39
|
|
|
if self.request.form.get("submitted", False): |
|
40
|
|
|
return self.handle_submit() |
|
41
|
|
|
return self.template() |
|
42
|
|
|
|
|
43
|
|
|
def redirect(self, message=None, level="info"): |
|
44
|
|
|
"""Redirect with a message |
|
45
|
|
|
""" |
|
46
|
|
|
redirect_url = api.get_url(self.context) |
|
47
|
|
|
if message is not None: |
|
48
|
|
|
self.context.plone_utils.addPortalMessage(message, level) |
|
49
|
|
|
return self.request.response.redirect(redirect_url) |
|
50
|
|
|
|
|
51
|
|
|
def get_analysis(self): |
|
52
|
|
|
uid = self.get_uid_from_request() |
|
53
|
|
|
obj = api.get_object_by_uid(uid) |
|
54
|
|
|
if IRequestAnalysis.providedBy(obj): |
|
55
|
|
|
# Only analyses that implement IRequestAnalysis support conditions |
|
56
|
|
|
return obj |
|
57
|
|
|
return None |
|
58
|
|
|
|
|
59
|
|
|
def get_uid_from_request(self): |
|
60
|
|
|
"""Returns the uid from the request, if any |
|
61
|
|
|
""" |
|
62
|
|
|
uid = self.request.form.get("uid", self.request.get("uid")) |
|
63
|
|
|
if api.is_uid(uid): |
|
64
|
|
|
return uid |
|
65
|
|
|
return None |
|
66
|
|
|
|
|
67
|
|
|
def get_analysis_name(self): |
|
68
|
|
|
analysis = self.get_analysis() |
|
69
|
|
|
return api.get_title(analysis) |
|
70
|
|
|
|
|
71
|
|
|
def get_conditions(self): |
|
72
|
|
|
conditions = self.get_analysis().getConditions() |
|
73
|
|
|
conditions = copy.deepcopy(conditions) |
|
74
|
|
|
for condition in conditions: |
|
75
|
|
|
choices = condition.get("choices", "") |
|
76
|
|
|
options = filter(None, choices.split("|")) |
|
77
|
|
|
condition.update({"options": options}) |
|
78
|
|
|
return conditions |
|
79
|
|
|
|
|
80
|
|
|
def handle_submit(self): |
|
81
|
|
|
analysis = self.get_analysis() |
|
82
|
|
|
title = safe_unicode(api.get_title(analysis)) |
|
83
|
|
|
if not check_permission(FieldEditAnalysisConditions, analysis): |
|
84
|
|
|
message = _("Not allowed to update conditions: {}").format(title) |
|
85
|
|
|
return self.redirect(message=message, level="error") |
|
86
|
|
|
|
|
87
|
|
|
# Update the conditions |
|
88
|
|
|
conditions = self.request.form.get("conditions", []) |
|
89
|
|
|
analysis.setConditions(conditions) |
|
90
|
|
|
message = _("Analysis conditions updated: {}").format(title) |
|
91
|
|
|
return self.redirect(message=message) |
|
92
|
|
|
|