Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions base_report_to_printer/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ Contributors
* Matias Peralta <mnp@adhoc.com.ar>
* Akim Juillerat <akim.juillerat@camptocamp.com>

* `Tecnativa <https://www.tecnativa.com>`_:

* Sergio Teruel
* David Vidal

Maintainers
~~~~~~~~~~~

Expand Down
8 changes: 8 additions & 0 deletions base_report_to_printer/i18n/base_report_to_printer.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 15.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-10-28 09:59+0000\n"
"PO-Revision-Date: 2024-10-28 09:59+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
Expand Down Expand Up @@ -464,6 +466,12 @@ msgstr ""
msgid "Model"
msgstr ""

#. module: base_report_to_printer
#: model:ir.model.fields,field_description:base_report_to_printer.field_printing_printer__multi_thread
#: model:ir.model.fields,field_description:base_report_to_printer.field_printing_server__multi_thread
msgid "Multi Thread"
msgstr ""

#. module: base_report_to_printer
#: model:ir.model.fields,field_description:base_report_to_printer.field_printing_action__name
#: model:ir.model.fields,field_description:base_report_to_printer.field_printing_job__name
Expand Down
27 changes: 26 additions & 1 deletion base_report_to_printer/models/ir_actions_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
# Copyright (C) 2011 Domsense srl (<http://www.domsense.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).
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

REPORT_TYPES = {"qweb-pdf": "pdf", "qweb-text": "text"}
Expand Down Expand Up @@ -118,6 +120,29 @@
result["printer_exception"] = True
return result

def print_document_client_action(self, record_ids, data=None):
behaviour = self.behaviour()
printer = behaviour.pop("printer", None)

Check warning on line 125 in base_report_to_printer/models/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

base_report_to_printer/models/ir_actions_report.py#L124-L125

Added lines #L124 - L125 were not covered by tests
if printer.multi_thread:

@self.env.cr.postcommit.add
def _launch_print_thread():
threaded_calculation = threading.Thread(

Check warning on line 130 in base_report_to_printer/models/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

base_report_to_printer/models/ir_actions_report.py#L128-L130

Added lines #L128 - L130 were not covered by tests
target=self.print_document_threaded,
args=(self.id, record_ids, data),
)
threaded_calculation.start()

Check warning on line 134 in base_report_to_printer/models/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

base_report_to_printer/models/ir_actions_report.py#L134

Added line #L134 was not covered by tests

return True

Check warning on line 136 in base_report_to_printer/models/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

base_report_to_printer/models/ir_actions_report.py#L136

Added line #L136 was not covered by tests
else:
return self.print_document(record_ids, data=data)

Check warning on line 138 in base_report_to_printer/models/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

base_report_to_printer/models/ir_actions_report.py#L138

Added line #L138 was not covered by tests

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)

Check warning on line 144 in base_report_to_printer/models/ir_actions_report.py

View check run for this annotation

Codecov / codecov/patch

base_report_to_printer/models/ir_actions_report.py#L141-L144

Added lines #L141 - L144 were not covered by tests

def print_document(self, record_ids, data=None):
"""Print a document, do not return the document file"""
report_type = REPORT_TYPES.get(self.report_type)
Expand Down
10 changes: 9 additions & 1 deletion base_report_to_printer/models/printing_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import os
from tempfile import mkstemp

from odoo import fields, models
from odoo import api, fields, models

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -66,6 +66,14 @@ class PrintingPrinter(models.Model):
tray_ids = fields.One2many(
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):
mapping = {3: "available", 4: "printing", 5: "error"}
Expand Down
1 change: 1 addition & 0 deletions base_report_to_printer/models/printing_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class PrintingServer(models.Model):
string="Printers List",
help="List of printers available on this server.",
)
multi_thread = fields.Boolean()

def _open_connection(self, raise_on_error=False):
self.ensure_one()
Expand Down
5 changes: 5 additions & 0 deletions base_report_to_printer/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
* Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
* Matias Peralta <mnp@adhoc.com.ar>
* Akim Juillerat <akim.juillerat@camptocamp.com>

* `Tecnativa <https://www.tecnativa.com>`_:

* Sergio Teruel
* David Vidal
16 changes: 9 additions & 7 deletions base_report_to_printer/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +274,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +300,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -510,14 +509,17 @@ <h2><a class="toc-backref" href="#toc-entry-10">Contributors</a></h2>
<li>Alexandre Fayolle &lt;<a class="reference external" href="mailto:alexandre.fayolle&#64;camptocamp.com">alexandre.fayolle&#64;camptocamp.com</a>&gt;</li>
<li>Matias Peralta &lt;<a class="reference external" href="mailto:mnp&#64;adhoc.com.ar">mnp&#64;adhoc.com.ar</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><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Sergio Teruel</li>
<li>David Vidal</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down
10 changes: 5 additions & 5 deletions base_report_to_printer/static/src/js/qweb_action_manager.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ async function cupsReportActionHandler(action, options, env) {
print_action.action === "server" &&
!print_action.printer_exception
) {
const result = await orm.call("ir.actions.report", "print_document", [
action.id,
action.context.active_ids,
action.data,
]);
const result = await orm.call(
"ir.actions.report",
"print_document_client_action",
[action.id, action.context.active_ids, action.data]
);
if (result) {
env.services.notification.add(env._t("Successfully sent to printer!"), {
type: "success",
Expand Down
17 changes: 17 additions & 0 deletions base_report_to_printer/tests/test_ir_actions_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,20 @@ def test_onchange_printer_tray_id_not_empty(self):
self.assertEqual(action.printer_tray_id, tray)
action.onchange_printing_printer_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,
},
)
1 change: 1 addition & 0 deletions base_report_to_printer/views/printing_printer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<field name="location" />
<field name="status" />
<field name="status_message" />
<field name="multi_thread" />
</group>
<group string="Trays" name="trays">
<field name="tray_ids" nolabel="1">
Expand Down
1 change: 1 addition & 0 deletions base_report_to_printer/views/printing_server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<field name="user" />
<field name="password" />
<field name="encryption_policy" />
<field name="multi_thread" />
</group>
<group>
<separator string="Printers" colspan="2" />
Expand Down
Loading