Skip to content

Commit

Permalink
Add get attachments method
Browse files Browse the repository at this point in the history
Also changed the old attachments to attachment due the return being a single object
GuillemCalidae committed Jul 16, 2024
1 parent eec8790 commit 79f5d4d
Showing 2 changed files with 74 additions and 2 deletions.
17 changes: 16 additions & 1 deletion report.py
Original file line number Diff line number Diff line change
@@ -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):

59 changes: 58 additions & 1 deletion tests/test_jinja_report.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 79f5d4d

Please sign in to comment.