Browse Source

[FIX] report_py3o: Add missing method _merge_pdf.

This method was previously provided by Odoo and is used to merge all the reports generated if the generation is called for more than one record
pull/347/head
Laurent Mignon (ACSONE) 6 years ago
parent
commit
00b3ec1afa
  1. 3
      report_py3o/__manifest__.py
  2. 22
      report_py3o/models/py3o_report.py
  3. 27
      report_py3o/tests/test_report_py3o.py

3
report_py3o/__manifest__.py

@ -14,7 +14,8 @@
'depends': ['web'],
'external_dependencies': {
'python': ['py3o.template',
'py3o.formats']
'py3o.formats',
'PyPDF2']
},
'data': [
'security/ir.model.access.csv',

22
report_py3o/models/py3o_report.py

@ -28,7 +28,10 @@ try:
from py3o.formats import Formats, UnkownFormatException
except ImportError:
logger.debug('Cannot import py3o.formats')
try:
from PyPDF2 import PdfFileWriter, PdfFileReader
except ImportError:
logger.debug('Cannot import PyPDF2')
_extender_functions = {}
@ -302,6 +305,23 @@ class Py3oReport(models.TransientModel):
cpt += 1
return result_path
@api.model
def _merge_pdf(self, reports_path):
""" Merge PDF files into one.
:param reports_path: list of path of pdf files
:returns: path of the merged pdf
"""
writer = PdfFileWriter()
for path in reports_path:
reader = PdfFileReader(path)
writer.appendPagesFromReader(reader)
merged_file_fd, merged_file_path = tempfile.mkstemp(
suffix='.pdf', prefix='report.merged.tmp.')
with closing(os.fdopen(merged_file_fd, 'wb')) as merged_file:
writer.write(merged_file)
return merged_file_path
@api.multi
def _merge_results(self, reports_path):
self.ensure_one()

27
report_py3o/tests/test_report_py3o.py

@ -18,6 +18,8 @@ from odoo.addons.base.tests.test_mimetypes import PNG
from ..models.py3o_report import TemplateNotFound
from ..models._py3o_parser_context import format_multiline_value
from base64 import b64encode
from PyPDF2 import PdfFileWriter
from PyPDF2.pdf import PageObject
import logging
logger = logging.getLogger(__name__)
@ -79,7 +81,30 @@ class TestReportPy3o(TransactionCase):
def test_reports(self):
res = self.report.render(self.env.user.ids)
self.assertTrue(res)
self.report.py3o_filetype = 'pdf'
def test_reports_merge_zip(self):
users = self.env['res.users'].search([])
self.assertTrue(len(users) > 0)
py3o_report = self.env['py3o.report']
_zip_results = self.py3o_report._zip_results
with mock.patch.object(
py3o_report.__class__, '_zip_results') as patched_zip_results:
patched_zip_results.side_effect = _zip_results
content, filetype = self.report.render(users.ids)
self.assertEqual(1, patched_zip_results.call_count)
self.assertEqual(filetype, 'zip')
def test_reports_merge_pdf(self):
reports_path = []
for i in range(0, 3):
result = tempfile.mktemp('.txt')
writer = PdfFileWriter()
writer.addPage(PageObject.createBlankPage(width=100, height=100))
with open(result, 'wb') as fp:
writer.write(fp)
reports_path.append(result)
res = self.py3o_report._merge_pdf(reports_path)
self.assertTrue(res)
def test_report_load_from_attachment(self):
self.report.write({"attachment_use": True,

Loading…
Cancel
Save