[MIG] report_xlsx: Migration to 12.0
* refactor: On new ActionManager override _makeReportUrls and triggerDownload to support XLSX format * pass action to custom _downloadReportXLSX: Need action to get report data * dict constructor to remove context langpull/690/head
parent
095c321a3d
commit
68767c3fa8
|
@ -31,7 +31,7 @@ A python class ::
|
|||
class PartnerXlsx(models.AbstractModel):
|
||||
_name = 'report.module_name.report_name'
|
||||
_inherit = 'report.report_xlsx.abstract'
|
||||
|
||||
|
||||
def generate_xlsx_report(self, workbook, data, partners):
|
||||
for obj in partners:
|
||||
report_name = obj.name
|
||||
|
@ -45,7 +45,7 @@ To manipulate the ``workbook`` and ``sheet`` objects, refer to the
|
|||
|
||||
A report XML record ::
|
||||
|
||||
<report
|
||||
<report
|
||||
id="partner_xlsx"
|
||||
model="res.partner"
|
||||
string="Print to XLSX"
|
||||
|
@ -57,7 +57,7 @@ A report XML record ::
|
|||
|
||||
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
|
||||
:alt: Try me on Runbot
|
||||
:target: https://runbot.odoo-community.org/runbot/143/11.0
|
||||
:target: https://runbot.odoo-community.org/runbot/143/12.0
|
||||
|
||||
Bug Tracker
|
||||
===========
|
||||
|
@ -75,6 +75,7 @@ Contributors
|
|||
|
||||
* Adrien Peiffer <adrien.peiffer@acsone.eu>
|
||||
* Enric Tobella <etobella@creublanca.es>
|
||||
* Cristian Salamea <cs@prisehub.com>
|
||||
|
||||
Maintainer
|
||||
----------
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
'Odoo Community Association (OCA)',
|
||||
'website': "https://github.com/oca/reporting-engine",
|
||||
'category': 'Reporting',
|
||||
'version': '11.0.1.0.3',
|
||||
'version': '12.0.1.0.0',
|
||||
'license': 'AGPL-3',
|
||||
'external_dependencies': {
|
||||
'python': [
|
||||
|
|
|
@ -6,28 +6,30 @@ from odoo.exceptions import UserError
|
|||
|
||||
|
||||
class ReportAction(models.Model):
|
||||
_inherit = 'ir.actions.report'
|
||||
_inherit = "ir.actions.report"
|
||||
|
||||
report_type = fields.Selection(selection_add=[("xlsx", "xlsx")])
|
||||
report_type = fields.Selection(selection_add=[("xlsx", "XLSX")])
|
||||
|
||||
@api.model
|
||||
def render_xlsx(self, docids, data):
|
||||
report_model_name = 'report.%s' % self.report_name
|
||||
report_model_name = "report.%s" % self.report_name
|
||||
report_model = self.env.get(report_model_name)
|
||||
if report_model is None:
|
||||
raise UserError(_('%s model was not found' % report_model_name))
|
||||
return report_model.with_context({
|
||||
'active_model': self.model
|
||||
}).create_xlsx_report(docids, data)
|
||||
raise UserError(_("%s model was not found" % report_model_name))
|
||||
return report_model.with_context(active_model=self.model).create_xlsx_report( # noqa
|
||||
docids, data
|
||||
)
|
||||
|
||||
@api.model
|
||||
def _get_report_from_name(self, report_name):
|
||||
res = super(ReportAction, self)._get_report_from_name(report_name)
|
||||
if res:
|
||||
return res
|
||||
report_obj = self.env['ir.actions.report']
|
||||
qwebtypes = ['xlsx']
|
||||
conditions = [('report_type', 'in', qwebtypes),
|
||||
('report_name', '=', report_name)]
|
||||
context = self.env['res.users'].context_get()
|
||||
report_obj = self.env["ir.actions.report"]
|
||||
qwebtypes = ["xlsx"]
|
||||
conditions = [
|
||||
("report_type", "in", qwebtypes),
|
||||
("report_name", "=", report_name),
|
||||
]
|
||||
context = self.env["res.users"].context_get()
|
||||
return report_obj.with_context(context).search(conditions, limit=1)
|
||||
|
|
|
@ -16,6 +16,7 @@ except ImportError:
|
|||
|
||||
class ReportXlsxAbstract(models.AbstractModel):
|
||||
_name = 'report.report_xlsx.abstract'
|
||||
_description = 'Abstract XLSX Report'
|
||||
|
||||
def _get_objs_for_report(self, docids, data):
|
||||
"""
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
// © 2017 Creu Blanca
|
||||
// License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
||||
odoo.define("report_xlsx.report", function (require) {
|
||||
"use strict";
|
||||
|
||||
var core = require("web.core");
|
||||
var ActionManager = require("web.ActionManager");
|
||||
var crash_manager = require("web.crash_manager");
|
||||
var framework = require("web.framework");
|
||||
var session = require("web.session");
|
||||
var _t = core._t;
|
||||
|
||||
ActionManager.include({
|
||||
|
||||
_downloadReportXLSX: function (url, actions) {
|
||||
framework.blockUI();
|
||||
var def = $.Deferred();
|
||||
var type = "xlsx";
|
||||
var cloned_action = _.clone(actions);
|
||||
|
||||
if (_.isUndefined(cloned_action.data) ||
|
||||
_.isNull(cloned_action.data) ||
|
||||
(_.isObject(cloned_action.data) && _.isEmpty(cloned_action.data)))
|
||||
{
|
||||
if (cloned_action.context.active_ids) {
|
||||
url += "/" + cloned_action.context.active_ids.join(',');
|
||||
}
|
||||
} else {
|
||||
url += "?options=" + encodeURIComponent(JSON.stringify(cloned_action.data));
|
||||
url += "&context=" + encodeURIComponent(JSON.stringify(cloned_action.context));
|
||||
}
|
||||
|
||||
var blocked = !session.get_file({
|
||||
url: url,
|
||||
data: {
|
||||
data: JSON.stringify([url, type]),
|
||||
},
|
||||
success: def.resolve.bind(def),
|
||||
error: function () {
|
||||
crash_manager.rpc_error.apply(crash_manager, arguments);
|
||||
def.reject();
|
||||
},
|
||||
complete: framework.unblockUI,
|
||||
});
|
||||
if (blocked) {
|
||||
// AAB: this check should be done in get_file service directly,
|
||||
// should not be the concern of the caller (and that way, get_file
|
||||
// could return a deferred)
|
||||
var message = _t('A popup window with your report was blocked. You ' +
|
||||
'may need to change your browser settings to allow ' +
|
||||
'popup windows for this page.');
|
||||
this.do_warn(_t('Warning'), message, true);
|
||||
}
|
||||
return def;
|
||||
},
|
||||
|
||||
_triggerDownload: function (action, options, type) {
|
||||
var self = this;
|
||||
var reportUrls = this._makeReportUrls(action);
|
||||
if (type === "xlsx") {
|
||||
return this._downloadReportXLSX(reportUrls[type], action).then(function () {
|
||||
if (action.close_on_report_download) {
|
||||
var closeAction = {type: 'ir.actions.act_window_close'};
|
||||
return self.doAction(closeAction, _.pick(options, 'on_close'));
|
||||
} else {
|
||||
return options.on_close();
|
||||
}
|
||||
});
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
|
||||
_makeReportUrls: function (action) {
|
||||
var reportUrls = this._super.apply(this, arguments);
|
||||
reportUrls.xlsx = '/report/xlsx/' + action.report_name;
|
||||
return reportUrls;
|
||||
},
|
||||
|
||||
_executeReportAction: function (action, options) {
|
||||
var self = this;
|
||||
if (action.report_type === 'xlsx') {
|
||||
return self._triggerDownload(action, options, 'xlsx');
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
|
@ -1,47 +0,0 @@
|
|||
// © 2017 Creu Blanca
|
||||
// License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
||||
odoo.define('report_xlsx.report', function(require){
|
||||
'use strict';
|
||||
|
||||
var ActionManager= require('web.ActionManager');
|
||||
var crash_manager = require('web.crash_manager');
|
||||
var framework = require('web.framework');
|
||||
|
||||
ActionManager.include({
|
||||
ir_actions_report: function (action, options){
|
||||
var self = this;
|
||||
var cloned_action = _.clone(action);
|
||||
if (cloned_action.report_type === 'xlsx') {
|
||||
framework.blockUI();
|
||||
var report_xlsx_url = 'report/xlsx/' + cloned_action.report_name;
|
||||
if (_.isUndefined(cloned_action.data) ||
|
||||
_.isNull(cloned_action.data) ||
|
||||
(_.isObject(cloned_action.data) && _.isEmpty(cloned_action.data)))
|
||||
{
|
||||
if(cloned_action.context.active_ids) {
|
||||
report_xlsx_url += '/' + cloned_action.context.active_ids.join(',');
|
||||
}
|
||||
} else {
|
||||
report_xlsx_url += '?options=' + encodeURIComponent(JSON.stringify(cloned_action.data));
|
||||
report_xlsx_url += '&context=' + encodeURIComponent(JSON.stringify(cloned_action.context));
|
||||
}
|
||||
self.getSession().get_file({
|
||||
url: report_xlsx_url,
|
||||
data: {data: JSON.stringify([
|
||||
report_xlsx_url,
|
||||
cloned_action.report_type
|
||||
])},
|
||||
error: crash_manager.rpc_error.bind(crash_manager),
|
||||
success: function (){
|
||||
if(cloned_action && options && !cloned_action.dialog){
|
||||
options.on_close();
|
||||
}
|
||||
}
|
||||
});
|
||||
framework.unblockUI();
|
||||
return;
|
||||
}
|
||||
return self._super(action, options);
|
||||
}
|
||||
});
|
||||
});
|
|
@ -8,7 +8,7 @@ _logger = logging.getLogger(__name__)
|
|||
try:
|
||||
from xlrd import open_workbook
|
||||
except ImportError:
|
||||
_logger.debug('Can not import xlsxwriter`.')
|
||||
_logger.debug('Can not import xlrd`.')
|
||||
|
||||
|
||||
class TestReport(common.TransactionCase):
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
-->
|
||||
<template id="assets_backend" inherit_id="web.assets_backend">
|
||||
<xpath expr="." position="inside">
|
||||
<script type="text/javascript" src="/report_xlsx/static/src/js/report/qwebactionmanager.js"/>
|
||||
<script type="text/javascript" src="/report_xlsx/static/src/js/report/action_manager_report.js"/>
|
||||
</xpath>
|
||||
|
||||
</template>
|
||||
|
|
Loading…
Reference in New Issue