Skip to content

Commit 6e85d0b

Browse files
sbejaouivictoralmau
authored andcommitted
[16.0][IMP] report_csv: support attachment use
[BOT] post-merge updates
1 parent f880ee5 commit 6e85d0b

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

report_csv/README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Base report csv
77
!! This file is generated by oca-gen-addon-readme !!
88
!! changes will be overwritten. !!
99
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-
!! source digest: sha256:3d970690227b2dd444c5359798ce3ea5be684d6fbc82504e90694e193e738938
10+
!! source digest: sha256:a39959a00912416454f8818d70b9a3ffdd305aeb1b1c35ce3ea007f3c41526bd
1111
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1212
1313
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png

report_csv/__manifest__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"author": "Creu Blanca, Odoo Community Association (OCA)",
77
"website": "https://github.com/OCA/reporting-engine",
88
"category": "Reporting",
9-
"version": "16.0.2.0.0",
9+
"version": "16.0.2.1.0",
1010
"license": "AGPL-3",
1111
"depends": ["base", "web"],
1212
"demo": ["demo/report.xml"],

report_csv/models/ir_report.py

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# Copyright 2019 Creu Blanca
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
33

4+
import logging
5+
46
from odoo import api, fields, models
7+
from odoo.exceptions import AccessError
8+
from odoo.tools.safe_eval import safe_eval, time
9+
10+
_logger = logging.getLogger(__name__)
511

612

713
class ReportAction(models.Model):
@@ -19,18 +25,58 @@ class ReportAction(models.Model):
1925
"there is a character that fail to be encoded.",
2026
)
2127

28+
def _create_csv_attachment(self, record, data):
29+
attachment_name = safe_eval(self.attachment, {"object": record, "time": time})
30+
# Unable to compute a name for the attachment.
31+
if not attachment_name:
32+
return
33+
if record and data:
34+
attachment = {
35+
"name": attachment_name,
36+
"raw": data,
37+
"res_model": self.model,
38+
"res_id": record.id,
39+
"type": "binary",
40+
}
41+
try:
42+
self.env["ir.attachment"].create(attachment)
43+
except AccessError:
44+
_logger.info(
45+
"Cannot save csv report %r as attachment", attachment["name"]
46+
)
47+
else:
48+
_logger.info(
49+
"The csv document %s is now saved in the database",
50+
attachment["name"],
51+
)
52+
2253
@api.model
2354
def _render_csv(self, report_ref, docids, data):
2455
report_sudo = self._get_report(report_ref)
2556
report_model_name = "report.%s" % report_sudo.report_name
2657
report_model = self.env[report_model_name]
27-
return report_model.with_context(
58+
res_id = len(docids) == 1 and docids[0]
59+
if not res_id or not report_sudo.attachment or not report_sudo.attachment_use:
60+
return report_model.with_context(
61+
**{
62+
"active_model": report_sudo.model,
63+
"encoding": self.encoding,
64+
"encode_error_handling": self.encode_error_handling,
65+
}
66+
).create_csv_report(docids, data)
67+
record = self.env[report_sudo.model].browse(res_id)
68+
attachment = report_sudo.retrieve_attachment(record)
69+
if attachment:
70+
return attachment.raw.decode(), "csv"
71+
data, ext = report_model.with_context(
2872
**{
2973
"active_model": report_sudo.model,
3074
"encoding": self.encoding,
3175
"encode_error_handling": self.encode_error_handling,
3276
}
3377
).create_csv_report(docids, data)
78+
report_sudo._create_csv_attachment(record, data)
79+
return data, ext
3480

3581
@api.model
3682
def _get_report_from_name(self, report_name):

report_csv/static/description/index.html

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<?xml version="1.0" encoding="utf-8"?>
21
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
32
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
43
<head>
@@ -9,10 +8,11 @@
98

109
/*
1110
:Author: David Goodger (goodger@python.org)
12-
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
11+
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
1312
:Copyright: This stylesheet has been placed in the public domain.
1413
1514
Default cascading style sheet for the HTML output of Docutils.
15+
Despite the name, some widely supported CSS2 features are used.
1616
1717
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
1818
customize this style sheet.
@@ -275,7 +275,7 @@
275275
margin-left: 2em ;
276276
margin-right: 2em }
277277

278-
pre.code .ln { color: grey; } /* line numbers */
278+
pre.code .ln { color: gray; } /* line numbers */
279279
pre.code, code { background-color: #eeeeee }
280280
pre.code .comment, code .comment { color: #5C6576 }
281281
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
@@ -301,7 +301,7 @@
301301
span.pre {
302302
white-space: pre }
303303

304-
span.problematic {
304+
span.problematic, pre.problematic {
305305
color: red }
306306

307307
span.section-subtitle {
@@ -367,7 +367,7 @@ <h1 class="title">Base report csv</h1>
367367
!! This file is generated by oca-gen-addon-readme !!
368368
!! changes will be overwritten. !!
369369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370-
!! source digest: sha256:3d970690227b2dd444c5359798ce3ea5be684d6fbc82504e90694e193e738938
370+
!! source digest: sha256:a39959a00912416454f8818d70b9a3ffdd305aeb1b1c35ce3ea007f3c41526bd
371371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372372
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/reporting-engine/tree/16.0/report_csv"><img alt="OCA/reporting-engine" src="https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/reporting-engine-16-0/reporting-engine-16-0-report_csv"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/reporting-engine&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373373
<p>This module provides a basic report class to generate csv report.</p>
@@ -471,7 +471,9 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
471471
<div class="section" id="maintainers">
472472
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
473473
<p>This module is maintained by the OCA.</p>
474-
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
474+
<a class="reference external image-reference" href="https://odoo-community.org">
475+
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
476+
</a>
475477
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
476478
mission is to support the collaborative development of Odoo features and
477479
promote its widespread use.</p>

report_csv/tests/test_report.py

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ def test_report(self):
4747
dict_report = list(csv.DictReader(str_io, delimiter=";", quoting=csv.QUOTE_ALL))
4848
self.assertEqual(self.docs.name, dict(dict_report[0])["name"])
4949

50+
def test_attachment_use(self):
51+
self.report.attachment = "object.name+'.csv'"
52+
self.report.attachment_use = True
53+
rep = self.report_object._render(self.report_name, self.docs.ids, {})
54+
attachment = self.env["ir.attachment"].search(
55+
[("name", "=", self.docs.name + ".csv")]
56+
)
57+
self.assertTrue(attachment)
58+
self.assertEqual(attachment.raw.decode(), rep[0])
59+
rep_from_attachment = self.report_object._render(
60+
self.report_name, self.docs.ids, {}
61+
)
62+
self.assertTupleEqual(rep, rep_from_attachment)
63+
5064
def test_id_retrieval(self):
5165

5266
# Typical call from WebUI with wizard

0 commit comments

Comments
 (0)