[MIG] report_csv: Backport from 11.0

This commit is contained in:
Laurent Mignon (ACSONE)
2020-07-29 10:41:37 +02:00
parent 29182742a6
commit 6fb8c68ff9
11 changed files with 144 additions and 73 deletions

View File

@@ -1,21 +1,29 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2019 Creu Blanca
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
from odoo.addons.web.controllers import main as report
from odoo.http import content_disposition, route, request
from odoo.tools.safe_eval import safe_eval
import json
import time
from werkzeug import url_decode
from odoo.addons.report.controllers import main as report
from odoo.http import route, request
from odoo.tools.safe_eval import safe_eval
from odoo.addons.web.controllers.main import (
_serialize_exception,
content_disposition
)
from odoo.tools import html_escape
class ReportController(report.ReportController):
@route()
def report_routes(self, reportname, docids=None, converter=None, **data):
if converter == 'csv':
report = request.env['ir.actions.report']._get_report_from_name(
report = request.env['report']._get_report_from_name(
reportname)
context = dict(request.env.context)
if docids:
docids = [int(i) for i in docids.split(',')]
if data.get('options'):
@@ -28,6 +36,7 @@ class ReportController(report.ReportController):
if data['context'].get('lang'):
del data['context']['lang']
context.update(data['context'])
csv = report.with_context(context).render_csv(
docids, data=data
)[0]
@@ -58,3 +67,43 @@ class ReportController(report.ReportController):
return super(ReportController, self).report_routes(
reportname, docids, converter, **data
)
@route()
def report_download(self, data, token):
"""This function is used by 'qwebactionmanager.js' in order to trigger
the download of a csv/controller report.
:param data: a javascript array JSON.stringified containg report
internal url ([0]) and type [1]
:returns: Response with a filetoken cookie and an attachment header
"""
requestcontent = json.loads(data)
url, type = requestcontent[0], requestcontent[1]
if type != 'csv':
return super(ReportController, self).report_download(data, token)
try:
reportname = url.split('/report/csv/')[1].split('?')[0]
docids = None
if '/' in reportname:
reportname, docids = reportname.split('/')
if docids:
# Generic report:
response = self.report_routes(
reportname, docids=docids, converter='csv')
else:
# Particular report:
# decoding the args represented in JSON
data = url_decode(url.split('?')[1]).items()
response = self.report_routes(
reportname, converter='csv', **dict(data))
response.set_cookie('fileToken', token)
return response
except Exception, e:
se = _serialize_exception(e)
error = {
'code': 200,
'message': "Odoo Server Error",
'data': se
}
return request.make_response(html_escape(json.dumps(error)))