Browse Source

[IMP] report_csv: use dialects

pull/272/head
Jordi Ballester Alomar 6 years ago
committed by Enric Tobella
parent
commit
cd3816bcc5
  1. 13
      report_csv/README.rst
  2. 6
      report_csv/report/report_csv.py
  3. 10
      report_csv/report/report_partner_csv.py
  4. 3
      report_csv/tests/test_report.py

13
report_csv/README.rst

@ -18,22 +18,27 @@ A python class ::
from odoo import models
class PartnerCsv(models.AbstractModel):
_name = 'report.module_name.report_name'
class PartnerCSV(models.AbstractModel):
_name = 'report.report_csv.partner_csv'
_inherit = 'report.report_csv.abstract'
def generate_csv_report(self, writer, data, partners):
for obj in partners:
writer.writeheader()
for obj in partners:
writer.writerow({
'name': obj.name
'name': obj.name,
'email': obj.email,
})
def csv_report_options(self):
res = super().csv_report_options()
res['fieldnames'].append('name')
res['fieldnames'].append('email')
res['delimiter'] = ';'
res['quoting'] = csv.QUOTE_ALL
return res
A report XML record ::
<report

6
report_csv/report/report_csv.py

@ -48,6 +48,12 @@ class ReportCSVAbstract(models.AbstractModel):
return file_data.read(), 'csv'
def csv_report_options(self):
"""
:return: dictionary of parameters. At least return 'fieldnames', but
you can optionally return parameters that define the export format.
Valid parameters include 'delimiter', 'quotechar', 'escapechar',
'doublequote', 'skipinitialspace', 'lineterminator', 'quoting'.
"""
return {'fieldnames': []}
def generate_csv_report(self, file, data, objs):

10
report_csv/report/report_partner_csv.py

@ -1,6 +1,6 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import csv
from odoo import models
@ -9,13 +9,17 @@ class PartnerCSV(models.AbstractModel):
_inherit = 'report.report_csv.abstract'
def generate_csv_report(self, writer, data, partners):
for obj in partners:
writer.writeheader()
for obj in partners:
writer.writerow({
'name': obj.name
'name': obj.name,
'email': obj.email,
})
def csv_report_options(self):
res = super().csv_report_options()
res['fieldnames'].append('name')
res['fieldnames'].append('email')
res['delimiter'] = ';'
res['quoting'] = csv.QUOTE_ALL
return res

3
report_csv/tests/test_report.py

@ -27,7 +27,8 @@ class TestReport(common.TransactionCase):
self.assertEqual(report.report_type, 'csv')
rep = report.render(self.docs.ids, {})
str_io = StringIO(rep[0])
dict_report = list(csv.DictReader(str_io))
dict_report = list(csv.DictReader(str_io, delimiter=';',
quoting=csv.QUOTE_ALL))
self.assertEqual(self.docs.name, dict(dict_report[0])['name'])
def test_id_retrieval(self):

Loading…
Cancel
Save