Browse Source

[IMP] report_xml: black, isort

myc-14.0-py3o
Tatiana Deribina 5 years ago
committed by Enric Tobella
parent
commit
e2869482c9
  1. 16
      report_xml/__manifest__.py
  2. 78
      report_xml/controllers/main.py
  3. 22
      report_xml/models/report_action.py
  4. 4
      report_xml/static/src/js/report/action_manager_report.js
  5. 11
      report_xml/tests/test_report_xml.py

16
report_xml/__manifest__.py

@ -5,20 +5,12 @@
"version": "12.0.1.0.0",
"category": "Reporting",
"website": "https://github.com/OCA/reporting-engine",
"author": "Tecnativa, "
"Odoo Community Association (OCA)",
"author": "Tecnativa, " "Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"application": False,
"summary": "Allow to generate XML reports",
"depends": [
"web",
],
"data": [
"views/webclient_templates.xml",
"views/ir_actions_views.xml",
],
"demo": [
"demo/report.xml",
]
"depends": ["web"],
"data": ["views/webclient_templates.xml", "views/ir_actions_views.xml"],
"demo": ["demo/report.xml"],
}

78
report_xml/controllers/main.py

@ -4,66 +4,71 @@
import json
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 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):
@route()
def report_routes(self, reportname, docids=None, converter=None, **data):
if converter == 'xml':
report = request.env['ir.actions.report']._get_report_from_name(
reportname)
if converter == "xml":
report = request.env["ir.actions.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'):
data.update(json.loads(data.pop('options')))
if data.get('context'):
docids = [int(i) for i in docids.split(",")]
if data.get("options"):
data.update(json.loads(data.pop("options")))
if data.get("context"):
# Ignore 'lang' here, because the context in data is the one
# from the webclient *but* if the user explicitely wants to
# change the lang, this mechanism overwrites it.
data['context'] = json.loads(data['context'])
if data['context'].get('lang'):
del data['context']['lang']
context.update(data['context'])
data["context"] = json.loads(data["context"])
if data["context"].get("lang"):
del data["context"]["lang"]
context.update(data["context"])
xml = report.with_context(context).render_qweb_xml(docids,
data=data)[0]
xmlhttpheaders = [('Content-Type', 'text/xml'),
('Content-Length', len(xml))]
xml = report.with_context(context).render_qweb_xml(docids, data=data)[0]
xmlhttpheaders = [
("Content-Type", "text/xml"),
("Content-Length", len(xml)),
]
return request.make_response(xml, headers=xmlhttpheaders)
else:
return super(ReportController, self).report_routes(
reportname, docids, converter, **data)
reportname, docids, converter, **data
)
@route()
def report_download(self, data, token):
requestcontent = json.loads(data)
url, report_type = requestcontent[0], requestcontent[1]
if report_type == 'qweb-xml':
if report_type == "qweb-xml":
try:
reportname = url.split('/report/xml/')[1].split('?')[0]
reportname = url.split("/report/xml/")[1].split("?")[0]
docids = None
if '/' in reportname:
reportname, docids = reportname.split('/')
if "/" in reportname:
reportname, docids = reportname.split("/")
if docids:
# Generic report:
response = self.report_routes(
reportname, docids=docids, converter='xml')
reportname, docids=docids, converter="xml"
)
else:
# Particular report:
# decoding the args represented in JSON
data = url_decode(url.split('?')[1]).items()
data = url_decode(url.split("?")[1]).items()
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)
filename = "%s.xml" % (report.name)
@ -71,21 +76,18 @@ class ReportController(report.ReportController):
ids = [int(x) for x in docids.split(",")]
records = request.env[report.model].browse(ids)
if report.print_report_name and not len(records) > 1:
report_name = safe_eval(report.print_report_name,
{'object': records,
'time': time})
report_name = safe_eval(
report.print_report_name, {"object": records, "time": time}
)
filename = "%s.xml" % (report_name)
response.headers.add('Content-Disposition',
content_disposition(filename))
response.set_cookie('fileToken', token)
response.headers.add(
"Content-Disposition", content_disposition(filename)
)
response.set_cookie("fileToken", token)
return response
except Exception as e:
se = serialize_exception(e)
error = {
'code': 200,
'message': "Odoo Server Error",
'data': se
}
error = {"code": 200, "message": "Odoo Server Error", "data": se}
return request.make_response(html_escape(json.dumps(error)))
else:
return super(ReportController, self).report_download(data, token)

22
report_xml/models/report_action.py

@ -1,9 +1,10 @@
# Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
from odoo import api, fields, models
from lxml import etree
from odoo import api, fields, models
class ReportAction(models.Model):
_inherit = "ir.actions.report"
@ -14,14 +15,17 @@ class ReportAction(models.Model):
def render_qweb_xml(self, docids, data=None):
if not data:
data = {}
data.setdefault('report_type', 'text')
data.setdefault("report_type", "text")
data = self._get_rendering_context(docids, data)
result = self.render_template(self.report_name, data)
return etree.tostring(
etree.fromstring(
str(result, 'UTF-8').lstrip('\n').lstrip().encode('UTF-8')
return (
etree.tostring(
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_declaration=True,
pretty_print=True
), "xml"
"xml",
)

4
report_xml/static/src/js/report/action_manager_report.js

@ -1,7 +1,7 @@
odoo.define('report_xml.ReportActionManager', function(require){
odoo.define('report_xml.ReportActionManager', function (require) {
'use strict';
var ActionManager = require('web.ActionManager');
var ActionManager = require('web.ActionManager');
ActionManager.include({
_executeReportAction: function (action, options) {

11
report_xml/tests/test_report_xml.py

@ -2,17 +2,18 @@
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl).
from lxml import etree
from odoo.tests import common
class TestXmlReport(common.TransactionCase):
def test_xml(self):
report_object = self.env['ir.actions.report']
report_name = 'report_xml.demo_report_xml_view'
report_object = self.env["ir.actions.report"]
report_name = "report_xml.demo_report_xml_view"
report = report_object._get_report_from_name(report_name)
docs = self.env['res.company'].search([], limit=1)
self.assertEqual(report.report_type, 'qweb-xml')
docs = self.env["res.company"].search([], limit=1)
self.assertEqual(report.report_type, "qweb-xml")
rep = report.render(docs.ids, {})
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)
Loading…
Cancel
Save