Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor report filename generation to own method #2072

Merged
merged 6 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
2.3.0 (unreleased)
------------------

- #2072 Refactor report filename generation to own method
- #2071 Move sample reports to report catalog, add batch ID and email sent flag to listing
- #2070 Fix typo/duplicate translation key in colophon
- #2067 Replace ParentAnalysisRequest ReferenceField by UIDReferenceField
Expand Down
10 changes: 8 additions & 2 deletions src/bika/lims/browser/publish/downloadview.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Copyright 2018-2021 by it's authors.
# Some rights reserved, see README and LICENSE.

from bika.lims import api
from Products.Five.browser import BrowserView


Expand All @@ -29,11 +30,16 @@ def __init__(self, context, request):
super(DownloadView, self).__init__(context, request)

def __call__(self):
ar = self.context.getAnalysisRequest()
filename = "{}.pdf".format(ar.getId())
filename = self.get_report_filename(self.context)
pdf = self.context.getPdf()
self.download(pdf.data, filename)

def get_report_filename(self, report):
"""Generate the filename for the sample PDF
"""
sample = report.getAnalysisRequest()
return "{}.pdf".format(api.get_id(sample))

def download(self, data, filename, content_type="application/pdf"):
"""Download the PDF
"""
Expand Down
11 changes: 8 additions & 3 deletions src/bika/lims/browser/publish/emailview.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ def email_attachments(self):
logger.error("Skipping empty PDF for report {}"
.format(report.getId()))
continue
sample = report.getAnalysisRequest()
filename = "{}.pdf".format(api.get_id(sample))
filename = self.get_report_filename(report)
filedata = pdf.data
attachments.append(
mailapi.to_email_attachment(filedata, filename))
Expand Down Expand Up @@ -550,7 +549,7 @@ def get_report_data(self, report):
attachments_data = map(self.get_attachment_data, attachments)
pdf = self.get_pdf(report)
filesize = "{} Kb".format(self.get_filesize(pdf))
filename = "{}.pdf".format(sample.getId())
filename = self.get_report_filename(report)

return {
"sample": sample,
Expand Down Expand Up @@ -700,6 +699,12 @@ def get_filesize(self, f):
except (POSKeyError, TypeError, AttributeError):
return 0.0

def get_report_filename(self, report):
"""Generate the filename for the sample PDF
"""
sample = report.getAnalysisRequest()
return "{}.pdf".format(api.get_id(sample))

def get_pdf(self, obj):
"""Get the report PDF
"""
Expand Down