[IMP] report: pass pdfreport_path to postprocess_pdf_report method

Restore a feature lost due to refactoring in https://github.com/odoo/odoo/commit/e80238042c9d93d492ea8b06b0041aced0d81dcd
that pass the path to the report instead of the attachment content in order to perform changes to be part of the final result.

see https://github.com/odoo/odoo/pull/16936#issuecomment-300827614
This commit is contained in:
Laurent Smet
2017-05-16 15:37:23 +02:00
parent d5fa3232fe
commit 259c5d9145
+7 -14
View File
@@ -216,15 +216,17 @@ class IrActionsReport(models.Model):
)
@api.model
def postprocess_pdf_report(self, res_id, attachment_content, attachment_name):
def postprocess_pdf_report(self, res_id, pdfreport_path, attachment_name):
'''Hook to handle post processing during the pdf report generation.
The basic behavior consists to create a new attachment containing the pdf
base64 encoded.
:param res_id: The record id.
:param attachment_content: The pdf content newly generated by wkhtmltopdf.
:param pdfreport_path: The path to the pdf content newly generated by wkhtmltopdf.
:param attachment_name: The name of the attachment.
'''
with open(pdfreport_path, 'rb') as pdfreport:
attachment_content = pdfreport.read()
attachment = {
'name': attachment_name,
'datas': base64.encodestring(attachment_content),
@@ -434,11 +436,6 @@ class IrActionsReport(models.Model):
pdfdocuments = []
temporary_files = []
# In most case, the report is created for a single report and an attachment is created (business report).
# To avoid reading the resulting report two times (one to return the content to the user and one during
# the post process). Then, in this specific case, the content is kept in memory to avoid a second reading.
content_read = None
for wkhtmltopdf_obj in wkhtmltopdf_objs:
local_command_args = []
pdfreport_fd, pdfreport_path = tempfile.mkstemp(suffix='.pdf', prefix='report.tmp.')
@@ -492,9 +489,7 @@ class IrActionsReport(models.Model):
# Call the postprocess method on the ir.actions.report.
if wkhtmltopdf_obj.res_id and wkhtmltopdf_obj.attachment_name:
with open(pdfreport_path, 'rb') as pdfreport:
content_read = pdfreport.read()
self.postprocess_pdf_report(wkhtmltopdf_obj.res_id, content_read, wkhtmltopdf_obj.attachment_name)
self.postprocess_pdf_report(wkhtmltopdf_obj.res_id, pdfreport_path, wkhtmltopdf_obj.attachment_name)
# Return the entire document
if len(pdfdocuments) == 1:
@@ -502,11 +497,9 @@ class IrActionsReport(models.Model):
else:
entire_report_path = _merge_pdf(pdfdocuments)
temporary_files.append(entire_report_path)
content_read = None
if not content_read:
with open(entire_report_path, 'rb') as pdfdocument:
content_read = pdfdocument.read()
with open(entire_report_path, 'rb') as pdfdocument:
content_read = pdfdocument.read()
# Manual cleanup of the temporary files
for temporary_file in temporary_files: