From 442dc6d5744f2ec7284d2d4a1affe1ea6183983b Mon Sep 17 00:00:00 2001 From: Jacques-Etienne Baudoux Date: Wed, 21 Jun 2023 10:03:41 +0200 Subject: [PATCH 1/8] [ADD] printing_auto_base --- printing_auto_base/README.rst | 80 ++++ printing_auto_base/__init__.py | 1 + printing_auto_base/__manifest__.py | 18 + printing_auto_base/models/__init__.py | 2 + printing_auto_base/models/printing_auto.py | 146 ++++++ .../models/printing_auto_mixin.py | 61 +++ printing_auto_base/readme/CONTRIBUTORS.rst | 4 + printing_auto_base/readme/DESCRIPTION.rst | 4 + .../static/description/index.html | 423 ++++++++++++++++++ printing_auto_base/tests/__init__.py | 1 + printing_auto_base/tests/common.py | 92 ++++ .../tests/test_printing_auto_base.py | 92 ++++ printing_auto_base/views/printing_auto.xml | 50 +++ .../odoo/addons/printing_auto_base | 1 + setup/printing_auto_base/setup.py | 6 + 15 files changed, 981 insertions(+) create mode 100644 printing_auto_base/README.rst create mode 100644 printing_auto_base/__init__.py create mode 100644 printing_auto_base/__manifest__.py create mode 100644 printing_auto_base/models/__init__.py create mode 100644 printing_auto_base/models/printing_auto.py create mode 100644 printing_auto_base/models/printing_auto_mixin.py create mode 100644 printing_auto_base/readme/CONTRIBUTORS.rst create mode 100644 printing_auto_base/readme/DESCRIPTION.rst create mode 100644 printing_auto_base/static/description/index.html create mode 100644 printing_auto_base/tests/__init__.py create mode 100644 printing_auto_base/tests/common.py create mode 100644 printing_auto_base/tests/test_printing_auto_base.py create mode 100644 printing_auto_base/views/printing_auto.xml create mode 120000 setup/printing_auto_base/odoo/addons/printing_auto_base create mode 100644 setup/printing_auto_base/setup.py diff --git a/printing_auto_base/README.rst b/printing_auto_base/README.rst new file mode 100644 index 00000000000..2148a63b615 --- /dev/null +++ b/printing_auto_base/README.rst @@ -0,0 +1,80 @@ +================== +printing_auto_base +================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freport--print--send-lightgray.png?logo=github + :target: https://github.com/OCA/report-print-send/tree/14.0/printing_auto_base + :alt: OCA/report-print-send +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/report-print-send-14-0/report-print-send-14-0-printing_auto_base + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/report-print-send&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Base module to support automatic printing of a report or attachments. + +Check other repo like stock-logistics-reporting module printing_auto_stock_picking +for printing documents related to a stock transfer. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* BCIM +* MT Software + +Contributors +~~~~~~~~~~~~ + +* Jacques-Etienne Baudoux (BCIM) +* Michael Tietz (MT Software) +* Camptocamp +* Christopher Hansen + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/report-print-send `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/printing_auto_base/__init__.py b/printing_auto_base/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/printing_auto_base/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/printing_auto_base/__manifest__.py b/printing_auto_base/__manifest__.py new file mode 100644 index 00000000000..6888c3ba29b --- /dev/null +++ b/printing_auto_base/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2022 Jacques-Etienne Baudoux (BCIM) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Printing Auto Base", + "author": "BCIM, MT Software, Odoo Community Association (OCA)", + "maintainers": ["jbaudoux"], + "category": "Warehouse Management", + "data": [ + "views/printing_auto.xml", + ], + "depends": [ + "base_report_to_printer", + ], + "license": "AGPL-3", + "version": "14.0.1.0.0", + "website": "https://github.com/OCA/report-print-send", +} diff --git a/printing_auto_base/models/__init__.py b/printing_auto_base/models/__init__.py new file mode 100644 index 00000000000..0b24867e8a9 --- /dev/null +++ b/printing_auto_base/models/__init__.py @@ -0,0 +1,2 @@ +from . import printing_auto +from . import printing_auto_mixin diff --git a/printing_auto_base/models/printing_auto.py b/printing_auto_base/models/printing_auto.py new file mode 100644 index 00000000000..2216fc0c8df --- /dev/null +++ b/printing_auto_base/models/printing_auto.py @@ -0,0 +1,146 @@ +# Copyright 2022 Jacques-Etienne Baudoux (BCIM) +# Copyright 2022 Michael Tietz (MT Software) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import base64 + +from odoo import _, api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.osv import expression +from odoo.tools.safe_eval import safe_eval + + +class PrintingAuto(models.Model): + """Configure which document to print automatically. This model must be + linked with a many2many relation from the another model from which you want + to print a document""" + + _name = "printing.auto" + _description = "Printing Auto" + + name = fields.Char(string="Name", required=True) + + data_source = fields.Selection( + [ + ("report", "Report"), + ("attachment", "Attachment"), + ], + string="Data source", + default="report", + required=True, + help=( + "Choose to print the result of an odoo report or a pre-existing " + "attachment (useful for labels received from carriers that are " + "recorded on the picking as an attachment)" + ), + ) + report_id = fields.Many2one("ir.actions.report") + attachment_domain = fields.Char("Attachment domain", default="[]") + + condition = fields.Char( + "Condition", + default="[]", + help="Give a domain that must be valid for printing this", + ) + record_change = fields.Char( + "Record change", + help="Select on which document the report must be executed. Use a path " + "using a dotted notation starting from any record field. For " + "example, if your record is a stock.picking, you can access the " + "next picking with 'move_lines.move_dest_ids.picking_id'", + ) + + 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) + + @api.constrains("data_source", "report_id", "attachment_domain") + def _check_data_source(self): + for rec in self: + if rec.data_source == "report" and not rec.report_id: + raise UserError(_("Report is not set")) + if rec.data_source == "attachment" and ( + not rec.attachment_domain or rec.attachment_domain == "[]" + ): + raise UserError(_("Attachment domain is not set")) + + def _get_behaviour(self): + if self.printer_id: + result = {"printer": self.printer_id} + if self.printer_tray_id: + result["tray"] = self.printer_tray_id.system_name + return result + if self.data_source == "report": + return self.report_id.behaviour() + return self.env["ir.actions.report"]._get_user_default_print_behaviour() + + def _get_record(self, record): + if self.record_change: + try: + return safe_eval(f"obj.{self.record_change}", {"obj": record}) + except Exception as e: + raise ValidationError( + _("The Record change could not be applied because: %s") % str(e) + ) from e + return record + + def _check_condition(self, record): + domain = safe_eval(self.condition, {"env": self.env}) + return record.filtered_domain(domain) + + def _get_content(self, record): + generate_data_func = getattr( + self, f"_generate_data_from_{self.data_source}", None + ) + content = [] + if generate_data_func: + records = self._get_record(record) + for record in records: + content.append(generate_data_func(record)[0]) + return content + + def _prepare_attachment_domain(self, record): + domain = safe_eval(self.attachment_domain) + record_domain = [ + ("res_id", "=", record.id), + ("res_model", "=", record._name), + ] + return expression.AND([domain, record_domain]) + + def _generate_data_from_attachment(self, record): + domain = self._prepare_attachment_domain(record) + attachments = self.env["ir.attachment"].search(domain) + if not attachments: + raise ValidationError(_("No attachment was found.")) + return [base64.b64decode(a.datas) for a in attachments] + + def _generate_data_from_report(self, record): + self.ensure_one() + data, _ = self.report_id.with_context(must_skip_send_to_printer=True)._render( + record.id + ) + return [data] + + def do_print(self, record): + self.ensure_one() + record.ensure_one() + + behaviour = self._get_behaviour() + printer = behaviour["printer"] + + if self.nbr_of_copies <= 0: + return (printer, 0) + if not self._check_condition(record): + return (printer, 0) + + if not printer: + raise UserError( + _("No printer configured to print this {}.").format(self.name) + ) + + count = 0 + for content in self._get_content(record): + for _n in range(self.nbr_of_copies): + printer.print_document(report=None, content=content, **behaviour) + count += 1 + return (printer, count) diff --git a/printing_auto_base/models/printing_auto_mixin.py b/printing_auto_base/models/printing_auto_mixin.py new file mode 100644 index 00000000000..036415b1e0f --- /dev/null +++ b/printing_auto_base/models/printing_auto_mixin.py @@ -0,0 +1,61 @@ +# Copyright 2022 Jacques-Etienne Baudoux (BCIM) +# Copyright 2022 Michael Tietz (MT Software) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import logging + +from odoo import _, fields, models + +_logger = logging.getLogger(__name__) + + +class PrintingAutoMixin(models.AbstractModel): + _name = "printing.auto.mixin" + _description = "Printing Auto Mixin" + + auto_printing_ids = fields.Many2many( + "printing.auto", string="Auto Printing Configuration" + ) + printing_auto_error = fields.Text("Printing error") + + def _on_printing_auto_start(self): + self.write({"printing_auto_error": False}) + + def _printing_auto_done_post(self, auto, printer, count): + self.ensure_one() + self.message_post( + body=_("{name}: {count} document(s) sent to printer {printer}").format( + name=auto.name, count=count, printer=printer.name + ) + ) + + def _on_printing_auto_done(self, auto, printer, count): + self._printing_auto_done_post(auto, printer, count) + + def _on_printing_auto_error(self, e): + self.write({"printing_auto_error": str(e)}) + + def _get_printing_auto(self): + return self.auto_printing_ids + + def _handle_print_auto(self, printing_auto): + self.ensure_one() + printing_auto.ensure_one() + try: + with self.env.cr.savepoint(): + printer, count = printing_auto.do_print(self) + if count: + self._on_printing_auto_done(printing_auto, printer, count) + except Exception as e: + _logger.exception( + "An error occurred while printing '%s' for record %s.", + printing_auto, + self, + ) + self._on_printing_auto_error(e) + + def handle_print_auto(self): + """Print some report or attachment directly to the corresponding printer.""" + self._on_printing_auto_start() + for record in self: + for printing_auto in record._get_printing_auto(): + record._handle_print_auto(printing_auto) diff --git a/printing_auto_base/readme/CONTRIBUTORS.rst b/printing_auto_base/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..6a130e8ceeb --- /dev/null +++ b/printing_auto_base/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* Jacques-Etienne Baudoux (BCIM) +* Michael Tietz (MT Software) +* Camptocamp +* Christopher Hansen diff --git a/printing_auto_base/readme/DESCRIPTION.rst b/printing_auto_base/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..82ead2ae4c6 --- /dev/null +++ b/printing_auto_base/readme/DESCRIPTION.rst @@ -0,0 +1,4 @@ +Base module to support automatic printing of a report or attachments. + +Check other repo like stock-logistics-reporting module printing_auto_stock_picking +for printing documents related to a stock transfer. diff --git a/printing_auto_base/static/description/index.html b/printing_auto_base/static/description/index.html new file mode 100644 index 00000000000..018029963d4 --- /dev/null +++ b/printing_auto_base/static/description/index.html @@ -0,0 +1,423 @@ + + + + + + +printing_auto_base + + + +
+

printing_auto_base

+ + +

Beta License: AGPL-3 OCA/report-print-send Translate me on Weblate Try me on Runboat

+

Base module to support automatic priting of a report or attachments.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • BCIM
  • +
  • MT Software
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

This module is part of the OCA/report-print-send project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/printing_auto_base/tests/__init__.py b/printing_auto_base/tests/__init__.py new file mode 100644 index 00000000000..998b91457ce --- /dev/null +++ b/printing_auto_base/tests/__init__.py @@ -0,0 +1 @@ +from . import test_printing_auto_base diff --git a/printing_auto_base/tests/common.py b/printing_auto_base/tests/common.py new file mode 100644 index 00000000000..1cd0d1110bb --- /dev/null +++ b/printing_auto_base/tests/common.py @@ -0,0 +1,92 @@ +# Copyright 2022 Jacques-Etienne Baudoux (BCIM) +# Copyright 2022 Michael Tietz (MT Software) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo.tests import common + + +def print_document(cls, *args, **kwargs): + return + + +class TestPrintingAutoCommon(common.SavepointCase): + @classmethod + def _create_printer(cls, name): + return cls.env["printing.printer"].create( + { + "name": name, + "system_name": name, + "server_id": cls.server.id, + } + ) + + @classmethod + def _create_tray(cls, name, printer): + return cls.env["printing.tray"].create( + {"name": name, "system_name": name, "printer_id": printer.id} + ) + + @classmethod + def setUpReportAndRecord(cls): + cls.report = cls.env.ref("base.report_ir_model_overview") + cls.record = cls.env.ref("base.model_res_partner") + + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.server = cls.env["printing.server"].create({}) + for i in range(1, 4): + printer_name = f"printer_{i}" + tray_name = f"tray_{i}" + printer = cls._create_printer(printer_name) + tray = cls._create_tray(tray_name, printer) + setattr(cls, printer_name, printer) + setattr(cls, tray_name, tray) + + cls.setUpReportAndRecord() + cls.data = cls.report._render(cls.record.id)[0] + + @classmethod + def _create_printing_auto(cls, vals): + return cls.env["printing.auto"].create(vals) + + @classmethod + def _create_attachment(cls, record, data, name_suffix): + return cls.env["ir.attachment"].create( + { + "res_model": record._name, + "res_id": record.id, + "name": f"printing_auto_test_attachment_{name_suffix}.txt", + "raw": data, + } + ) + + @classmethod + def _prepare_printing_auto_report_vals(cls): + return { + "data_source": "report", + "report_id": cls.report.id, + "name": "Printing auto report", + } + + @classmethod + def _create_printing_auto_report(cls, vals=None): + _vals = cls._prepare_printing_auto_report_vals() + _vals.update(vals or {}) + return cls._create_printing_auto(_vals) + + @classmethod + def _prepare_printing_auto_attachment_vals(cls): + return { + "data_source": "attachment", + "attachment_domain": "[('name', 'like', 'printing_auto_test_attachment')]", + "name": "Printing auto attachment", + } + + @classmethod + def _create_printing_auto_attachment(cls, vals=None): + _vals = cls._prepare_printing_auto_attachment_vals() + _vals.update(vals or {}) + return cls._create_printing_auto(_vals) diff --git a/printing_auto_base/tests/test_printing_auto_base.py b/printing_auto_base/tests/test_printing_auto_base.py new file mode 100644 index 00000000000..1e1607b49ae --- /dev/null +++ b/printing_auto_base/tests/test_printing_auto_base.py @@ -0,0 +1,92 @@ +# Copyright 2022 Jacques-Etienne Baudoux (BCIM) +# Copyright 2022 Michael Tietz (MT Software) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from unittest import mock + +from odoo.exceptions import UserError, ValidationError + +from .common import PrintingPrinter, TestPrintingAutoCommon, print_document + + +@mock.patch.object(PrintingPrinter, "print_document", print_document) +class TestPrintingAutoBase(TestPrintingAutoCommon): + def test_check_data_source(self): + with self.assertRaises(UserError): + self._create_printing_auto_report({"report_id": False}) + + with self.assertRaises(UserError): + self._create_printing_auto_attachment({"attachment_domain": "[]"}) + + with self.assertRaises(UserError): + printing_auto = self._create_printing_auto_attachment() + printing_auto.attachment_domain = False + + def test_behaviour(self): + expected = {"printer": self.printer_1} + printing_auto = self._create_printing_auto_report( + {"printer_id": self.printer_1.id} + ) + self.assertEqual(expected, printing_auto._get_behaviour()) + + printing_auto.printer_tray_id = self.tray_1 + expected["tray"] = self.tray_1.system_name + self.assertEqual(expected, printing_auto._get_behaviour()) + + expected = printing_auto.report_id.behaviour() + printing_auto.printer_id = False + printing_auto.printer_tray_id = False + self.assertEqual(expected, printing_auto._get_behaviour()) + + expected = self.env["ir.actions.report"]._get_user_default_print_behaviour() + printing_auto = self._create_printing_auto_attachment() + self.assertEqual(expected, printing_auto._get_behaviour()) + + def test_record_change(self): + parent = self.env["res.partner"].create({"name": "Parent"}) + partner = parent.create({"name": "Child", "parent_id": parent.id}) + printing_auto = self._create_printing_auto_report( + {"record_change": "parent_id"} + ) + self.assertEqual(parent, printing_auto._get_record(partner)) + + def test_check_condition(self): + partner = self.env["res.partner"].create({"name": "Partner"}) + printing_auto = self._create_printing_auto_report( + {"condition": f"[('name', '=', '{partner.name}')]"} + ) + self.assertEqual(partner, printing_auto._check_condition(partner)) + printing_auto.condition = "[('name', '=', '1')]" + self.assertFalse(printing_auto._check_condition(partner)) + + def test_get_content(self): + printing_auto_report = self._create_printing_auto_report() + self.assertEqual([self.data], printing_auto_report._get_content(self.record)) + + printing_auto_attachment = self._create_printing_auto_attachment() + attachment = self._create_attachment(self.record, self.data, "1") + self.assertEqual( + [attachment.raw], printing_auto_attachment._get_content(self.record) + ) + attachment.unlink() + + with self.assertRaises(ValidationError): + printing_auto_attachment._get_content(self.record) + + def test_do_print(self): + printing_auto = self._create_printing_auto_attachment( + {"attachment_domain": "[('name', 'like', 'printing_auto_test')]"} + ) + self._create_attachment(self.record, self.data, "1") + with self.assertRaises(UserError): + printing_auto.do_print(self.record) + + printing_auto.printer_id = self.printer_1 + for nbr_of_copies in [0, 2, 1]: + expected = (self.printer_1, nbr_of_copies) + printing_auto.nbr_of_copies = nbr_of_copies + self.assertEqual(expected, printing_auto.do_print(self.record)) + + printing_auto.condition = "[('name', '=', 'test_printing_auto')]" + expected = (self.printer_1, 0) + self.assertEqual(expected, printing_auto.do_print(self.record)) diff --git a/printing_auto_base/views/printing_auto.xml b/printing_auto_base/views/printing_auto.xml new file mode 100644 index 00000000000..a346bd5ff35 --- /dev/null +++ b/printing_auto_base/views/printing_auto.xml @@ -0,0 +1,50 @@ + + + printing.auto.view.form + printing.auto + +
+ + + + + + + + + + + + + +
+
+
+ + + printing.auto.view.tree + printing.auto + + + + + + + + + + + + + + +
diff --git a/setup/printing_auto_base/odoo/addons/printing_auto_base b/setup/printing_auto_base/odoo/addons/printing_auto_base new file mode 120000 index 00000000000..b17e1789c1a --- /dev/null +++ b/setup/printing_auto_base/odoo/addons/printing_auto_base @@ -0,0 +1 @@ +../../../../printing_auto_base \ No newline at end of file diff --git a/setup/printing_auto_base/setup.py b/setup/printing_auto_base/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/printing_auto_base/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) From 9dc8e3ad02a38dc1a62a6e0dbf3794f1b72d0732 Mon Sep 17 00:00:00 2001 From: Jacques-Etienne Baudoux Date: Wed, 21 Jun 2023 10:04:06 +0200 Subject: [PATCH 2/8] [ADD] printing_auto_label_printer --- printing_auto_label_printer/README.rst | 77 ++++ printing_auto_label_printer/__init__.py | 1 + printing_auto_label_printer/__manifest__.py | 20 + .../models/__init__.py | 1 + .../models/printing_auto.py | 16 + .../readme/CONTRIBUTORS.rst | 4 + .../readme/DESCRIPTION.rst | 1 + .../static/description/index.html | 423 ++++++++++++++++++ printing_auto_label_printer/tests/__init__.py | 1 + .../tests/test_printing_auto_label_printer.py | 13 + .../views/printing_auto.xml | 23 + .../odoo/addons/printing_auto_label_printer | 1 + setup/printing_auto_label_printer/setup.py | 6 + 13 files changed, 587 insertions(+) create mode 100644 printing_auto_label_printer/README.rst create mode 100644 printing_auto_label_printer/__init__.py create mode 100644 printing_auto_label_printer/__manifest__.py create mode 100644 printing_auto_label_printer/models/__init__.py create mode 100644 printing_auto_label_printer/models/printing_auto.py create mode 100644 printing_auto_label_printer/readme/CONTRIBUTORS.rst create mode 100644 printing_auto_label_printer/readme/DESCRIPTION.rst create mode 100644 printing_auto_label_printer/static/description/index.html create mode 100644 printing_auto_label_printer/tests/__init__.py create mode 100644 printing_auto_label_printer/tests/test_printing_auto_label_printer.py create mode 100644 printing_auto_label_printer/views/printing_auto.xml create mode 120000 setup/printing_auto_label_printer/odoo/addons/printing_auto_label_printer create mode 100644 setup/printing_auto_label_printer/setup.py diff --git a/printing_auto_label_printer/README.rst b/printing_auto_label_printer/README.rst new file mode 100644 index 00000000000..b878cdd1a35 --- /dev/null +++ b/printing_auto_label_printer/README.rst @@ -0,0 +1,77 @@ +=========================== +printing_auto_label_printer +=========================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freport--print--send-lightgray.png?logo=github + :target: https://github.com/OCA/report-print-send/tree/14.0/printing_auto_label_printer + :alt: OCA/report-print-send +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/report-print-send-14-0/report-print-send-14-0-printing_auto_label_printer + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/report-print-send&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Base module to support automatic priting of a report or attachments on a label printer. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* BCIM +* MT Software + +Contributors +~~~~~~~~~~~~ + +* Jacques-Etienne Baudoux (BCIM) +* Michael Tietz (MT Software) +* Camptocamp +* Christopher Hansen + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/report-print-send `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/printing_auto_label_printer/__init__.py b/printing_auto_label_printer/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/printing_auto_label_printer/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/printing_auto_label_printer/__manifest__.py b/printing_auto_label_printer/__manifest__.py new file mode 100644 index 00000000000..d61e5282773 --- /dev/null +++ b/printing_auto_label_printer/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2022 Jacques-Etienne Baudoux (BCIM) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Printing Auto Label Printer", + "author": "BCIM, MT Software, Odoo Community Association (OCA)", + "maintainers": ["jbaudoux"], + "category": "Warehouse Management", + "data": [ + "views/printing_auto.xml", + ], + "depends": [ + "base_report_to_label_printer", + "printing_auto_base", + ], + "auto_install": True, + "license": "AGPL-3", + "version": "14.0.1.0.0", + "website": "https://github.com/OCA/report-print-send", +} diff --git a/printing_auto_label_printer/models/__init__.py b/printing_auto_label_printer/models/__init__.py new file mode 100644 index 00000000000..ad80a1bfdf6 --- /dev/null +++ b/printing_auto_label_printer/models/__init__.py @@ -0,0 +1 @@ +from . import printing_auto diff --git a/printing_auto_label_printer/models/printing_auto.py b/printing_auto_label_printer/models/printing_auto.py new file mode 100644 index 00000000000..2c20c972cd2 --- /dev/null +++ b/printing_auto_label_printer/models/printing_auto.py @@ -0,0 +1,16 @@ +# Copyright 2022 Jacques-Etienne Baudoux (BCIM) +# Copyright 2022 Michael Tietz (MT Software) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class PrintingAuto(models.Model): + _inherit = "printing.auto" + + is_label = fields.Boolean("Is Label") + + def _get_behaviour(self): + if self.is_label and not self.printer_id: + return {"printer": self.env.user.default_label_printer_id} + return super()._get_behaviour() diff --git a/printing_auto_label_printer/readme/CONTRIBUTORS.rst b/printing_auto_label_printer/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..6a130e8ceeb --- /dev/null +++ b/printing_auto_label_printer/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* Jacques-Etienne Baudoux (BCIM) +* Michael Tietz (MT Software) +* Camptocamp +* Christopher Hansen diff --git a/printing_auto_label_printer/readme/DESCRIPTION.rst b/printing_auto_label_printer/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..a354dacddb0 --- /dev/null +++ b/printing_auto_label_printer/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Base module to support automatic priting of a report or attachments on a label printer. diff --git a/printing_auto_label_printer/static/description/index.html b/printing_auto_label_printer/static/description/index.html new file mode 100644 index 00000000000..0dc5be5d32a --- /dev/null +++ b/printing_auto_label_printer/static/description/index.html @@ -0,0 +1,423 @@ + + + + + + +printing_auto_label_printer + + + +
+

