|
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.content.attachment import Attachment |
|
28
|
|
|
from bika.lims.interfaces.analysis import IRequestAnalysis |
|
29
|
|
|
from Products.CMFPlone.utils import safe_unicode |
|
30
|
|
|
from Products.Five.browser import BrowserView |
|
31
|
|
|
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class SetAnalysisConditionsView(BrowserView): |
|
35
|
|
|
"""View for the update of analysis conditions |
|
36
|
|
|
""" |
|
37
|
|
|
template = ViewPageTemplateFile("templates/set_analysis_conditions.pt") |
|
38
|
|
|
|
|
39
|
|
|
def __call__(self): |
|
40
|
|
|
if self.request.form.get("submitted", False): |
|
41
|
|
|
return self.handle_submit() |
|
42
|
|
|
return self.template() |
|
43
|
|
|
|
|
44
|
|
|
def redirect(self, message=None, level="info"): |
|
45
|
|
|
"""Redirect with a message |
|
46
|
|
|
""" |
|
47
|
|
|
redirect_url = api.get_url(self.context) |
|
48
|
|
|
if message is not None: |
|
49
|
|
|
self.context.plone_utils.addPortalMessage(message, level) |
|
50
|
|
|
return self.request.response.redirect(redirect_url) |
|
51
|
|
|
|
|
52
|
|
|
def get_analysis(self): |
|
53
|
|
|
uid = self.get_uid_from_request() |
|
54
|
|
|
obj = api.get_object_by_uid(uid) |
|
55
|
|
|
if IRequestAnalysis.providedBy(obj): |
|
56
|
|
|
# Only analyses that implement IRequestAnalysis support conditions |
|
57
|
|
|
return obj |
|
58
|
|
|
return None |
|
59
|
|
|
|
|
60
|
|
|
def get_uid_from_request(self): |
|
61
|
|
|
"""Returns the uid from the request, if any |
|
62
|
|
|
""" |
|
63
|
|
|
uid = self.request.form.get("uid", self.request.get("uid")) |
|
64
|
|
|
if api.is_uid(uid): |
|
65
|
|
|
return uid |
|
66
|
|
|
return None |
|
67
|
|
|
|
|
68
|
|
|
def get_analysis_name(self): |
|
69
|
|
|
analysis = self.get_analysis() |
|
70
|
|
|
return api.get_title(analysis) |
|
71
|
|
|
|
|
72
|
|
|
def get_conditions(self): |
|
73
|
|
|
"""Returns the conditions to display in the form, those with empty or |
|
74
|
|
|
non-set value included |
|
75
|
|
|
""" |
|
76
|
|
|
conditions = self.get_analysis().getConditions(empties=True) |
|
77
|
|
|
conditions = copy.deepcopy(conditions) |
|
78
|
|
|
for condition in conditions: |
|
79
|
|
|
choices = condition.get("choices", "") |
|
80
|
|
|
options = filter(None, choices.split("|")) |
|
81
|
|
|
condition.update({"options": options}) |
|
82
|
|
|
|
|
83
|
|
|
if condition.get("type") == "file": |
|
84
|
|
|
uid = condition.get("value") |
|
85
|
|
|
condition["attachment"] = self.get_attachment_info(uid) |
|
86
|
|
|
|
|
87
|
|
|
return conditions |
|
88
|
|
|
|
|
89
|
|
|
def get_attachment_info(self, uid): |
|
90
|
|
|
attachment = api.get_object_by_uid(uid, default=None) |
|
91
|
|
|
if not isinstance(attachment, Attachment): |
|
92
|
|
|
return {} |
|
93
|
|
|
|
|
94
|
|
|
url = api.get_url(attachment) |
|
95
|
|
|
at_file = attachment.getAttachmentFile() |
|
96
|
|
|
return { |
|
97
|
|
|
"uid": api.get_uid(attachment), |
|
98
|
|
|
"id": api.get_id(attachment), |
|
99
|
|
|
"url": url, |
|
100
|
|
|
"download_url": "{}/at_download/AttachmentFile".format(url), |
|
101
|
|
|
"filename": at_file.filename, |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
def handle_submit(self): |
|
105
|
|
|
analysis = self.get_analysis() |
|
106
|
|
|
title = safe_unicode(api.get_title(analysis)) |
|
107
|
|
|
if not check_permission(FieldEditAnalysisConditions, analysis): |
|
108
|
|
|
message = _("Not allowed to update conditions: {}").format(title) |
|
109
|
|
|
return self.redirect(message=message, level="error") |
|
110
|
|
|
|
|
111
|
|
|
# Update the conditions |
|
112
|
|
|
conditions = self.request.form.get("conditions", []) |
|
113
|
|
|
analysis.setConditions(conditions) |
|
114
|
|
|
message = _("Analysis conditions updated: {}").format(title) |
|
115
|
|
|
return self.redirect(message=message) |
|
116
|
|
|
|