From a504014023e020159e128a8ce11479a71cdd1d67 Mon Sep 17 00:00:00 2001 From: Thomas Kosel Date: Wed, 2 Oct 2024 11:16:14 +0200 Subject: [PATCH] [UPD] zpl_printer: adding test for zpl_printer Introducing a test that checks the default behavior of the zpl_printer model. Also doing some minor updates to that to make it more error resilient. --- zpl_printer/i18n/de.po | 19 ++++- zpl_printer/i18n/zpl_printer.pot | 139 +++++++++++++++++++++++++++++++ zpl_printer/model/zpl_printer.py | 16 +++- zpl_printer/tests/__init__.py | 1 + zpl_printer/tests/zpl_printer.py | 45 ++++++++++ 5 files changed, 214 insertions(+), 6 deletions(-) create mode 100644 zpl_printer/i18n/zpl_printer.pot create mode 100644 zpl_printer/tests/__init__.py create mode 100644 zpl_printer/tests/zpl_printer.py diff --git a/zpl_printer/i18n/de.po b/zpl_printer/i18n/de.po index 3b05d1a74c7..1156009093d 100644 --- a/zpl_printer/i18n/de.po +++ b/zpl_printer/i18n/de.po @@ -4,10 +4,10 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 16.0+e-20240827\n" +"Project-Id-Version: Odoo Server 16.0+e-20240918\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-28 10:41+0000\n" -"PO-Revision-Date: 2024-08-28 10:41+0000\n" +"POT-Creation-Date: 2024-10-02 09:01+0000\n" +"PO-Revision-Date: 2024-10-02 09:01+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -52,6 +52,7 @@ msgstr "" #. module: zpl_printer #: model:ir.model,name:zpl_printer.model_zpl_printer_zpl_printer +#: model_terms:ir.ui.view,arch_db:zpl_printer.view_zpl_printer_form msgid "Label Printer" msgstr "Label-Drucker" @@ -81,6 +82,13 @@ msgstr "" msgid "Name" msgstr "" +#. module: zpl_printer +#. odoo-python +#: code:addons/zpl_printer/model/zpl_printer.py:0 +#, python-format +msgid "No default printer specified" +msgstr "Kein Standard-Label-Drucker festgelegt" + #. module: zpl_printer #: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__resolution msgid "Printing Resolution (DPI)" @@ -124,6 +132,11 @@ msgstr "Die URL muss eindeutig sein." msgid "Url" msgstr "URL" +#. module: zpl_printer +#: model_terms:ir.ui.view,arch_db:zpl_printer.view_tree_zpl_printer_tree +msgid "ZPL-Printer" +msgstr "Label-Drucker" + #. module: zpl_printer #: model:ir.model.fields.selection,name:zpl_printer.selection__ir_actions_report__report_type__qweb-zpl msgid "qweb-zpl" diff --git a/zpl_printer/i18n/zpl_printer.pot b/zpl_printer/i18n/zpl_printer.pot new file mode 100644 index 00000000000..585d2b9fe54 --- /dev/null +++ b/zpl_printer/i18n/zpl_printer.pot @@ -0,0 +1,139 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * zpl_printer +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0+e-20240918\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-10-02 09:01+0000\n" +"PO-Revision-Date: 2024-10-02 09:01+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: zpl_printer +#: model:ir.model.fields.selection,name:zpl_printer.selection__zpl_printer_zpl_printer__resolution__200 +msgid "200" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields.selection,name:zpl_printer.selection__zpl_printer_zpl_printer__resolution__300 +msgid "300" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__create_uid +msgid "Created by" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__create_date +msgid "Created on" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__default +msgid "Default" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__display_name +msgid "Display Name" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__id +msgid "ID" +msgstr "" + +#. module: zpl_printer +#: model:ir.model,name:zpl_printer.model_zpl_printer_zpl_printer +#: model_terms:ir.ui.view,arch_db:zpl_printer.view_zpl_printer_form +msgid "Label Printer" +msgstr "" + +#. module: zpl_printer +#: model:ir.actions.act_window,name:zpl_printer.action_zpl_printer +#: model_terms:ir.ui.view,arch_db:zpl_printer.res_config_settings_view_form +msgid "Label-Printers" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer____last_update +msgid "Last Modified on" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__write_uid +msgid "Last Updated by" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__write_date +msgid "Last Updated on" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__name +msgid "Name" +msgstr "" + +#. module: zpl_printer +#. odoo-python +#: code:addons/zpl_printer/model/zpl_printer.py:0 +#, python-format +msgid "No default printer specified" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__resolution +msgid "Printing Resolution (DPI)" +msgstr "" + +#. module: zpl_printer +#: model:ir.model,name:zpl_printer.model_ir_actions_report +msgid "Report Action" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_ir_actions_report__report_type +msgid "Report Type" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.constraint,message:zpl_printer.constraint_zpl_printer_zpl_printer_name_unique +msgid "The name must be unique." +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,help:zpl_printer.field_ir_actions_report__report_type +msgid "" +"The type of the report that will be rendered, each one having its own " +"rendering method. HTML means the report will be opened directly in your " +"browser PDF means the report will be rendered using Wkhtmltopdf and " +"downloaded by the user." +msgstr "" + +#. module: zpl_printer +#: model:ir.model.constraint,message:zpl_printer.constraint_zpl_printer_zpl_printer_url_unique +msgid "The url must be unique." +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__url +msgid "Url" +msgstr "" + +#. module: zpl_printer +#: model_terms:ir.ui.view,arch_db:zpl_printer.view_tree_zpl_printer_tree +msgid "ZPL-Printer" +msgstr "" + +#. module: zpl_printer +#: model:ir.model.fields.selection,name:zpl_printer.selection__ir_actions_report__report_type__qweb-zpl +msgid "qweb-zpl" +msgstr "ZPL" diff --git a/zpl_printer/model/zpl_printer.py b/zpl_printer/model/zpl_printer.py index a1af579b1e3..cdb5e90188f 100644 --- a/zpl_printer/model/zpl_printer.py +++ b/zpl_printer/model/zpl_printer.py @@ -1,4 +1,4 @@ -from odoo import fields, models +from odoo import _, fields, models class ZplPrinter(models.Model): @@ -26,7 +26,7 @@ class ZplPrinter(models.Model): def write(self, vals): """There may only be one default.""" - if vals["default"]: + if vals.get("default", False): for previous_default in self.search( [("default", "=", True), ("id", "!=", self.id)] ): @@ -34,8 +34,18 @@ def write(self, vals): return super().write(vals) def get_default_printer(self): - return self.search([("default", "=", True)]) + printer = self.search([("default", "=", True)]) + if len(printer) >= 1: + return printer[0] + printer = self.search([]) + if len(printer) == 0: + raise ValueError(_("No default printer specified")) + return printer[0] def get_label_printer_data(self, report_name, active_ids): + """This function is called from the client to determine which printer will be used. + So this implementation just provides the default printer, but other modules will allow + reacting more specific to this, for example to print a label for a certain product + always on a different printer.""" default_printer = self.get_default_printer() return {"url": default_printer.url, "resolution": default_printer.resolution} diff --git a/zpl_printer/tests/__init__.py b/zpl_printer/tests/__init__.py new file mode 100644 index 00000000000..ffc95ce3289 --- /dev/null +++ b/zpl_printer/tests/__init__.py @@ -0,0 +1 @@ +from . import zpl_printer diff --git a/zpl_printer/tests/zpl_printer.py b/zpl_printer/tests/zpl_printer.py new file mode 100644 index 00000000000..834ecd42428 --- /dev/null +++ b/zpl_printer/tests/zpl_printer.py @@ -0,0 +1,45 @@ +from odoo.tests import common, tagged + +_DEFAULT_PRINTER_URL = "https://default_printer.my_company_network.internal" + + +@tagged("zpl") +class TestZplPrinter(common.TransactionCase): + def setUp(self): + super(TestZplPrinter, self).setUp() + self.env["zpl_printer.zpl_printer"].create( + [ + { + "name": "default", + "url": _DEFAULT_PRINTER_URL, + "resolution": "200", + "default": True, + }, + { + "name": "other_printer", + "url": "https://other_printer.my_company_network.internal", + "resolution": "300", + }, + ] + ) + + def test_write(self): + """Changing the default flag of a printer should remove it from all other printers""" + printer = self.env["zpl_printer.zpl_printer"].search( + [("name", "=", "other_printer")] + ) + printer.default = True + default_after_change = self.env["zpl_printer.zpl_printer"].search( + [("name", "=", "default")] + ) + try: + self.assertFalse(default_after_change.default) + finally: + default_after_change.default = True + + def test_get_label_printer_data(self): + """Unless otherwise specified through this method, the default should be returned""" + result = self.env["zpl_printer.zpl_printer"].get_label_printer_data( + "unspecific_report_name", [1] + ) + self.assertEqual(result, {"url": _DEFAULT_PRINTER_URL, "resolution": "200"})