[IMP] base_report_to_printer: Add printer option to launch reports in new thread

This commit is contained in:
sergio-teruel
2024-10-26 23:32:27 +02:00
committed by David
parent 82471a7a12
commit 05ac999d11
10 changed files with 75 additions and 7 deletions

View File

@@ -163,6 +163,11 @@ Contributors
* Akim Juillerat <akim.juillerat@camptocamp.com> * Akim Juillerat <akim.juillerat@camptocamp.com>
* Jacques-Etienne Baudoux (BCIM) <je@bcim.be> * Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
* `Tecnativa <https://www.tecnativa.com>`_:
* Sergio Teruel
* David Vidal
Maintainers Maintainers
~~~~~~~~~~~ ~~~~~~~~~~~

View File

@@ -3,9 +3,11 @@
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>) # Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
# 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>)
# Copyright 2024 Tecnativa - Sergio Teruel
# 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 threading
from odoo import _, api, exceptions, fields, models from odoo import _, api, exceptions, fields, models, registry
from odoo.tools.safe_eval import safe_eval, time from odoo.tools.safe_eval import safe_eval, time
REPORT_TYPES = {"qweb-pdf": "pdf", "qweb-text": "text"} REPORT_TYPES = {"qweb-pdf": "pdf", "qweb-text": "text"}
@@ -118,6 +120,29 @@ class IrActionsReport(models.Model):
result["printer_exception"] = True result["printer_exception"] = True
return result return result
def print_document_client_action(self, record_ids, data=None):
behaviour = self.behaviour()
printer = behaviour.pop("printer", None)
if printer.multi_thread:
@self.env.cr.postcommit.add
def _launch_print_thread():
threaded_calculation = threading.Thread(
target=self.print_document_threaded,
args=(self.id, record_ids, data),
)
threaded_calculation.start()
return True
else:
return self.print_document(record_ids, data=data)
def print_document_threaded(self, report_id, record_ids, data):
with registry(self._cr.dbname).cursor() as cr:
self = self.with_env(self.env(cr=cr))
report = self.env["ir.actions.report"].browse(report_id)
report.print_document(record_ids, data)
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"""
report_type = REPORT_TYPES.get(self.report_type) report_type = REPORT_TYPES.get(self.report_type)

View File

@@ -12,7 +12,7 @@ import logging
import os import os
from tempfile import mkstemp from tempfile import mkstemp
from odoo import fields, models from odoo import api, fields, models
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@@ -67,6 +67,14 @@ class PrintingPrinter(models.Model):
tray_ids = fields.One2many( tray_ids = fields.One2many(
comodel_name="printing.tray", inverse_name="printer_id", string="Paper Sources" comodel_name="printing.tray", inverse_name="printer_id", string="Paper Sources"
) )
multi_thread = fields.Boolean(
compute="_compute_multi_thread", readonly=False, store=True
)
@api.depends("server_id.multi_thread")
def _compute_multi_thread(self):
for printer in self:
printer.multi_thread = printer.server_id.multi_thread
def _prepare_update_from_cups(self, cups_connection, cups_printer): def _prepare_update_from_cups(self, cups_connection, cups_printer):
mapping = {3: "available", 4: "printing", 5: "error"} mapping = {3: "available", 4: "printing", 5: "error"}

View File

@@ -42,6 +42,7 @@ class PrintingServer(models.Model):
string="Printers List", string="Printers List",
help="List of printers available on this server.", help="List of printers available on this server.",
) )
multi_thread = fields.Boolean()
def _open_connection(self, raise_on_error=False): def _open_connection(self, raise_on_error=False):
self.ensure_one() self.ensure_one()

View File

@@ -15,3 +15,8 @@
* Hughes Damry <hughes.damry@acsone.eu> * Hughes Damry <hughes.damry@acsone.eu>
* Akim Juillerat <akim.juillerat@camptocamp.com> * Akim Juillerat <akim.juillerat@camptocamp.com>
* Jacques-Etienne Baudoux (BCIM) <je@bcim.be> * Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
* `Tecnativa <https://www.tecnativa.com>`_:
* Sergio Teruel
* David Vidal

View File

@@ -512,6 +512,11 @@ If you spotted it first, help us to smash it by providing a detailed and welcome
<li>Hughes Damry &lt;<a class="reference external" href="mailto:hughes.damry&#64;acsone.eu">hughes.damry&#64;acsone.eu</a>&gt;</li> <li>Hughes Damry &lt;<a class="reference external" href="mailto:hughes.damry&#64;acsone.eu">hughes.damry&#64;acsone.eu</a>&gt;</li>
<li>Akim Juillerat &lt;<a class="reference external" href="mailto:akim.juillerat&#64;camptocamp.com">akim.juillerat&#64;camptocamp.com</a>&gt;</li> <li>Akim Juillerat &lt;<a class="reference external" href="mailto:akim.juillerat&#64;camptocamp.com">akim.juillerat&#64;camptocamp.com</a>&gt;</li>
<li>Jacques-Etienne Baudoux (BCIM) &lt;<a class="reference external" href="mailto:je&#64;bcim.be">je&#64;bcim.be</a>&gt;</li> <li>Jacques-Etienne Baudoux (BCIM) &lt;<a class="reference external" href="mailto:je&#64;bcim.be">je&#64;bcim.be</a>&gt;</li>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Sergio Teruel</li>
<li>David Vidal</li>
</ul>
</li>
</ul> </ul>
</div> </div>
<div class="section" id="maintainers"> <div class="section" id="maintainers">

View File

@@ -15,11 +15,11 @@ async function cupsReportActionHandler(action, options, env) {
print_action.action === "server" && print_action.action === "server" &&
!print_action.printer_exception !print_action.printer_exception
) { ) {
const result = await orm.call("ir.actions.report", "print_document", [ const result = await orm.call(
action.id, "ir.actions.report",
action.context.active_ids, "print_document_client_action",
action.data, [action.id, action.context.active_ids, action.data]
]); );
if (result) { if (result) {
env.services.notification.add(env._t("Successfully sent to printer!"), { env.services.notification.add(env._t("Successfully sent to printer!"), {
type: "success", type: "success",

View File

@@ -295,3 +295,20 @@ class TestIrActionsReportXml(TransactionCase):
self.assertEqual(action.printer_tray_id, tray) self.assertEqual(action.printer_tray_id, tray)
action.onchange_printing_printer_id() action.onchange_printing_printer_id()
self.assertFalse(action.printer_tray_id) self.assertFalse(action.printer_tray_id)
def test_print_in_new_thread(self):
"""It should return the action and printer from printing action in other thread"""
report = self.Model.search([], limit=1)
self.env.user.printing_action = "server"
printing_action = self.new_printing_action()
printing_action.user_id = self.env.user
printing_action.printer_id = self.new_printer()
printing_action.printer_id.multi_thread = True
self.assertEqual(
report.behaviour(),
{
"action": printing_action.action,
"printer": printing_action.printer_id,
"tray": False,
},
)

View File

@@ -78,6 +78,7 @@
<field name="location" /> <field name="location" />
<field name="status" /> <field name="status" />
<field name="status_message" /> <field name="status_message" />
<field name="multi_thread" />
</group> </group>
<group string="Trays" name="trays"> <group string="Trays" name="trays">
<field name="tray_ids" nolabel="1" colspan="2"> <field name="tray_ids" nolabel="1" colspan="2">

View File

@@ -31,6 +31,7 @@
<field name="user" /> <field name="user" />
<field name="password" /> <field name="password" />
<field name="encryption_policy" /> <field name="encryption_policy" />
<field name="multi_thread" />
</group> </group>
<group name="printers" string="Printers"> <group name="printers" string="Printers">
<field name="printer_ids" nolabel="1" colspan="2" /> <field name="printer_ids" nolabel="1" colspan="2" />