Skip to content

Commit 621dcc1

Browse files
committed
Merge PR #980 into 17.0
Signed-off-by pedrobaeza
2 parents 7181858 + dbc7e74 commit 621dcc1

25 files changed

+1546
-0
lines changed

report_csv/README.rst

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
===============
2+
Base report csv
3+
===============
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:a39959a00912416454f8818d70b9a3ffdd305aeb1b1c35ce3ea007f3c41526bd
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github
20+
:target: https://github.com/OCA/reporting-engine/tree/17.0/report_csv
21+
:alt: OCA/reporting-engine
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/reporting-engine-17-0/reporting-engine-17-0-report_csv
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/reporting-engine&target_branch=17.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module provides a basic report class to generate csv report.
32+
33+
**Table of contents**
34+
35+
.. contents::
36+
:local:
37+
38+
Configuration
39+
=============
40+
41+
In case the exported CSV report should be encoded in another system than
42+
UTF-8, following fields of the report record (*Settings > Technical >
43+
Reports*) should be populated accordingly.
44+
45+
- Encoding: set an encoding system (such as cp932)
46+
- Encode Error Handling: select 'Ignore' or 'Replace' as necessary.
47+
48+
- 'Ignore': in case of an encoding error, the problematic character
49+
will be removed from the exported file.
50+
- 'Replace': in case of an encoding error, the problematic character
51+
will be replaced with '?' symbol.
52+
- Leaving the field blank: in case of an encoding error, the report
53+
generation fails with an error message.
54+
55+
Usage
56+
=====
57+
58+
An example of CSV report for partners on a module called
59+
\`module_name\`:
60+
61+
A python class :
62+
63+
::
64+
65+
from odoo import models
66+
67+
class PartnerCSV(models.AbstractModel):
68+
_name = 'report.report_csv.partner_csv'
69+
_inherit = 'report.report_csv.abstract'
70+
71+
def generate_csv_report(self, writer, data, partners):
72+
writer.writeheader()
73+
for obj in partners:
74+
writer.writerow({
75+
'name': obj.name,
76+
'email': obj.email,
77+
})
78+
79+
def csv_report_options(self):
80+
res = super().csv_report_options()
81+
res['fieldnames'].append('name')
82+
res['fieldnames'].append('email')
83+
res['delimiter'] = ';'
84+
res['quoting'] = csv.QUOTE_ALL
85+
return res
86+
87+
A report XML record :
88+
89+
::
90+
91+
<report
92+
id="partner_csv"
93+
model="res.partner"
94+
string="Print to CSV"
95+
report_type="csv"
96+
name="module_name.report_name"
97+
file="res_partner"
98+
attachment_use="False"
99+
/>
100+
101+
Update encoding with an appropriate value (e.g. cp932) as necessary.
102+
103+
Bug Tracker
104+
===========
105+
106+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/reporting-engine/issues>`_.
107+
In case of trouble, please check there if your issue has already been reported.
108+
If you spotted it first, help us to smash it by providing a detailed and welcomed
109+
`feedback <https://github.com/OCA/reporting-engine/issues/new?body=module:%20report_csv%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
110+
111+
Do not contact contributors directly about support or help with technical issues.
112+
113+
Credits
114+
=======
115+
116+
Authors
117+
-------
118+
119+
* Creu Blanca
120+
121+
Contributors
122+
------------
123+
124+
- Enric Tobella <etobella@creublanca.es>
125+
- Jaime Arroyo <jaime.arroyo@creublanca.es>
126+
- Rattapong Chokmasermkul <rattapongc@ecosoft.co.th>
127+
- `Quartile <https://www.quartile.co>`__:
128+
129+
- Aung Ko Ko Lin
130+
131+
Maintainers
132+
-----------
133+
134+
This module is maintained by the OCA.
135+
136+
.. image:: https://odoo-community.org/logo.png
137+
:alt: Odoo Community Association
138+
:target: https://odoo-community.org
139+
140+
OCA, or the Odoo Community Association, is a nonprofit organization whose
141+
mission is to support the collaborative development of Odoo features and
142+
promote its widespread use.
143+
144+
This module is part of the `OCA/reporting-engine <https://github.com/OCA/reporting-engine/tree/17.0/report_csv>`_ project on GitHub.
145+
146+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

report_csv/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import controllers
2+
from . import models
3+
from . import report

report_csv/__manifest__.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2019 Creu Blanca
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
3+
{
4+
"name": "Base report csv",
5+
"summary": "Base module to create csv report",
6+
"author": "Creu Blanca, Odoo Community Association (OCA)",
7+
"website": "https://github.com/OCA/reporting-engine",
8+
"category": "Reporting",
9+
"version": "17.0.1.0.0",
10+
"license": "AGPL-3",
11+
"depends": ["base", "web"],
12+
"demo": ["demo/report.xml"],
13+
"data": ["views/ir_actions_views.xml"],
14+
"assets": {
15+
"web.assets_backend": [
16+
"report_csv/static/src/js/report/qwebactionmanager.esm.js"
17+
]
18+
},
19+
"installable": True,
20+
}

