Skip to content

Commit

Permalink
[IMP] printing_auto_base: Select on printing.auto to log or raise errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-software-de authored and jbaudoux committed Aug 23, 2023
1 parent 9fa4736 commit b731511
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
6 changes: 6 additions & 0 deletions printing_auto_base/models/printing_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class PrintingAuto(models.Model):
printer_id = fields.Many2one("printing.printer", "Printer")
printer_tray_id = fields.Many2one("printing.tray", "Tray")
nbr_of_copies = fields.Integer("Number of Copies", default=1)
action_on_error = fields.Selection(
[("log", "Record an error"), ("raise", "Raise an Exception")],
"Action on error",
default="log",
required=True,
)

@api.constrains("data_source", "report_id", "attachment_domain")
def _check_data_source(self):
Expand Down
15 changes: 11 additions & 4 deletions printing_auto_base/models/printing_auto_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@ def _on_printing_auto_error(self, e):
def _get_printing_auto(self):
return self.auto_printing_ids

def _handle_print_auto(self, printing_auto):
def _do_print_auto(self, printing_auto):
self.ensure_one()
printing_auto.ensure_one()
printer, count = printing_auto.do_print(self)
if count:
self._on_printing_auto_done(printing_auto, printer, count)

def _handle_print_auto(self, printing_auto):
printing_auto.ensure_one()
if printing_auto.action_on_error == "raise":
self._do_print_auto(printing_auto)
return

Check warning on line 51 in printing_auto_base/models/printing_auto_mixin.py

View check run for this annotation

Codecov / codecov/patch

printing_auto_base/models/printing_auto_mixin.py#L51

Added line #L51 was not covered by tests
try:
with self.env.cr.savepoint():
printer, count = printing_auto.do_print(self)
if count:
self._on_printing_auto_done(printing_auto, printer, count)
self._do_print_auto(printing_auto)
except Exception as e:
_logger.exception(
"An error occurred while printing '%s' for record %s.",
Expand Down
2 changes: 2 additions & 0 deletions printing_auto_base/views/printing_auto.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
domain="[('printer_id', '=', printer_id)]"
/>
<field name="nbr_of_copies" />
<field name="action_on_error" />
</group>
</sheet>
</form>
Expand All @@ -44,6 +45,7 @@
<field name="printer_id" />
<field name="printer_tray_id" />
<field name="nbr_of_copies" />
<field name="action_on_error" />
</tree>
</field>
</record>
Expand Down
7 changes: 7 additions & 0 deletions printing_auto_stock_picking/tests/test_printing_auto_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import logging
from unittest import mock

from odoo.exceptions import UserError

from odoo.addons.printing_auto_base.tests.common import (
PrintingPrinter,
TestPrintingAutoCommon,
Expand Down Expand Up @@ -39,3 +41,8 @@ def test_action_done_printing_auto(self):
def test_action_done_printing_error(self):
self.record._action_done()
self.assertTrue(self.record.printing_auto_error)

def test_action_done_printing_error_raise(self):
self.printing_auto.action_on_error = "raise"
with self.assertRaises(UserError):
self.record._action_done()

0 comments on commit b731511

Please sign in to comment.