|
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-2021 by it's authors. |
|
19
|
|
|
# Some rights reserved, see README and LICENSE. |
|
20
|
|
|
|
|
21
|
|
|
from bika.lims import api |
|
22
|
|
|
from bika.lims import FieldEditAnalysisResult |
|
23
|
|
|
from bika.lims import WorksheetAddAttachment |
|
24
|
|
|
from bika.lims.api.security import check_permission |
|
25
|
|
|
from plone.app.layout.viewlets.common import ViewletBase |
|
26
|
|
|
from plone.memoize import view |
|
27
|
|
|
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class AttachmentsViewlet(ViewletBase): |
|
31
|
|
|
"""Viewlet to manage Attachments in ARs |
|
32
|
|
|
|
|
33
|
|
|
Most of the heavy lifting is now delegated in the template to the |
|
34
|
|
|
`attachments_view` browser view. |
|
35
|
|
|
""" |
|
36
|
|
|
template = ViewPageTemplateFile("templates/attachments.pt") |
|
37
|
|
|
|
|
38
|
|
|
def get_attachments_view(self): |
|
39
|
|
|
# refactored functionality into this separate Browser view, to be able |
|
40
|
|
|
# to have a form submit target. |
|
41
|
|
|
attachments_view = api.get_view("attachments_view", |
|
42
|
|
|
context=self.context, |
|
43
|
|
|
request=self.request) |
|
44
|
|
|
return attachments_view |
|
45
|
|
|
|
|
46
|
|
|
def show(self): |
|
47
|
|
|
"""Controls if the viewlet should be rendered |
|
48
|
|
|
""" |
|
49
|
|
|
url = self.request.getURL() |
|
50
|
|
|
# XXX: Hack to show the viewlet only on the AR base_view |
|
51
|
|
|
if not any(map(url.endswith, ["base_view", "manage_results"])): |
|
52
|
|
|
return False |
|
53
|
|
|
return True |
|
54
|
|
|
|
|
55
|
|
|
def update(self): |
|
56
|
|
|
"""Called always before render() |
|
57
|
|
|
""" |
|
58
|
|
|
super(AttachmentsViewlet, self).update() |
|
59
|
|
|
self.attachments_view = self.get_attachments_view() |
|
60
|
|
|
|
|
61
|
|
|
def render(self): |
|
62
|
|
|
if not self.show(): |
|
63
|
|
|
return "" |
|
64
|
|
|
return self.template() |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
class WorksheetAttachmentsViewlet(AttachmentsViewlet): |
|
68
|
|
|
"""Viewlet to manage Attachments on Worksheets |
|
69
|
|
|
|
|
70
|
|
|
Most of the heavy lifting is now delegated in the template to the |
|
71
|
|
|
`attachments_view` browser view. |
|
72
|
|
|
""" |
|
73
|
|
|
template = ViewPageTemplateFile("templates/worksheet_attachments.pt") |
|
74
|
|
|
|
|
75
|
|
|
def show(self): |
|
76
|
|
|
"""Controls if the viewlet should be rendered |
|
77
|
|
|
""" |
|
78
|
|
|
# XXX: Hack to show the viewlet only on the WS manage_results view |
|
79
|
|
|
if not self.request.getURL().endswith("manage_results"): |
|
80
|
|
|
return False |
|
81
|
|
|
return check_permission(WorksheetAddAttachment, self.context) |
|
82
|
|
|
|
|
83
|
|
|
@view.memoize |
|
84
|
|
|
def get_services(self): |
|
85
|
|
|
"""Returns a list of dicts that represent the Analysis Services that |
|
86
|
|
|
are editable and present in current Worksheet |
|
87
|
|
|
""" |
|
88
|
|
|
services = set() |
|
89
|
|
|
for analysis in self.context.getAnalyses(): |
|
90
|
|
|
# Skip non-editable analyses |
|
91
|
|
|
if not check_permission(FieldEditAnalysisResult, analysis): |
|
92
|
|
|
continue |
|
93
|
|
|
service = analysis.getAnalysisService() |
|
94
|
|
|
services.add(service) |
|
95
|
|
|
|
|
96
|
|
|
# Return the |
|
97
|
|
|
services = sorted(list(services), key=lambda s: api.get_title(s)) |
|
98
|
|
|
return map(self.get_service_info, services) |
|
99
|
|
|
|
|
100
|
|
|
def get_service_info(self, service): |
|
101
|
|
|
"""Returns a dict that represents an analysis service |
|
102
|
|
|
""" |
|
103
|
|
|
return { |
|
104
|
|
|
"uid": api.get_uid(service), |
|
105
|
|
|
"title": api.get_title(service), |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
@view.memoize |
|
109
|
|
|
def get_analyses(self): |
|
110
|
|
|
"""Returns a list of dicts that represent the Analyses that are |
|
111
|
|
|
editable and present in current Worksheet |
|
112
|
|
|
""" |
|
113
|
|
|
analyses = set() |
|
114
|
|
|
for analysis in self.context.getAnalyses(): |
|
115
|
|
|
# Skip non-editable analyses |
|
116
|
|
|
if not check_permission(FieldEditAnalysisResult, analysis): |
|
117
|
|
|
continue |
|
118
|
|
|
analyses.add(analysis) |
|
119
|
|
|
|
|
120
|
|
|
analyses = map(self.get_analysis_info, analyses) |
|
121
|
|
|
return sorted(analyses, key=lambda s: s.get("title")) |
|
122
|
|
|
|
|
123
|
|
|
def get_analysis_info(self, analysis): |
|
124
|
|
|
"""Returns a dict that represents an analysis |
|
125
|
|
|
""" |
|
126
|
|
|
title = api.get_title(analysis) |
|
127
|
|
|
sample_id = analysis.getRequestID() |
|
128
|
|
|
position = self.context.get_slot_position_for(analysis) |
|
129
|
|
|
return { |
|
130
|
|
|
"uid": api.get_uid(analysis), |
|
131
|
|
|
"title": "{}: {} - {}".format(str(position), sample_id, title) |
|
132
|
|
|
} |
|
133
|
|
|
|