report_csv/controllers/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import main

report_csv/controllers/main.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Copyright (C) 2019 Creu Blanca
2+
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
3+
4+
import json
5+
import logging
6+
7+
from werkzeug.exceptions import InternalServerError
8+
from werkzeug.urls import url_decode
9+
10+
from odoo.http import (
11+
content_disposition,
12+
request,
13+
route,
14+
)
15+
from odoo.http import (
16+
serialize_exception as _serialize_exception,
17+
)
18+
from odoo.tools import html_escape
19+
from odoo.tools.safe_eval import safe_eval, time
20+
21+
from odoo.addons.web.controllers import report
22+
23+
_logger = logging.getLogger(__name__)
24+
25+
26+
class ReportController(report.ReportController):
27+
@route()
28+
def report_routes(self, reportname, docids=None, converter=None, **data):
29+
if converter == "csv":
30+
report = request.env["ir.actions.report"]._get_report_from_name(reportname)
31+
context = dict(request.env.context)
32+
if docids:
33+
docids = [int(i) for i in docids.split(",")]
34+
if data.get("options"):
35+
data.update(json.loads(data.pop("options")))
36+
if data.get("context"):
37+
# Ignore 'lang' here, because the context in data is the one
38+
# from the webclient *but* if the user explicitely wants to
39+
# change the lang, this mechanism overwrites it.
40+
data["context"] = json.loads(data["context"])
41+
if data["context"].get("lang"):
42+
del data["context"]["lang"]
43+
context.update(data["context"])
44+
csv = report.with_context(**context)._render_csv(
45+
reportname, docids, data=data
46+
)[0]
47+
csvhttpheaders = [
48+
("Content-Type", "text/csv"),
49+
("Content-Length", len(csv)),
50+
]
51+
return request.make_response(csv, headers=csvhttpheaders)
52+
return super().report_routes(reportname, docids, converter, **data)
53+
54+
@route()
55+
def report_download(self, data, context=None, token=None):
56+
requestcontent = json.loads(data)
57+
url, report_type = requestcontent[0], requestcontent[1]
58+
reportname = ""
59+
try:
60+
if report_type == "csv":
61+
reportname = url.split("/report/csv/")[1].split("?")[0]
62+
docids = None
63+
if "/" in reportname:
64+
reportname, docids = reportname.split("/")
65+
if docids:
66+
# Generic report:
67+
response = self.report_routes(
68+
reportname, docids=docids, converter="csv", context=context
69+
)
70+
else:
71+
# Particular report:
72+
data = dict(
73+
url_decode(url.split("?")[1]).items()
74+
) # decoding the args represented in JSON
75+
if "context" in data:
76+
context, data_context = (
77+
json.loads(context or "{}"),
78+
json.loads(data.pop("context")),
79+
)
80+
context = json.dumps({**context, **data_context})
81+
response = self.report_routes(
82+
reportname, converter="csv", context=context, **data
83+
)
84+
85+
report = request.env["ir.actions.report"]._get_report_from_name(
86+
reportname
87+
)
88+
filename = f"{report.name}.csv"
89+
90+
if docids:
91+
ids = [int(x) for x in docids.split(",")]
92+
obj = request.env[report.model].browse(ids)
93+
if report.print_report_name and not len(obj) > 1:
94+
report_name = safe_eval(
95+
report.print_report_name, {"object": obj, "time": time}
96+
)
97+
filename = f"{report_name}.csv"
98+
response.headers.add(
99+
"Content-Disposition", content_disposition(filename)
100+
)
101+
return response
102+
else:
103+
return super().report_download(data, context, token=token)
104+
except Exception as e:
105+
_logger.exception("Error while generating report %s", reportname)
106+
se = _serialize_exception(e)
107+
error = {"code": 200, "message": "Odoo Server Error", "data": se}
108+
res = request.make_response(html_escape(json.dumps(error)))
109+
raise InternalServerError(response=res) from e

report_csv/demo/report.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<odoo>
3+
<!--
4+
Copyright 2019 Creu Blanca
5+
License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
6+
-->
7+
<record id="partner_csv" model="ir.actions.report">
8+
<field name="name">Print to CSV</field>
9+
<field name="model">res.partner</field>
10+
<field name="report_type">csv</field>
11+
<field name="report_name">report_csv.partner_csv</field>
12+
<field name="report_file">res_partner</field>
13+
</record>
14+
</odoo>

0 commit comments

Comments
 (0)