[MIG][IMP][13.0] rebase to include new linters

pull/554/head
Rod Schouteden 2019-10-22 09:56:07 +02:00 committed by Lois Rilo
parent 70ad9aff1f
commit 67ecf5c597
7 changed files with 90 additions and 98 deletions

View File

@ -1,30 +1,16 @@
# Copyright 2015 ACSONE SA/NV (<http://acsone.eu>) # Copyright 2015 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{ {
'name': "Base report xlsx", "name": "Base report xlsx",
"summary": "Base module to create xlsx report",
'summary': "Base module to create xlsx report", "author": "ACSONE SA/NV," "Creu Blanca," "Odoo Community Association (OCA)",
'author': 'ACSONE SA/NV,' "website": "https://github.com/oca/reporting-engine",
'Creu Blanca,' "category": "Reporting",
'Odoo Community Association (OCA)', "version": "13.0.1.0.0",
'website': "https://github.com/oca/reporting-engine", "license": "AGPL-3",
'category': 'Reporting', "external_dependencies": {"python": ["xlsxwriter", "xlrd"]},
'version': '13.0.1.0.0', "depends": ["base", "web"],
'license': 'AGPL-3', "data": ["views/webclient_templates.xml"],
'external_dependencies': { "demo": ["demo/report.xml"],
'python': [ "installable": True,
'xlsxwriter',
'xlrd',
],
},
'depends': [
'base', 'web',
],
'data': [
'views/webclient_templates.xml',
],
'demo': [
'demo/report.xml',
],
'installable': True,
} }

View File

