Skip to content

Commit

Permalink
Add get images function
Browse files Browse the repository at this point in the history
This function returns a list of bytes instead of one (get_attachments)
  • Loading branch information
GuillemCalidae committed Jul 16, 2024
1 parent eec8790 commit 299748b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
15 changes: 15 additions & 0 deletions report.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def get_environ(cls):
def get_context(cls, records, header, data):
report_context = super().get_context(records, header, data)
report_context['attachments'] = cls.get_attachment
report_context['images'] = cls.get_images
return report_context

@classmethod
Expand All @@ -77,6 +78,20 @@ def get_attachment(cls, record, name):
finally:
return data

@classmethod
def get_images(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):

Expand Down
56 changes: 56 additions & 0 deletions tests/test_jinja_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,61 @@ 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 img in images(record, 'image') %}"
b"{{ img | 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,
(
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAAXNSR0IArs"
"4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAVSU"
"RBVBhXY3hnZPVWTo0BiN/buQIAJsQFL0PuuMYAAAAASUVORK5CYII="
# second image
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAAXNSR0IArs"
"4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAVSU"
"RBVBhXY3hnZPVWTo0BiN/buQIAJsQFL0PuuMYAAAAASUVORK5CYII="
)
)


del ModuleTestCase

0 comments on commit 299748b

Please sign in to comment.