Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get images function #15

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):

Expand Down
59 changes: 58 additions & 1 deletion tests/test_jinja_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
),
)
Expand All @@ -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
Loading