@ -1,49 +1,48 @@
# Copyright (C) 2017 Creu Blanca # Copyright (C) 2017 Creu Blanca
# 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.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 json
import time import time
from odoo.http import content_disposition, request, route
from odoo.tools.safe_eval import 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 == 'xlsx': if converter == "xlsx":
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"])
xlsx = report.with_context(context).render_xlsx( xlsx = report.with_context(context).render_xlsx(docids, data=data)[0]
docids, data=data
)[0]
report_name = report.report_file report_name = report.report_file
if report.print_report_name and not len(docids) > 1: if report.print_report_name and not len(docids) > 1:
obj = request.env[report.model].browse(docids[0]) obj = request.env[report.model].browse(docids[0])
report_name = safe_eval(report.print_report_name, report_name = safe_eval(
{'object': obj, 'time': time}) report.print_report_name, {"object": obj, "time": time}
xlsxhttpheaders = [
('Content-Type', 'application/vnd.openxmlformats-'
'officedocument.spreadsheetml.sheet'),
('Content-Length', len(xlsx)),
(
'Content-Disposition',
content_disposition(report_name + '.xlsx')
) )
xlsxhttpheaders = [
(
"Content-Type",
"application/vnd.openxmlformats-"
"officedocument.spreadsheetml.sheet",
),
("Content-Length", len(xlsx)),
("Content-Disposition", content_disposition(report_name + ".xlsx")),
] ]
return request.make_response(xlsx, headers=xlsxhttpheaders) return request.make_response(xlsx, headers=xlsxhttpheaders)
return super(ReportController, self).report_routes( return super(ReportController, self).report_routes(

View File

@ -1,7 +1,7 @@
# Copyright 2015 ACSONE SA/NV (<http://acsone.eu>) # Copyright 2015 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models, _ from odoo import _, api, fields, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
@ -15,8 +15,10 @@ class ReportAction(models.Model):
report_model_name = "report.%s" % self.report_name report_model_name = "report.%s" % self.report_name
report_model = self.env.get(report_model_name) report_model = self.env.get(report_model_name)
if report_model is None: if report_model is None:
raise UserError(_('%s model was not found') % report_model_name) raise UserError(_("%s model was not found") % report_model_name)
return report_model.with_context(active_model=self.model).create_xlsx_report( # noqa return report_model.with_context(
active_model=self.model
).create_xlsx_report( # noqa
docids, data docids, data
) )

View File

@ -5,12 +5,12 @@ from odoo import models
class PartnerXlsx(models.AbstractModel): class PartnerXlsx(models.AbstractModel):
_name = 'report.report_xlsx.partner_xlsx' _name = "report.report_xlsx.partner_xlsx"
_inherit = 'report.report_xlsx.abstract' _inherit = "report.report_xlsx.abstract"
_description = 'Partner XLSX Report' _description = "Partner XLSX Report"
def generate_xlsx_report(self, workbook, data, partners): def generate_xlsx_report(self, workbook, data, partners):
for obj in partners: for obj in partners:
sheet = workbook.add_worksheet('Report') sheet = workbook.add_worksheet("Report")
bold = workbook.add_format({'bold': True}) bold = workbook.add_format({"bold": True})
sheet.write(0, 0, obj.name, bold) sheet.write(0, 0, obj.name, bold)

View File

@ -1,22 +1,22 @@
# Copyright 2015 ACSONE SA/NV (<http://acsone.eu>) # Copyright 2015 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import logging
from io import BytesIO from io import BytesIO
from odoo import models from odoo import models
import logging
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
try: try:
import xlsxwriter import xlsxwriter
except ImportError: except ImportError:
_logger.debug('Can not import xlsxwriter`.') _logger.debug("Can not import xlsxwriter`.")
class ReportXlsxAbstract(models.AbstractModel): class ReportXlsxAbstract(models.AbstractModel):
_name = 'report.report_xlsx.abstract' _name = "report.report_xlsx.abstract"
_description = 'Abstract XLSX Report' _description = "Abstract XLSX Report"
def _get_objs_for_report(self, docids, data): def _get_objs_for_report(self, docids, data):
""" """
@ -34,11 +34,11 @@ class ReportXlsxAbstract(models.AbstractModel):
""" """
if docids: if docids:
ids = docids ids = docids
elif data and 'context' in data: elif data and "context" in data:
ids = data["context"].get('active_ids', []) ids = data["context"].get("active_ids", [])
else: else:
ids = self.env.context.get('active_ids', []) ids = self.env.context.get("active_ids", [])
return self.env[self.env.context.get('active_model')].browse(ids) return self.env[self.env.context.get("active_model")].browse(ids)
def create_xlsx_report(self, docids, data): def create_xlsx_report(self, docids, data):
objs = self._get_objs_for_report(docids, data) objs = self._get_objs_for_report(docids, data)
@ -47,7 +47,7 @@ class ReportXlsxAbstract(models.AbstractModel):
self.generate_xlsx_report(workbook, data, objs) self.generate_xlsx_report(workbook, data, objs)
workbook.close() workbook.close()
file_data.seek(0) file_data.seek(0)
return file_data.read(), 'xlsx' return file_data.read(), "xlsx"
def get_workbook_options(self): def get_workbook_options(self):
""" """

View File

@ -17,23 +17,25 @@ odoo.define("report_xlsx.report", function (require) {
var def = $.Deferred(); var def = $.Deferred();
var type = "xlsx"; var type = "xlsx";
var cloned_action = _.clone(actions); var cloned_action = _.clone(actions);
var new_url = url;
if (_.isUndefined(cloned_action.data) || if (_.isUndefined(cloned_action.data) ||
_.isNull(cloned_action.data) || _.isNull(cloned_action.data) ||
(_.isObject(cloned_action.data) && _.isEmpty(cloned_action.data))) (_.isObject(cloned_action.data) && _.isEmpty(cloned_action.data))) {
{
if (cloned_action.context.active_ids) { if (cloned_action.context.active_ids) {
url += "/" + cloned_action.context.active_ids.join(','); new_url += "/" + cloned_action.context.active_ids.join(',');
} }
} else { } else {
url += "?options=" + encodeURIComponent(JSON.stringify(cloned_action.data)); new_url += "?options=" + encodeURIComponent(
url += "&context=" + encodeURIComponent(JSON.stringify(cloned_action.context)); JSON.stringify(cloned_action.data));
new_url += "&context=" + encodeURIComponent(
JSON.stringify(cloned_action.context));
} }
var blocked = !session.get_file({ var blocked = !session.get_file({
url: url, url: new_url,
data: { data: {
data: JSON.stringify([url, type]), data: JSON.stringify([new_url, type]),
}, },
success: def.resolve.bind(def), success: def.resolve.bind(def),
error: function () { error: function () {
@ -58,13 +60,14 @@ odoo.define("report_xlsx.report", function (require) {
var self = this; var self = this;
var reportUrls = this._makeReportUrls(action); var reportUrls = this._makeReportUrls(action);
if (type === "xlsx") { if (type === "xlsx") {
return this._downloadReportXLSX(reportUrls[type], action).then(function () { return this._downloadReportXLSX(reportUrls[type], action).then(
function () {
if (action.close_on_report_download) { if (action.close_on_report_download) {
var closeAction = {type: 'ir.actions.act_window_close'}; var closeAction = {type: 'ir.actions.act_window_close'};
return self.doAction(closeAction, _.pick(options, 'on_close')); return self.doAction(closeAction, _.pick(
} else { options, 'on_close'));
return options.on_close();
} }
return options.on_close();
}); });
} }
return this._super.apply(this, arguments); return this._super.apply(this, arguments);
@ -82,7 +85,7 @@ odoo.define("report_xlsx.report", function (require) {
return self._triggerDownload(action, options, 'xlsx'); return self._triggerDownload(action, options, 'xlsx');
} }
return this._super.apply(this, arguments); return this._super.apply(this, arguments);
} },
}); });
}); });

View File

@ -1,31 +1,32 @@
# Copyright 2017 Creu Blanca # Copyright 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo.tests import common
import logging import logging
from odoo.tests import common
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
try: try:
from xlrd import open_workbook from xlrd import open_workbook
except ImportError: except ImportError:
_logger.debug('Can not import xlrd`.') _logger.debug("Can not import xlrd`.")
class TestReport(common.TransactionCase): class TestReport(common.TransactionCase):
def setUp(self): def setUp(self):
super(TestReport, self).setUp() super(TestReport, self).setUp()
report_object = self.env['ir.actions.report'] report_object = self.env["ir.actions.report"]
self.xlsx_report = ( self.xlsx_report = self.env["report.report_xlsx.abstract"].with_context(
self.env['report.report_xlsx.abstract'] active_model="res.partner"
.with_context(active_model='res.partner')
) )
self.report_name = 'report_xlsx.partner_xlsx' self.report_name = "report_xlsx.partner_xlsx"
self.report = report_object._get_report_from_name(self.report_name) self.report = report_object._get_report_from_name(self.report_name)
self.docs = self.env['res.company'].search([], limit=1).partner_id self.docs = self.env["res.company"].search([], limit=1).partner_id
def test_report(self): def test_report(self):
report = self.report report = self.report
self.assertEqual(report.report_type, 'xlsx') self.assertEqual(report.report_type, "xlsx")
rep = report.render(self.docs.ids, {}) rep = report.render(self.docs.ids, {})
wb = open_workbook(file_contents=rep[0]) wb = open_workbook(file_contents=rep[0])
sheet = wb.sheet_by_index(0) sheet = wb.sheet_by_index(0)
@ -35,18 +36,19 @@ class TestReport(common.TransactionCase):
# Typical call from WebUI with wizard # Typical call from WebUI with wizard
objs = self.xlsx_report._get_objs_for_report( objs = self.xlsx_report._get_objs_for_report(
False, {"context": {"active_ids": self.docs.ids}}) False, {"context": {"active_ids": self.docs.ids}}
)
self.assertEquals(objs, self.docs) self.assertEquals(objs, self.docs)
# Typical call from within code not to report_action # Typical call from within code not to report_action
objs = self.xlsx_report.with_context( objs = self.xlsx_report.with_context(
active_ids=self.docs.ids)._get_objs_for_report(False, False) active_ids=self.docs.ids
)._get_objs_for_report(False, False)
self.assertEquals(objs, self.docs) self.assertEquals(objs, self.docs)
# Typical call from WebUI # Typical call from WebUI
objs = self.xlsx_report._get_objs_for_report( objs = self.xlsx_report._get_objs_for_report(
self.docs.ids, self.docs.ids, {"data": [self.report_name, self.report.report_type]}
{"data": [self.report_name, self.report.report_type]}
) )
self.assertEquals(objs, self.docs) self.assertEquals(objs, self.docs)