printing_auto_label_printer

+ + +

Beta License: AGPL-3 OCA/report-print-send Translate me on Weblate Try me on Runboat

+

Base module to support automatic priting of a report or attachments on a label printer.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • BCIM
  • +
  • MT Software
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

This module is part of the OCA/report-print-send project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/printing_auto_label_printer/tests/__init__.py b/printing_auto_label_printer/tests/__init__.py new file mode 100644 index 00000000000..4bd0e6ae3ec --- /dev/null +++ b/printing_auto_label_printer/tests/__init__.py @@ -0,0 +1 @@ +from . import test_printing_auto_label_printer diff --git a/printing_auto_label_printer/tests/test_printing_auto_label_printer.py b/printing_auto_label_printer/tests/test_printing_auto_label_printer.py new file mode 100644 index 00000000000..19ad06990f6 --- /dev/null +++ b/printing_auto_label_printer/tests/test_printing_auto_label_printer.py @@ -0,0 +1,13 @@ +# Copyright 2022 Jacques-Etienne Baudoux (BCIM) +# Copyright 2022 Michael Tietz (MT Software) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.addons.printing_auto_base.tests.common import TestPrintingAutoCommon + + +class TestPrintingAutoBase(TestPrintingAutoCommon): + def test_behaviour_label(self): + self.env.user.default_label_printer_id = self.printer_2 + expected = {"printer": self.printer_2} + printing_auto = self._create_printing_auto_attachment({"is_label": True}) + self.assertEqual(expected, printing_auto._get_behaviour()) diff --git a/printing_auto_label_printer/views/printing_auto.xml b/printing_auto_label_printer/views/printing_auto.xml new file mode 100644 index 00000000000..dca737cb7f4 --- /dev/null +++ b/printing_auto_label_printer/views/printing_auto.xml @@ -0,0 +1,23 @@ + + + printing.auto.label.printer + printing.auto + + + + + + + + + + printing.auto.label.printer + printing.auto + + + + + + + + diff --git a/setup/printing_auto_label_printer/odoo/addons/printing_auto_label_printer b/setup/printing_auto_label_printer/odoo/addons/printing_auto_label_printer new file mode 120000 index 00000000000..391a7dcd082 --- /dev/null +++ b/setup/printing_auto_label_printer/odoo/addons/printing_auto_label_printer @@ -0,0 +1 @@ +../../../../printing_auto_label_printer \ No newline at end of file diff --git a/setup/printing_auto_label_printer/setup.py b/setup/printing_auto_label_printer/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/printing_auto_label_printer/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) From fb24e652fe833fa26fd783d5d33951b3a0850c97 Mon Sep 17 00:00:00 2001 From: Michael Tietz Date: Fri, 14 Jul 2023 15:13:05 +0200 Subject: [PATCH 3/8] [IMP] printing_auto_base: log or raise Select on printing.auto to log or raise errors --- printing_auto_base/models/printing_auto.py | 6 ++++++ printing_auto_base/models/printing_auto_mixin.py | 15 +++++++++++---- printing_auto_base/views/printing_auto.xml | 2 ++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/printing_auto_base/models/printing_auto.py b/printing_auto_base/models/printing_auto.py index 2216fc0c8df..351f80b3277 100644 --- a/printing_auto_base/models/printing_auto.py +++ b/printing_auto_base/models/printing_auto.py @@ -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): diff --git a/printing_auto_base/models/printing_auto_mixin.py b/printing_auto_base/models/printing_auto_mixin.py index 036415b1e0f..5bb0f851b40 100644 --- a/printing_auto_base/models/printing_auto_mixin.py +++ b/printing_auto_base/models/printing_auto_mixin.py @@ -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.", diff --git a/printing_auto_base/views/printing_auto.xml b/printing_auto_base/views/printing_auto.xml index a346bd5ff35..5489e49a448 100644 --- a/printing_auto_base/views/printing_auto.xml +++ b/printing_auto_base/views/printing_auto.xml @@ -24,6 +24,7 @@ domain="[('printer_id', '=', printer_id)]" /> + @@ -44,6 +45,7 @@ + From 28ad399edfbac16494bf79b2d1d2ec8b2fcf8250 Mon Sep 17 00:00:00 2001 From: Thierry Ducrest Date: Fri, 15 Sep 2023 13:12:38 +0200 Subject: [PATCH 4/8] [FIX] printing_auto_base: don't print twice Fix same record printed multiple times --- printing_auto_base/models/printing_auto.py | 12 ++-- .../models/printing_auto_mixin.py | 9 ++- printing_auto_base/tests/model_test.py | 37 +++++++++++ .../tests/test_printing_auto_base.py | 61 +++++++++++++++++++ 4 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 printing_auto_base/tests/model_test.py diff --git a/printing_auto_base/models/printing_auto.py b/printing_auto_base/models/printing_auto.py index 351f80b3277..d5c7bd5cc4f 100644 --- a/printing_auto_base/models/printing_auto.py +++ b/printing_auto_base/models/printing_auto.py @@ -94,13 +94,13 @@ def _check_condition(self, record): domain = safe_eval(self.condition, {"env": self.env}) return record.filtered_domain(domain) - def _get_content(self, record): + def _get_content(self, records): generate_data_func = getattr( self, f"_generate_data_from_{self.data_source}", None ) content = [] if generate_data_func: - records = self._get_record(record) + records = self._get_record(records) for record in records: content.append(generate_data_func(record)[0]) return content @@ -127,16 +127,16 @@ def _generate_data_from_report(self, record): ) return [data] - def do_print(self, record): + def do_print(self, records): self.ensure_one() - record.ensure_one() behaviour = self._get_behaviour() printer = behaviour["printer"] if self.nbr_of_copies <= 0: return (printer, 0) - if not self._check_condition(record): + records = self._check_condition(records) + if not records: return (printer, 0) if not printer: @@ -145,7 +145,7 @@ def do_print(self, record): ) count = 0 - for content in self._get_content(record): + for content in self._get_content(records): for _n in range(self.nbr_of_copies): printer.print_document(report=None, content=content, **behaviour) count += 1 diff --git a/printing_auto_base/models/printing_auto_mixin.py b/printing_auto_base/models/printing_auto_mixin.py index 5bb0f851b40..715289c52fe 100644 --- a/printing_auto_base/models/printing_auto_mixin.py +++ b/printing_auto_base/models/printing_auto_mixin.py @@ -38,7 +38,6 @@ def _get_printing_auto(self): return self.auto_printing_ids def _do_print_auto(self, printing_auto): - self.ensure_one() printing_auto.ensure_one() printer, count = printing_auto.do_print(self) if count: @@ -63,6 +62,12 @@ def _handle_print_auto(self, printing_auto): def handle_print_auto(self): """Print some report or attachment directly to the corresponding printer.""" self._on_printing_auto_start() + to_print = {} for record in self: for printing_auto in record._get_printing_auto(): - record._handle_print_auto(printing_auto) + if printing_auto not in to_print.keys(): + to_print[printing_auto] = record + else: + to_print[printing_auto] |= record + for printing_auto, records in to_print.items(): + records._handle_print_auto(printing_auto) diff --git a/printing_auto_base/tests/model_test.py b/printing_auto_base/tests/model_test.py new file mode 100644 index 00000000000..3cf729ba9ab --- /dev/null +++ b/printing_auto_base/tests/model_test.py @@ -0,0 +1,37 @@ +from odoo import fields, models + + +def setup_test_model(env, model_cls): + """Pass a test model class and initialize it. + + Courtesy of SBidoul from https://github.com/OCA/mis-builder :) + """ + model_cls._build_model(env.registry, env.cr) + env.registry.setup_models(env.cr) + env.registry.init_models( + env.cr, [model_cls._name], dict(env.context, update_custom_fields=True) + ) + + +def teardown_test_model(env, model_cls): + """Pass a test model class and deinitialize it. + + Courtesy of SBidoul from https://github.com/OCA/mis-builder :) + """ + if not getattr(model_cls, "_teardown_no_delete", False): + del env.registry.models[model_cls._name] + env.registry.setup_models(env.cr) + + +class PrintingAutoTesterChild(models.Model): + _name = "printingauto.tester.child" + + name = fields.Char() + + +class PrintingAutoTester(models.Model): + _name = "printingauto.tester" + _inherit = "printing.auto.mixin" + + name = fields.Char() + child_ids = fields.Many2many("printingauto.tester.child") diff --git a/printing_auto_base/tests/test_printing_auto_base.py b/printing_auto_base/tests/test_printing_auto_base.py index 1e1607b49ae..c88b10d1318 100644 --- a/printing_auto_base/tests/test_printing_auto_base.py +++ b/printing_auto_base/tests/test_printing_auto_base.py @@ -7,10 +7,17 @@ from odoo.exceptions import UserError, ValidationError from .common import PrintingPrinter, TestPrintingAutoCommon, print_document +from .model_test import PrintingAutoTester, PrintingAutoTesterChild, setup_test_model @mock.patch.object(PrintingPrinter, "print_document", print_document) class TestPrintingAutoBase(TestPrintingAutoCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + setup_test_model(cls.env, PrintingAutoTesterChild) + setup_test_model(cls.env, PrintingAutoTester) + def test_check_data_source(self): with self.assertRaises(UserError): self._create_printing_auto_report({"report_id": False}) @@ -90,3 +97,57 @@ def test_do_print(self): printing_auto.condition = "[('name', '=', 'test_printing_auto')]" expected = (self.printer_1, 0) self.assertEqual(expected, printing_auto.do_print(self.record)) + + def test_do_not_print_multiple_time_the_same_record(self): + """Check the same record is not printed multiple times. + + When the 'record_change' field is being used on the printing auto configuration + and 'handle_print_auto' is called from a recrodset. + The same record could be send for printing multiple times. + + """ + printing_auto = self._create_printing_auto_report( + vals={"record_change": "child_ids", "printer_id": self.printer_1.id} + ) + child1 = self.env["printingauto.tester.child"].create({"name": "Child One"}) + child2 = self.env["printingauto.tester.child"].create({"name": "Child Two"}) + parent1 = self.env["printingauto.tester"].create( + { + "name": "Customer One", + "child_ids": [(4, child1.id, 0)], + "auto_printing_ids": [(4, printing_auto.id, 0)], + } + ) + parent2 = self.env["printingauto.tester"].create( + { + "name": "Customer Two", + "child_ids": [(4, child1.id, 0)], + "auto_printing_ids": [(4, printing_auto.id, 0)], + } + ) + parents = parent1 | parent2 + generate_data_from = ( + "odoo.addons.printing_auto_base.models.printing_auto." + "PrintingAuto._generate_data_from_report" + ) + with mock.patch(generate_data_from) as generate_data_from: + # Both parents have the same child only print the child report once + parents.handle_print_auto() + self.assertEqual(generate_data_from.call_count, 1) + generate_data_from.assert_called_with(child1) + generate_data_from.reset_mock() + # Both parents have different childs, print both child reports + parent2.child_ids = [(6, 0, child2.ids)] + parents.handle_print_auto() + self.assertEqual(generate_data_from.call_count, 2) + generate_data_from.assert_has_calls( + [mock.call(child1), mock.call(child2)], any_order=True + ) + generate_data_from.reset_mock() + # THe parents have one child in common and one parent has a 2nd child + parent2.child_ids = [(4, child1.id, 0)] + parents.handle_print_auto() + self.assertEqual(generate_data_from.call_count, 2) + generate_data_from.assert_has_calls( + [mock.call(child1), mock.call(child2)], any_order=True + ) From 47aface33b995932a309f730ee0a49d7a1c6b443 Mon Sep 17 00:00:00 2001 From: Jacques-Etienne Baudoux Date: Tue, 19 Sep 2023 17:32:10 +0200 Subject: [PATCH 5/8] [FIX] printing_auto_base: collect all attachments --- printing_auto_base/models/printing_auto.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/printing_auto_base/models/printing_auto.py b/printing_auto_base/models/printing_auto.py index d5c7bd5cc4f..fe1d13ac9c2 100644 --- a/printing_auto_base/models/printing_auto.py +++ b/printing_auto_base/models/printing_auto.py @@ -102,7 +102,7 @@ def _get_content(self, records): if generate_data_func: records = self._get_record(records) for record in records: - content.append(generate_data_func(record)[0]) + content += generate_data_func(record) return content def _prepare_attachment_domain(self, record): From 221d2f8e6b509e21a75df2fa43eb26f4ab03fcca Mon Sep 17 00:00:00 2001 From: Jacques-Etienne Baudoux Date: Fri, 6 Dec 2024 12:24:11 +0100 Subject: [PATCH 6/8] [FIX] printing_auto_base: bound to model --- printing_auto_base/models/printing_auto.py | 1 + printing_auto_base/tests/common.py | 1 + 2 files changed, 2 insertions(+) diff --git a/printing_auto_base/models/printing_auto.py b/printing_auto_base/models/printing_auto.py index fe1d13ac9c2..5f3ed8bf6dc 100644 --- a/printing_auto_base/models/printing_auto.py +++ b/printing_auto_base/models/printing_auto.py @@ -19,6 +19,7 @@ class PrintingAuto(models.Model): _description = "Printing Auto" name = fields.Char(string="Name", required=True) + model = fields.Char(string="Related Document Model", required=True) data_source = fields.Selection( [ diff --git a/printing_auto_base/tests/common.py b/printing_auto_base/tests/common.py index 1cd0d1110bb..847b7e2ada4 100644 --- a/printing_auto_base/tests/common.py +++ b/printing_auto_base/tests/common.py @@ -50,6 +50,7 @@ def setUpClass(cls): @classmethod def _create_printing_auto(cls, vals): + vals.setdefault("model", "printing.auto") return cls.env["printing.auto"].create(vals) @classmethod From 690f7c32b2fc5eab137605ef9ef5c88af3cb00c4 Mon Sep 17 00:00:00 2001 From: Jacques-Etienne Baudoux Date: Fri, 6 Dec 2024 15:40:43 +0100 Subject: [PATCH 7/8] [FIX] printing_auto_base: use odoo_test_helper --- printing_auto_base/tests/model_test.py | 25 +++---------------- .../tests/test_printing_auto_base.py | 14 ++++++++--- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/printing_auto_base/tests/model_test.py b/printing_auto_base/tests/model_test.py index 3cf729ba9ab..61bac3f916c 100644 --- a/printing_auto_base/tests/model_test.py +++ b/printing_auto_base/tests/model_test.py @@ -1,26 +1,7 @@ -from odoo import fields, models - - -def setup_test_model(env, model_cls): - """Pass a test model class and initialize it. - - Courtesy of SBidoul from https://github.com/OCA/mis-builder :) - """ - model_cls._build_model(env.registry, env.cr) - env.registry.setup_models(env.cr) - env.registry.init_models( - env.cr, [model_cls._name], dict(env.context, update_custom_fields=True) - ) +# Copyright 2023 Camptocamp +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). - -def teardown_test_model(env, model_cls): - """Pass a test model class and deinitialize it. - - Courtesy of SBidoul from https://github.com/OCA/mis-builder :) - """ - if not getattr(model_cls, "_teardown_no_delete", False): - del env.registry.models[model_cls._name] - env.registry.setup_models(env.cr) +from odoo import fields, models class PrintingAutoTesterChild(models.Model): diff --git a/printing_auto_base/tests/test_printing_auto_base.py b/printing_auto_base/tests/test_printing_auto_base.py index c88b10d1318..fce32e202ed 100644 --- a/printing_auto_base/tests/test_printing_auto_base.py +++ b/printing_auto_base/tests/test_printing_auto_base.py @@ -4,10 +4,13 @@ from unittest import mock +from odoo_test_helper import FakeModelLoader + from odoo.exceptions import UserError, ValidationError -from .common import PrintingPrinter, TestPrintingAutoCommon, print_document -from .model_test import PrintingAutoTester, PrintingAutoTesterChild, setup_test_model +from odoo.addons.base_report_to_printer.models.printing_printer import PrintingPrinter + +from .common import TestPrintingAutoCommon, print_document @mock.patch.object(PrintingPrinter, "print_document", print_document) @@ -15,8 +18,11 @@ class TestPrintingAutoBase(TestPrintingAutoCommon): @classmethod def setUpClass(cls): super().setUpClass() - setup_test_model(cls.env, PrintingAutoTesterChild) - setup_test_model(cls.env, PrintingAutoTester) + cls.loader = FakeModelLoader(cls.env, cls.__module__) + cls.loader.backup_registry() + from .model_test import PrintingAutoTester, PrintingAutoTesterChild + + cls.loader.update_registry((PrintingAutoTesterChild, PrintingAutoTester)) def test_check_data_source(self): with self.assertRaises(UserError): From e5b0dee319bf4a48f651f739294bf7139be8bed4 Mon Sep 17 00:00:00 2001 From: Jacques-Etienne Baudoux Date: Fri, 6 Dec 2024 15:43:55 +0100 Subject: [PATCH 8/8] [FIX] printing_auto_base: use proper Error --- printing_auto_base/models/printing_auto.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/printing_auto_base/models/printing_auto.py b/printing_auto_base/models/printing_auto.py index 5f3ed8bf6dc..88a8aaa41e7 100644 --- a/printing_auto_base/models/printing_auto.py +++ b/printing_auto_base/models/printing_auto.py @@ -65,11 +65,11 @@ class PrintingAuto(models.Model): def _check_data_source(self): for rec in self: if rec.data_source == "report" and not rec.report_id: - raise UserError(_("Report is not set")) + raise ValidationError(_("Report is not set")) if rec.data_source == "attachment" and ( not rec.attachment_domain or rec.attachment_domain == "[]" ): - raise UserError(_("Attachment domain is not set")) + raise ValidationError(_("Attachment domain is not set")) def _get_behaviour(self): if self.printer_id: @@ -86,7 +86,7 @@ def _get_record(self, record): try: return safe_eval(f"obj.{self.record_change}", {"obj": record}) except Exception as e: - raise ValidationError( + raise UserError( _("The Record change could not be applied because: %s") % str(e) ) from e return record @@ -118,7 +118,7 @@ def _generate_data_from_attachment(self, record): domain = self._prepare_attachment_domain(record) attachments = self.env["ir.attachment"].search(domain) if not attachments: - raise ValidationError(_("No attachment was found.")) + raise UserError(_("No attachment was found.")) return [base64.b64decode(a.datas) for a in attachments] def _generate_data_from_report(self, record):