From 860439b7433fc88b476bf999b758bd25625fcd12 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Tue, 30 May 2017 10:51:18 +0200 Subject: [PATCH] [FIX] report_py3o: must return the path to the report refs #119 --- report_py3o/models/py3o_report.py | 6 +++- report_py3o/tests/test_report_py3o.py | 47 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/report_py3o/models/py3o_report.py b/report_py3o/models/py3o_report.py index 6b76aa35..24b1af45 100644 --- a/report_py3o/models/py3o_report.py +++ b/report_py3o/models/py3o_report.py @@ -295,7 +295,11 @@ class Py3oReport(models.TransientModel): 'loaded_documents'].get(model_instance.id): d = save_in_attachment[ 'loaded_documents'].get(model_instance.id) - return d, self.ir_actions_report_xml_id.py3o_filetype + report_file = tempfile.mktemp( + "." + self.ir_actions_report_xml_id.py3o_filetype) + with open(report_file, "wb") as f: + f.write(d) + return report_file return self._create_single_report( model_instance, data, save_in_attachment) diff --git a/report_py3o/tests/test_report_py3o.py b/report_py3o/tests/test_report_py3o.py index 0ddd7541..f53ed058 100644 --- a/report_py3o/tests/test_report_py3o.py +++ b/report_py3o/tests/test_report_py3o.py @@ -2,6 +2,7 @@ # Copyright 2016 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).). +import base64 from base64 import b64decode import mock import os @@ -106,6 +107,52 @@ class TestReportPy3o(TransactionCase): self.env.user.ids, self.report.report_name, {}) self.assertEqual(('test result', 'pdf'), res) + def test_report_load_from_attachment(self): + py3o_report = self.env['py3o.report'] + with mock.patch.object( + py3o_report.__class__, '_create_single_report') as patched_pdf: + result = tempfile.mktemp('.txt') + with open(result, 'w') as fp: + fp.write('dummy') + patched_pdf.return_value = result + # test the call the the create method inside our custom parser + self.report.render_report(self.env.user.ids, + self.report.report_name, + {}) + self.assertEqual(1, patched_pdf.call_count) + # generated files no more exists + self.assertFalse(os.path.exists(result)) + res = self.report.render_report( + self.env.user.ids, self.report.report_name, {}) + self.assertTrue(res) + py3o_server = self.env['py3o.server'].create({"url": "http://dummy"}) + # check the call to the fusion server + self.report.write({"py3o_filetype": "pdf", + "py3o_server_id": py3o_server.id, + "attachment_use": True, + "attachment": "'my_saved_report'"}) + attachments = self.env['ir.attachment'].search([]) + with mock.patch('requests.post') as patched_post: + magick_response = mock.MagicMock() + magick_response.status_code = 200 + patched_post.return_value = magick_response + magick_response.iter_content.return_value = "test result" + res = self.report.render_report( + self.env.user.ids, self.report.report_name, {}) + self.assertEqual(('test result', 'pdf'), res) + new_attachments = self.env['ir.attachment'].search([]) + created_attachement = new_attachments - attachments + self.assertEqual(1, len(created_attachement)) + content = b64decode(created_attachement.datas) + self.assertEqual("test result", content) + # put a new content into tha attachement and check that the next + # time we ask the report we received the saved attachment not a newly + # generated document + created_attachement.datas = base64.encodestring("new content") + res = self.report.render_report( + self.env.user.ids, self.report.report_name, {}) + self.assertEqual(('new content', 'pdf'), res) + def test_report_post_process(self): """ By default the post_process method is in charge to save the