From 79f5d4d86913106bc20afa11ed8bb4776a9258e3 Mon Sep 17 00:00:00 2001 From: GuillemCalidae Date: Tue, 16 Jul 2024 11:16:06 +0200 Subject: [PATCH] Add get attachments method Also changed the old attachments to attachment due the return being a single object --- report.py | 17 ++++++++++- tests/test_jinja_report.py | 59 +++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/report.py b/report.py index f5f7566..135d0c2 100644 --- a/report.py +++ b/report.py @@ -52,7 +52,8 @@ def get_environ(cls): @classmethod def get_context(cls, records, header, data): report_context = super().get_context(records, header, data) - report_context['attachments'] = cls.get_attachment + report_context['attachment'] = cls.get_attachment + report_context['attachments'] = cls.get_attachments return report_context @classmethod @@ -77,6 +78,20 @@ def get_attachment(cls, record, name): finally: return data + @classmethod + def get_attachments(cls, record, name): + Attachment = Pool().get('ir.attachment') + try: + attachments = Attachment.search([ + ('resource', '=', record), + ('name', 'ilike', f"{name}%"), + ]) + data = [attachment.data for attachment in attachments] + except ValueError: + data = [] + finally: + return data + class SetTranslationJinja2(JinjaExtensionsMixin, metaclass=PoolMeta): diff --git a/tests/test_jinja_report.py b/tests/test_jinja_report.py index b173479..7f8ccaf 100644 --- a/tests/test_jinja_report.py +++ b/tests/test_jinja_report.py @@ -66,7 +66,7 @@ def test_jinja_report_attachment(self): extension='html', template_extension='html', report_content_custom=( - b"{{ attachments(record, 'example.png') " + b"{{ attachment(record, 'example.png') " b"| b64encode | decode }}" ), ) @@ -85,5 +85,62 @@ def test_jinja_report_attachment(self): ) ) + @with_transaction() + def test_jinja_report_images(self): + # GIVEN + record = trytond_factories.TestModel.create() + trytond_factories.DataAttachment.create( + resource=record, + name='image example.png', + data=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x02\x00' + b'\x00\x00\x02\x08\x02\x00\x00\x00\xfd\xd4\x9as\x00\x00\x00\x01' + b'sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f' + b'\x0b\xfca\x05\x00\x00\x00\tpHYs\x00\x00\x0e\xc3\x00\x00\x0e\xc3' + b'\x01\xc7o\xa8d\x00\x00\x00\x15IDAT\x18Wcxgd\xf5VN\x8d\x01\x88' + b'\xdf\xdb\xb9\x02\x00&\xc4\x05/C\xee\xb8\xc6\x00\x00\x00\x00' + b'IEND\xaeB`\x82', + ) + trytond_factories.DataAttachment.create( + resource=record, + name='image example2.png', + data=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x02\x00' + b'\x00\x00\x02\x08\x02\x00\x00\x00\xfd\xd4\x9as\x00\x00\x00\x01' + b'sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f' + b'\x0b\xfca\x05\x00\x00\x00\tpHYs\x00\x00\x0e\xc3\x00\x00\x0e\xc3' + b'\x01\xc7o\xa8d\x00\x00\x00\x15IDAT\x18Wcxgd\xf5VN\x8d\x01\x88' + b'\xdf\xdb\xb9\x02\x00&\xc4\x05/C\xee\xb8\xc6\x00\x00\x00\x00' + b'IEND\xaeB`\x82', + ) + report = trytond_factories.Report.create( + model='test.model', + report_name='jinja.test_report', + extension='html', + template_extension='html', + report_content_custom=( + b"{% for attachment in attachments(record, 'image') %}" + b"{{ attachment | b64encode | decode }}" + b"{% endfor %}" + ), + ) + Report = Pool().get(report.report_name, type='report') + + # WHEN + (_, report_data, _, _) = Report.execute(ids=[record.id], data={}) + + # THEN + self.assertEqual( + report_data, + ( + # first attachment + "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAAXNSR0IArs" + "4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAVSU" + "RBVBhXY3hnZPVWTo0BiN/buQIAJsQFL0PuuMYAAAAASUVORK5CYII=" + # second attachment + "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAAXNSR0IArs" + "4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAVSU" + "RBVBhXY3hnZPVWTo0BiN/buQIAJsQFL0PuuMYAAAAASUVORK5CYII=" + ) + ) + del ModuleTestCase