Skip to content

Commit

Permalink
Merge pull request #17 from calidae/add-attachments-function
Browse files Browse the repository at this point in the history
Add attachments function and version 7.0.1
  • Loading branch information
GuillemCalidae authored Jul 16, 2024
2 parents 522c8a2 + 492d011 commit 1464fae
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
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_attachments(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
2 changes: 1 addition & 1 deletion tryton.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tryton]
version=7.0.0
version=7.0.1
depends:
ir
xml:

0 comments on commit 1464fae

Please sign in to comment.