Skip to content

Commit

Permalink
[16.0][IMP] report_csv: support attachment use
Browse files Browse the repository at this point in the history
  • Loading branch information
sbejaoui committed Sep 26, 2023
1 parent e73b7c2 commit 8f03e49
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion report_csv/models/ir_report.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import logging

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

_logger = logging.getLogger(__name__)


class ReportAction(models.Model):
Expand All @@ -11,14 +17,53 @@ class ReportAction(models.Model):
selection_add=[("csv", "csv")], ondelete={"csv": "set default"}
)

def _create_csv_attachment(self, record, data):
attachment_name = safe_eval(self.attachment, {"object": record, "time": time})

Check warning on line 21 in report_csv/models/ir_report.py

View check run for this annotation

Codecov / codecov/patch

report_csv/models/ir_report.py#L21

Added line #L21 was not covered by tests
# Unable to compute a name for the attachment.
if not attachment_name:
return

Check warning on line 24 in report_csv/models/ir_report.py

View check run for this annotation

Codecov / codecov/patch

report_csv/models/ir_report.py#L24

Added line #L24 was not covered by tests
if record and data:
attachment = {

Check warning on line 26 in report_csv/models/ir_report.py

View check run for this annotation

Codecov / codecov/patch

report_csv/models/ir_report.py#L26

Added line #L26 was not covered by tests
"name": attachment_name,
"raw": data,
"res_model": self.model,
"res_id": record.id,
"type": "binary",
}
try:
self.env["ir.attachment"].create(attachment)
except AccessError:
_logger.info(

Check warning on line 36 in report_csv/models/ir_report.py

View check run for this annotation

Codecov / codecov/patch

report_csv/models/ir_report.py#L33-L36

Added lines #L33 - L36 were not covered by tests
"Cannot save csv report %r as attachment", attachment["name"]
)
else:
_logger.info(

Check warning on line 40 in report_csv/models/ir_report.py

View check run for this annotation

Codecov / codecov/patch

report_csv/models/ir_report.py#L40

Added line #L40 was not covered by tests
"The csv document %s is now saved in the database",
attachment["name"],
)

@api.model
def _render_csv(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 report_model.with_context(

res_id = len(docids) == 1 and docids[0]
record = self.env[report_sudo.model].browse(res_id)
if not report_sudo.attachment or not report_sudo.attachment_use:
return report_model.with_context(
active_model=report_sudo.model
).create_csv_report(docids, data)

attachment = report_sudo.retrieve_attachment(record)

Check warning on line 58 in report_csv/models/ir_report.py

View check run for this annotation

Codecov / codecov/patch

report_csv/models/ir_report.py#L58

Added line #L58 was not covered by tests
if attachment:
return attachment.raw, "csv"

Check warning on line 60 in report_csv/models/ir_report.py

View check run for this annotation

Codecov / codecov/patch

report_csv/models/ir_report.py#L60

Added line #L60 was not covered by tests

data, ext = report_model.with_context(

Check warning on line 62 in report_csv/models/ir_report.py

View check run for this annotation

Codecov / codecov/patch

report_csv/models/ir_report.py#L62

Added line #L62 was not covered by tests
active_model=report_sudo.model
).create_csv_report(docids, data)
report_sudo._create_csv_attachment(record, data)
return data, ext

Check warning on line 66 in report_csv/models/ir_report.py

View check run for this annotation

Codecov / codecov/patch

report_csv/models/ir_report.py#L65-L66

Added lines #L65 - L66 were not covered by tests

@api.model
def _get_report_from_name(self, report_name):
Expand Down

0 comments on commit 8f03e49

Please sign in to comment.