mirror of
https://github.com/OCA/reporting-engine.git
synced 2025-02-16 16:30:38 +02:00
[IMP] report_xml: black, isort
This commit is contained in:
committed by
Enric Tobella
parent
fec3d83024
commit
e2869482c9
@@ -5,20 +5,12 @@
|
|||||||
"version": "12.0.1.0.0",
|
"version": "12.0.1.0.0",
|
||||||
"category": "Reporting",
|
"category": "Reporting",
|
||||||
"website": "https://github.com/OCA/reporting-engine",
|
"website": "https://github.com/OCA/reporting-engine",
|
||||||
"author": "Tecnativa, "
|
"author": "Tecnativa, " "Odoo Community Association (OCA)",
|
||||||
"Odoo Community Association (OCA)",
|
|
||||||
"license": "AGPL-3",
|
"license": "AGPL-3",
|
||||||
"installable": True,
|
"installable": True,
|
||||||
"application": False,
|
"application": False,
|
||||||
"summary": "Allow to generate XML reports",
|
"summary": "Allow to generate XML reports",
|
||||||
"depends": [
|
"depends": ["web"],
|
||||||
"web",
|
"data": ["views/webclient_templates.xml", "views/ir_actions_views.xml"],
|
||||||
],
|
"demo": ["demo/report.xml"],
|
||||||
"data": [
|
|
||||||
"views/webclient_templates.xml",
|
|
||||||
"views/ir_actions_views.xml",
|
|
||||||
],
|
|
||||||
"demo": [
|
|
||||||
"demo/report.xml",
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,66 +4,71 @@
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from odoo.addons.web.controllers import main as report
|
|
||||||
from odoo.http import content_disposition, request, route, serialize_exception
|
|
||||||
from odoo.tools import safe_eval, html_escape
|
|
||||||
from werkzeug.urls import url_decode
|
from werkzeug.urls import url_decode
|
||||||
|
|
||||||
|
from odoo.http import content_disposition, request, route, serialize_exception
|
||||||
|
from odoo.tools import html_escape, safe_eval
|
||||||
|
|
||||||
|
from odoo.addons.web.controllers import main as report
|
||||||
|
|
||||||
|
|
||||||
class ReportController(report.ReportController):
|
class ReportController(report.ReportController):
|
||||||
@route()
|
@route()
|
||||||
def report_routes(self, reportname, docids=None, converter=None, **data):
|
def report_routes(self, reportname, docids=None, converter=None, **data):
|
||||||
if converter == 'xml':
|
if converter == "xml":
|
||||||
report = request.env['ir.actions.report']._get_report_from_name(
|
report = request.env["ir.actions.report"]._get_report_from_name(reportname)
|
||||||
reportname)
|
|
||||||
context = dict(request.env.context)
|
context = dict(request.env.context)
|
||||||
|
|
||||||
if docids:
|
if docids:
|
||||||
docids = [int(i) for i in docids.split(',')]
|
docids = [int(i) for i in docids.split(",")]
|
||||||
if data.get('options'):
|
if data.get("options"):
|
||||||
data.update(json.loads(data.pop('options')))
|
data.update(json.loads(data.pop("options")))
|
||||||
if data.get('context'):
|
if data.get("context"):
|
||||||
# Ignore 'lang' here, because the context in data is the one
|
# Ignore 'lang' here, because the context in data is the one
|
||||||
# from the webclient *but* if the user explicitely wants to
|
# from the webclient *but* if the user explicitely wants to
|
||||||
# change the lang, this mechanism overwrites it.
|
# change the lang, this mechanism overwrites it.
|
||||||
data['context'] = json.loads(data['context'])
|
data["context"] = json.loads(data["context"])
|
||||||
if data['context'].get('lang'):
|
if data["context"].get("lang"):
|
||||||
del data['context']['lang']
|
del data["context"]["lang"]
|
||||||
context.update(data['context'])
|
context.update(data["context"])
|
||||||
|
|
||||||
xml = report.with_context(context).render_qweb_xml(docids,
|
xml = report.with_context(context).render_qweb_xml(docids, data=data)[0]
|
||||||
data=data)[0]
|
xmlhttpheaders = [
|
||||||
xmlhttpheaders = [('Content-Type', 'text/xml'),
|
("Content-Type", "text/xml"),
|
||||||
('Content-Length', len(xml))]
|
("Content-Length", len(xml)),
|
||||||
|
]
|
||||||
return request.make_response(xml, headers=xmlhttpheaders)
|
return request.make_response(xml, headers=xmlhttpheaders)
|
||||||
else:
|
else:
|
||||||
return super(ReportController, self).report_routes(
|
return super(ReportController, self).report_routes(
|
||||||
reportname, docids, converter, **data)
|
reportname, docids, converter, **data
|
||||||
|
)
|
||||||
|
|
||||||
@route()
|
@route()
|
||||||
def report_download(self, data, token):
|
def report_download(self, data, token):
|
||||||
requestcontent = json.loads(data)
|
requestcontent = json.loads(data)
|
||||||
url, report_type = requestcontent[0], requestcontent[1]
|
url, report_type = requestcontent[0], requestcontent[1]
|
||||||
if report_type == 'qweb-xml':
|
if report_type == "qweb-xml":
|
||||||
try:
|
try:
|
||||||
reportname = url.split('/report/xml/')[1].split('?')[0]
|
reportname = url.split("/report/xml/")[1].split("?")[0]
|
||||||
|
|
||||||
docids = None
|
docids = None
|
||||||
if '/' in reportname:
|
if "/" in reportname:
|
||||||
reportname, docids = reportname.split('/')
|
reportname, docids = reportname.split("/")
|
||||||
|
|
||||||
if docids:
|
if docids:
|
||||||
# Generic report:
|
# Generic report:
|
||||||
response = self.report_routes(
|
response = self.report_routes(
|
||||||
reportname, docids=docids, converter='xml')
|
reportname, docids=docids, converter="xml"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# Particular report:
|
# Particular report:
|
||||||
# decoding the args represented in JSON
|
# decoding the args represented in JSON
|
||||||
data = url_decode(url.split('?')[1]).items()
|
data = url_decode(url.split("?")[1]).items()
|
||||||
response = self.report_routes(
|
response = self.report_routes(
|
||||||
reportname, converter='xml', **dict(data))
|
reportname, converter="xml", **dict(data)
|
||||||
|
)
|
||||||
|
|
||||||
report_obj = request.env['ir.actions.report']
|
report_obj = request.env["ir.actions.report"]
|
||||||
report = report_obj._get_report_from_name(reportname)
|
report = report_obj._get_report_from_name(reportname)
|
||||||
filename = "%s.xml" % (report.name)
|
filename = "%s.xml" % (report.name)
|
||||||
|
|
||||||
@@ -71,21 +76,18 @@ class ReportController(report.ReportController):
|
|||||||
ids = [int(x) for x in docids.split(",")]
|
ids = [int(x) for x in docids.split(",")]
|
||||||
records = request.env[report.model].browse(ids)
|
records = request.env[report.model].browse(ids)
|
||||||
if report.print_report_name and not len(records) > 1:
|
if report.print_report_name and not len(records) > 1:
|
||||||
report_name = safe_eval(report.print_report_name,
|
report_name = safe_eval(
|
||||||
{'object': records,
|
report.print_report_name, {"object": records, "time": time}
|
||||||
'time': time})
|
)
|
||||||
filename = "%s.xml" % (report_name)
|
filename = "%s.xml" % (report_name)
|
||||||
response.headers.add('Content-Disposition',
|
response.headers.add(
|
||||||
content_disposition(filename))
|
"Content-Disposition", content_disposition(filename)
|
||||||
response.set_cookie('fileToken', token)
|
)
|
||||||
|
response.set_cookie("fileToken", token)
|
||||||
return response
|
return response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
se = serialize_exception(e)
|
se = serialize_exception(e)
|
||||||
error = {
|
error = {"code": 200, "message": "Odoo Server Error", "data": se}
|
||||||
'code': 200,
|
|
||||||
'message': "Odoo Server Error",
|
|
||||||
'data': se
|
|
||||||
}
|
|
||||||
return request.make_response(html_escape(json.dumps(error)))
|
return request.make_response(html_escape(json.dumps(error)))
|
||||||
else:
|
else:
|
||||||
return super(ReportController, self).report_download(data, token)
|
return super(ReportController, self).report_download(data, token)
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
|
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
|
||||||
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
||||||
|
|
||||||
from odoo import api, fields, models
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
|
from odoo import api, fields, models
|
||||||
|
|
||||||
|
|
||||||
class ReportAction(models.Model):
|
class ReportAction(models.Model):
|
||||||
_inherit = "ir.actions.report"
|
_inherit = "ir.actions.report"
|
||||||
@@ -14,14 +15,17 @@ class ReportAction(models.Model):
|
|||||||
def render_qweb_xml(self, docids, data=None):
|
def render_qweb_xml(self, docids, data=None):
|
||||||
if not data:
|
if not data:
|
||||||
data = {}
|
data = {}
|
||||||
data.setdefault('report_type', 'text')
|
data.setdefault("report_type", "text")
|
||||||
data = self._get_rendering_context(docids, data)
|
data = self._get_rendering_context(docids, data)
|
||||||
result = self.render_template(self.report_name, data)
|
result = self.render_template(self.report_name, data)
|
||||||
return etree.tostring(
|
return (
|
||||||
etree.fromstring(
|
etree.tostring(
|
||||||
str(result, 'UTF-8').lstrip('\n').lstrip().encode('UTF-8')
|
etree.fromstring(
|
||||||
|
str(result, "UTF-8").lstrip("\n").lstrip().encode("UTF-8")
|
||||||
|
),
|
||||||
|
encoding="UTF-8",
|
||||||
|
xml_declaration=True,
|
||||||
|
pretty_print=True,
|
||||||
),
|
),
|
||||||
encoding='UTF-8',
|
"xml",
|
||||||
xml_declaration=True,
|
)
|
||||||
pretty_print=True
|
|
||||||
), "xml"
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
odoo.define('report_xml.ReportActionManager', function(require){
|
odoo.define('report_xml.ReportActionManager', function (require) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ActionManager = require('web.ActionManager');
|
var ActionManager = require('web.ActionManager');
|
||||||
|
|
||||||
ActionManager.include({
|
ActionManager.include({
|
||||||
_executeReportAction: function (action, options) {
|
_executeReportAction: function (action, options) {
|
||||||
|
|||||||
@@ -2,17 +2,18 @@
|
|||||||
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl).
|
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl).
|
||||||
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
from odoo.tests import common
|
from odoo.tests import common
|
||||||
|
|
||||||
|
|
||||||
class TestXmlReport(common.TransactionCase):
|
class TestXmlReport(common.TransactionCase):
|
||||||
def test_xml(self):
|
def test_xml(self):
|
||||||
report_object = self.env['ir.actions.report']
|
report_object = self.env["ir.actions.report"]
|
||||||
report_name = 'report_xml.demo_report_xml_view'
|
report_name = "report_xml.demo_report_xml_view"
|
||||||
report = report_object._get_report_from_name(report_name)
|
report = report_object._get_report_from_name(report_name)
|
||||||
docs = self.env['res.company'].search([], limit=1)
|
docs = self.env["res.company"].search([], limit=1)
|
||||||
self.assertEqual(report.report_type, 'qweb-xml')
|
self.assertEqual(report.report_type, "qweb-xml")
|
||||||
rep = report.render(docs.ids, {})
|
rep = report.render(docs.ids, {})
|
||||||
root = etree.fromstring(rep[0])
|
root = etree.fromstring(rep[0])
|
||||||
el = root.xpath('/root/user/name')
|
el = root.xpath("/root/user/name")
|
||||||
self.assertEqual(el[0].text, docs.ensure_one().name)
|
self.assertEqual(el[0].text, docs.ensure_one().name)
|
||||||
|
|||||||
Reference in New Issue
Block a user