Skip to content

Commit

Permalink
[IMP] base_report_to_printer: exceptions notifications
Browse files Browse the repository at this point in the history
Better handling of exceptions feedback. A notification will show up with
the issued printer and report and a button for the user to download the
report as a fallback to the failure.

TT51628
  • Loading branch information
chienandalu committed Nov 15, 2024
1 parent f097b37 commit b9e9685
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 31 deletions.
7 changes: 7 additions & 0 deletions base_report_to_printer/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ Guidelines for use:
When no tray is configured for a report and a user, the default tray
setup on the CUPS server is used.

Known issues / Roadmap
======================

- With threaded printing there's no download fallback when the issue
isn't detected by the CUPS Odoo backend. To able to do it, we would
need to notify the bus or use web_notify for it.

Changelog
=========

Expand Down
8 changes: 7 additions & 1 deletion base_report_to_printer/models/ir_actions_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def print_action_for_report_name(self, report_name):
"skip_printer_exception"
):
serializable_result["printer_exception"] = True

Check warning on line 61 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#L61

Added line #L61 was not covered by tests
if self.env.context.get("force_print_to_client"):
serializable_result["action"] = "client"

Check warning on line 63 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#L63

Added line #L63 was not covered by tests
return serializable_result

def _get_user_default_print_behaviour(self):
Expand Down Expand Up @@ -135,7 +137,10 @@ def _launch_print_thread():

return True

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
else:
return self.print_document(record_ids, data=data)
try:
return self.print_document(record_ids, data=data)
except Exception:
return

Check warning on line 143 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#L140-L143

Added lines #L140 - L143 were not covered by tests

