Skip to content
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
46 changes: 44 additions & 2 deletions report_xlsx/models/ir_report.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Copyright 2015 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models
import logging
Comment thread
pedrobaeza marked this conversation as resolved.

from odoo import api, exceptions, fields, models
from odoo.tools.safe_eval import safe_eval, time

_logger = logging.getLogger(__name__)


class ReportAction(models.Model):
Expand All @@ -16,11 +21,14 @@ def _render_xlsx(self, report_ref, docids, data):
report_sudo = self._get_report(report_ref)
report_model_name = "report.%s" % report_sudo.report_name
report_model = self.env[report_model_name]
return (
ret = (
report_model.with_context(active_model=report_sudo.model)
.sudo(False)
.create_xlsx_report(docids, data) # noqa
)
if ret and isinstance(ret, (tuple, list)): # data, "xlsx"
report_sudo.save_xlsx_report_attachment(docids, ret[0])
return ret

@api.model
def _get_report_from_name(self, report_name):
Expand All @@ -35,3 +43,37 @@ def _get_report_from_name(self, report_name):
]
context = self.env["res.users"].context_get()
return report_obj.with_context(**context).search(conditions, limit=1)

def save_xlsx_report_attachment(self, docids, report_contents):
"""Save as attachment when the report is set up as such."""
# Similar to ir.actions.report::_render_qweb_pdf in the base module.
if not self.attachment:
return
if len(docids) != 1: # unlike PDFs, here we don't have multiple streams
_logger.warning(f"{self.name}: No records to save attachments onto.")
return
record = self.env[self.model].browse(docids)
attachment_name = safe_eval(self.attachment, {"object": record, "time": time})
if not attachment_name:
return # same as for PDFs, get out silently when name fails
attachment_values = {
"name": attachment_name,
"raw": report_contents,
"res_id": record.id,
"res_model": self.model,
"type": "binary",
}
try:
attachment = self.env["ir.attachment"].create(attachment_values)
except exceptions.AccessError:
_logger.info(
"Cannot save XLSX report %r attachment for user %r",
attachment_values["name"],
self.env.user.display_name,
)
else:
_logger.info(
"The XLSX document %r is now saved in the database",
attachment_values["name"],
)
return attachment, record
1 change: 1 addition & 0 deletions report_xlsx/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
* Rod Schouteden <rod.schouteden@dynapps.be>
* Eugene Molotov <molotov@it-projects.info>
* Christopher Ormaza <chris.ormaza@forgeflow.com>
* Houzéfa Abbasbhay <houzefa.abba@xcg-consulting.fr>
9 changes: 9 additions & 0 deletions report_xlsx/tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ def test_report(self):
sheet = wb.sheet_by_index(0)
self.assertEqual(sheet.cell(0, 0).value, self.docs.name)

def test_save_attachment(self):
self.report.attachment = 'object.name + ".xlsx"'
self.report_object._render(self.report_name, self.docs.ids, {})
attachment = self.env["ir.attachment"].search(
[("res_id", "=", self.docs.id), ("res_model", "=", self.docs._name)]
)
self.assertEqual(len(attachment), 1)
self.assertEqual(attachment.name, f"{self.docs.name}.xlsx")

def test_id_retrieval(self):

# Typical call from WebUI with wizard
Expand Down