Merge PR #269 into 14.0

Signed-off-by dreispt
This commit is contained in:
OCA-git-bot
2022-04-13 08:17:49 +00:00
3 changed files with 62 additions and 8 deletions

View File

@@ -4,10 +4,12 @@
# Copyright (C) 2011 Domsense srl (<http://www.domsense.com>) # Copyright (C) 2011 Domsense srl (<http://www.domsense.com>)
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>) # Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import time from time import time
from odoo import _, api, exceptions, fields, models from odoo import _, api, exceptions, fields, models
from odoo.tools import safe_eval from odoo.tools.safe_eval import safe_eval
REPORT_TYPES = {"qweb-pdf": "pdf", "qweb-text": "text"}
class IrActionsReport(models.Model): class IrActionsReport(models.Model):
@@ -100,9 +102,16 @@ class IrActionsReport(models.Model):
def print_document(self, record_ids, data=None): def print_document(self, record_ids, data=None):
"""Print a document, do not return the document file""" """Print a document, do not return the document file"""
document, doc_format = self.with_context( report_type = REPORT_TYPES.get(self.report_type)
must_skip_send_to_printer=True if not report_type:
)._render_qweb_pdf(record_ids, data=data) raise exceptions.UserError(
_("This report type (%s) is not supported by direct printing!")
% str(self.report_type)
)
method_name = "_render_qweb_%s" % (report_type)
document, doc_format = getattr(
self.with_context(must_skip_send_to_printer=True), method_name
)(record_ids, data=data)
behaviour = self.behaviour() behaviour = self.behaviour()
printer = behaviour.pop("printer", None) printer = behaviour.pop("printer", None)
@@ -160,3 +169,22 @@ class IrActionsReport(models.Model):
) )
return document, doc_format return document, doc_format
def _render_qweb_text(self, docids, data=None):
"""Generate a TEXT file and returns it.
If the action configured on the report is server, it prints the
generated document as well.
"""
document, doc_format = super()._render_qweb_text(docids=docids, data=data)
behaviour = self.behaviour()
printer = behaviour.pop("printer", None)
can_print_report = self._can_print_report(behaviour, printer, document)
if can_print_report:
printer.print_document(
self, document, doc_format=self.report_type, **behaviour
)
return document, doc_format

View File

@@ -9,7 +9,7 @@ odoo.define("base_report_to_printer.print", function (require) {
_triggerDownload: function (action, options, type) { _triggerDownload: function (action, options, type) {
var self = this; var self = this;
var _super = this._super; var _super = this._super;
if (type === "pdf") { if (type === "pdf" || "text") {
this._rpc({ this._rpc({
model: "ir.actions.report", model: "ir.actions.report",
method: "print_action_for_report_name", method: "print_action_for_report_name",
@@ -49,9 +49,9 @@ odoo.define("base_report_to_printer.print", function (require) {
return _super.apply(self, [action, options, type]); return _super.apply(self, [action, options, type]);
} }
}); });
} else { return Promise.reject();
return _super.apply(self, [action, options, type]);
} }
return _super.apply(self, [action, options, type]);
}, },
}); });
}); });

View File

@@ -43,6 +43,14 @@ class TestReport(common.HttpCase):
"report_name": "base_report_to_printer.test", "report_name": "base_report_to_printer.test",
} }
) )
self.report_text = self.Model.create(
{
"name": "Test",
"report_type": "qweb-text",
"model": "res.partner",
"report_name": "base_report_to_printer.test",
}
)
self.partners = self.env["res.partner"] self.partners = self.env["res.partner"]
for n in range(5): for n in range(5):
self.partners += self.env["res.partner"].create({"name": "Test %d" % n}) self.partners += self.env["res.partner"].create({"name": "Test %d" % n})
@@ -109,6 +117,24 @@ class TestReport(common.HttpCase):
tray=False, tray=False,
) )
def test_render_qweb_text_printable(self):
"""It should print the report, only if it is printable"""
with mock.patch(
"odoo.addons.base_report_to_printer.models."
"printing_printer.PrintingPrinter."
"print_document"
) as print_document:
self.report_text.property_printing_action_id.action_type = "server"
self.report_text.printing_printer_id = self.new_printer()
document = self.report_text._render_qweb_text(self.partners.ids)
print_document.assert_called_once_with(
self.report_text,
document[0],
action="server",
doc_format="qweb-text",
tray=False,
)
def test_print_document_not_printable(self): def test_print_document_not_printable(self):
"""It should print the report, regardless of the defined behaviour""" """It should print the report, regardless of the defined behaviour"""
self.report.printing_printer_id = self.new_printer() self.report.printing_printer_id = self.new_printer()