def print_document_threaded(self, report_id, record_ids, data):
with registry(self._cr.dbname).cursor() as cr:
Expand Down Expand Up @@ -171,6 +176,7 @@ def print_document(self, record_ids, data=None):
else:
title = self.report_name
behaviour["title"] = title
behaviour["res_ids"] = record_ids
# TODO should we use doc_format instead of report_type
return printer.print_document(
self, document, doc_format=self.report_type, **behaviour
Expand Down
3 changes: 3 additions & 0 deletions base_report_to_printer/readme/ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- With threaded printing there's no download fallback when the issue
isn't detected by the CUPS Odoo backend. To able to do it, we would
need to notify the bus or use web_notify for it.
41 changes: 25 additions & 16 deletions base_report_to_printer/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,17 @@ <h1 class="title">Report to printer</h1>
<li><a class="reference internal" href="#installation" id="toc-entry-1">Installation</a></li>
<li><a class="reference internal" href="#configuration" id="toc-entry-2">Configuration</a></li>
<li><a class="reference internal" href="#usage" id="toc-entry-3">Usage</a></li>
<li><a class="reference internal" href="#changelog" id="toc-entry-4">Changelog</a><ul>
<li><a class="reference internal" href="#section-1" id="toc-entry-5">13.0.1.0.0 (2019-09-30)</a></li>
<li><a class="reference internal" href="#section-2" id="toc-entry-6">12.0.1.0.0 (2018-02-04)</a></li>
<li><a class="reference internal" href="#known-issues-roadmap" id="toc-entry-4">Known issues / Roadmap</a></li>
<li><a class="reference internal" href="#changelog" id="toc-entry-5">Changelog</a><ul>
<li><a class="reference internal" href="#section-1" id="toc-entry-6">13.0.1.0.0 (2019-09-30)</a></li>
<li><a class="reference internal" href="#section-2" id="toc-entry-7">12.0.1.0.0 (2018-02-04)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-7">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-8">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-9">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-10">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-11">Maintainers</a></li>
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-8">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-9">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-10">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-11">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-12">Maintainers</a></li>
</ul>
</li>
</ul>
Expand Down Expand Up @@ -462,33 +463,41 @@ <h1><a class="toc-backref" href="#toc-entry-3">Usage</a></h1>
<p>When no tray is configured for a report and a user, the default tray
setup on the CUPS server is used.</p>
</div>
<div class="section" id="known-issues-roadmap">
<h1><a class="toc-backref" href="#toc-entry-4">Known issues / Roadmap</a></h1>
<ul class="simple">
<li>With threaded printing there’s no download fallback when the issue
isn’t detected by the CUPS Odoo backend. To able to do it, we would
need to notify the bus or use web_notify for it.</li>
</ul>
</div>
<div class="section" id="changelog">
<h1><a class="toc-backref" href="#toc-entry-4">Changelog</a></h1>
<h1><a class="toc-backref" href="#toc-entry-5">Changelog</a></h1>
<div class="section" id="section-1">
<h2><a class="toc-backref" href="#toc-entry-5">13.0.1.0.0 (2019-09-30)</a></h2>
<h2><a class="toc-backref" href="#toc-entry-6">13.0.1.0.0 (2019-09-30)</a></h2>
<ul class="simple">
<li>[RELEASE] Port from V12.</li>
</ul>
</div>
<div class="section" id="section-2">
<h2><a class="toc-backref" href="#toc-entry-6">12.0.1.0.0 (2018-02-04)</a></h2>
<h2><a class="toc-backref" href="#toc-entry-7">12.0.1.0.0 (2018-02-04)</a></h2>
<ul class="simple">
<li>[RELEASE] Port from V11.</li>
</ul>
</div>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-7">Bug Tracker</a></h1>
<h1><a class="toc-backref" href="#toc-entry-8">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/report-print-send/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/report-print-send/issues/new?body=module:%20base_report_to_printer%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#toc-entry-8">Credits</a></h1>
<h1><a class="toc-backref" href="#toc-entry-9">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#toc-entry-9">Authors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-10">Authors</a></h2>
<ul class="simple">
<li>Agile Business Group &amp; Domsense</li>
<li>Pegueroles SCP</li>
Expand All @@ -499,7 +508,7 @@ <h2><a class="toc-backref" href="#toc-entry-9">Authors</a></h2>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-10">Contributors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-11">Contributors</a></h2>
<ul class="simple">
<li>Ferran Pegueroles &lt;<a class="reference external" href="mailto:ferran&#64;pegueroles.com">ferran&#64;pegueroles.com</a>&gt;</li>
<li>Albert Cervera i Areny &lt;<a class="reference external" href="mailto:albert&#64;nan-tic.com">albert&#64;nan-tic.com</a>&gt;</li>
Expand All @@ -521,7 +530,7 @@ <h2><a class="toc-backref" href="#toc-entry-10">Contributors</a></h2>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h2>
<h2><a class="toc-backref" href="#toc-entry-12">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" />
Expand Down
66 changes: 52 additions & 14 deletions base_report_to_printer/static/src/js/qweb_action_manager.esm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @odoo-module */
import {Markup} from "web.utils";
import {_t} from "@web/core/l10n/translation";
import {registry} from "@web/core/registry";

Expand All @@ -9,13 +10,12 @@ async function cupsReportActionHandler(action, options, env) {
const print_action = await orm.call(
"ir.actions.report",
"print_action_for_report_name",
[action.report_name]
[action.report_name],
{context: {force_print_to_client: action.context.force_print_to_client}}
);
if (
print_action &&
print_action.action === "server" &&
!print_action.printer_exception
) {
var printer_exception = print_action.printer_exception;
if (print_action && print_action.action === "server" && !printer_exception) {
// The Odoo CUPS backend is ok. We try to print into the printer
const result = await orm.call(
"ir.actions.report",
"print_document_client_action",
Expand All @@ -25,20 +25,58 @@ async function cupsReportActionHandler(action, options, env) {
env.services.notification.add(_t("Successfully sent to printer!"), {
type: "success",
});
} else {
env.services.notification.add(_t("Could not send to printer!"), {
type: "danger",
});
return true;
// In case of exception during the job, we won't get any response. So we
// should flag the exception and notify the user
}
return true;
env.services.notification.add(_t("Could not sent to printer!"), {
type: "danger",
});
printer_exception = true;
}
if (print_action.printer_exception) {
env.services.notification.add(
env._t("The printer couldn't be reached. Downloading document instead"),
if (print_action && print_action.action === "server" && printer_exception) {
// Just so the translation engine detects them as it doesn't do it inside
// template strings
const terms = {
the_report: env._t("The report"),
couldnt_be_printed: env._t(
"couldn't be printed. Click on the button below to download it"
),
issue_on: env._t("Issue on"),
};
const notificationRemove = env.services.notification.add(
Markup(
`<p>${terms.the_report} <strong>${action.name}</strong> ${terms.couldnt_be_printed}</p>`
),
{
title: `${terms.issue_on} ${print_action.printer_name}`,
type: "warning",
sticky: true,
messageIsHtml: true,
buttons: [
{
name: env._t("Print"),
primary: true,
icon: "fa-print",
onClick: async () => {
const context = {
force_print_to_client: true,
must_skip_send_to_printer: true,
};
env.services.user.updateContext(context);
await env.services.action.doAction(
{type: "ir.actions.report", ...action},
{
additionalContext: context,
}
);
notificationRemove();
},
},
],
}
);
return true;
}
}
}
Expand Down

0 comments on commit b9e9685

Please sign in to comment.