|
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 Products.Five.browser import BrowserView |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class DownloadView(BrowserView): |
|
26
|
|
|
"""Download View |
|
27
|
|
|
""" |
|
28
|
|
|
|
|
29
|
|
|
def __init__(self, context, request): |
|
30
|
|
|
super(DownloadView, self).__init__(context, request) |
|
31
|
|
|
|
|
32
|
|
|
def __call__(self): |
|
33
|
|
|
filename = self.get_report_filename(self.context) |
|
34
|
|
|
pdf = self.context.getPdf() |
|
35
|
|
|
self.download(pdf.data, filename) |
|
36
|
|
|
|
|
37
|
|
|
def get_report_filename(self, report): |
|
38
|
|
|
"""Generate the filename for the sample PDF |
|
39
|
|
|
""" |
|
40
|
|
|
sample = report.getAnalysisRequest() |
|
41
|
|
|
return "{}.pdf".format(api.get_id(sample)) |
|
42
|
|
|
|
|
43
|
|
|
def download(self, data, filename, content_type="application/pdf"): |
|
44
|
|
|
"""Download the PDF |
|
45
|
|
|
""" |
|
46
|
|
|
self.request.response.setHeader( |
|
47
|
|
|
"Content-Disposition", "inline; filename=%s" % filename) |
|
48
|
|
|
self.request.response.setHeader("Content-Type", content_type) |
|
49
|
|
|
self.request.response.setHeader("Content-Length", len(data)) |
|
50
|
|
|
self.request.response.setHeader("Cache-Control", "no-store") |
|
51
|
|
|
self.request.response.setHeader("Pragma", "no-cache") |
|
52
|
|
|
self.request.response.write(data) |
|
53
|
|
|
|