Skip to content

Commit

Permalink
[IMP] printing_auto_base: log or raise
Browse files Browse the repository at this point in the history
Select on printing.auto to log or raise errors
  • Loading branch information
mt-software-de authored and jbaudoux committed Sep 19, 2023
1 parent 15963b9 commit dd85c24
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 @@ -53,6 +53,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
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 dd85c24

Please sign in to comment.