From be18cfda933fadab80f605c976d543e3d0eba6b7 Mon Sep 17 00:00:00 2001 From: Florent de Labarre Date: Thu, 5 Mar 2020 20:41:33 +0100 Subject: [PATCH] [IMP] print_zpl2 : quick move --- printer_zpl2/models/printing_label_zpl2.py | 38 +- .../models/printing_label_zpl2_component.py | 16 + .../tests/test_printing_label_zpl2.py | 25 +- printer_zpl2/tests/test_test_mode.py | 10 +- .../tests/test_wizard_print_record_label.py | 4 +- printer_zpl2/views/printing_label_zpl2.xml | 330 +++++++++++++----- printer_zpl2/wizard/print_record_label.xml | 15 +- printer_zpl2/wizard/wizard_import_zpl2.xml | 17 +- setup/printer_zpl2/odoo/addons/printer_zpl2 | 1 + setup/printer_zpl2/setup.py | 6 + 10 files changed, 327 insertions(+), 135 deletions(-) create mode 120000 setup/printer_zpl2/odoo/addons/printer_zpl2 create mode 100644 setup/printer_zpl2/setup.py diff --git a/printer_zpl2/models/printing_label_zpl2.py b/printer_zpl2/models/printing_label_zpl2.py index 3c347e7..8f33ee9 100644 --- a/printer_zpl2/models/printing_label_zpl2.py +++ b/printer_zpl2/models/printing_label_zpl2.py @@ -70,7 +70,8 @@ class PrintingLabelZpl2(models.Model): record_id = fields.Integer(string="Record ID", default=1) extra = fields.Text(string="Extra", default="{}") printer_id = fields.Many2one(comodel_name="printing.printer", string="Printer") - labelary_image = fields.Binary(string="Image from Labelary", readonly=True) + labelary_image = fields.Binary(string='Image from Labelary', + compute='_compute_labelary_image') labelary_dpmm = fields.Selection( selection=[ ("6dpmm", "6dpmm (152 pdi)"), @@ -407,16 +408,14 @@ class PrintingLabelZpl2(models.Model): if record: label.print_label(label.printer_id, record, **extra) - @api.onchange( - "record_id", - "labelary_dpmm", - "labelary_width", - "labelary_height", - "component_ids", - "origin_x", - "origin_y", - ) - def _on_change_labelary(self): + @api.depends( + 'record_id', 'labelary_dpmm', 'labelary_width', 'labelary_height', + 'component_ids', 'origin_x', 'origin_y', 'test_labelary_mode') + def _compute_labelary_image(self): + for label in self: + label.labelary_image = label._generate_labelary_image() + + def _generate_labelary_image(self): self.ensure_one() if not ( self.test_labelary_mode @@ -426,7 +425,7 @@ class PrintingLabelZpl2(models.Model): and self.labelary_dpmm and self.component_ids ): - return + return False record = self._get_record() if record: # If case there an error (in the data field with the safe_eval @@ -454,15 +453,12 @@ class PrintingLabelZpl2(models.Model): new_im.paste(im, (1, 1)) imgByteArr = io.BytesIO() new_im.save(imgByteArr, format="PNG") - self.labelary_image = base64.b64encode(imgByteArr.getvalue()) + return base64.b64encode(imgByteArr.getvalue()) else: - return { - "warning": { - "title": _("Error with Labelary API."), - "message": response.status_code, - } - } + _logger.warning( + _( + "Error with Labelary API. %s") % response.status_code) except Exception as e: - self.labelary_image = False - return {"warning": {"title": _("Some thing is wrong."), "message": e}} + _logger.warning(_("Error with Labelary API. %s") % e) + return False diff --git a/printer_zpl2/models/printing_label_zpl2_component.py b/printer_zpl2/models/printing_label_zpl2_component.py index 9b566fa..9ad4660 100644 --- a/printer_zpl2/models/printing_label_zpl2_component.py +++ b/printer_zpl2/models/printing_label_zpl2_component.py @@ -232,3 +232,19 @@ class PrintingLabelZpl2Component(models.Model): help="This field holds a static image to print. " "If not set, the data field is evaluated.", ) + + def action_plus_origin_x(self): + self.ensure_one() + self.origin_x += 10 + + def action_minus_origin_x(self): + self.ensure_one() + self.origin_x -= 10 + + def action_plus_origin_y(self): + self.ensure_one() + self.origin_y += 10 + + def action_minus_origin_y(self): + self.ensure_one() + self.origin_y -= 10 diff --git a/printer_zpl2/tests/test_printing_label_zpl2.py b/printer_zpl2/tests/test_printing_label_zpl2.py index 28c853f..1d67e8a 100644 --- a/printer_zpl2/tests/test_printing_label_zpl2.py +++ b/printer_zpl2/tests/test_printing_label_zpl2.py @@ -67,10 +67,10 @@ class TestPrintingLabelZpl2(TransactionCase): def test_print_empty_label(self, cups): """ Check that printing an empty label works """ label = self.new_label() - file_name = 'test.zpl' + file_name = "test.zpl" label.print_label(self.printer, self.printer) cups.Connection().printFile.assert_called_once_with( - printer.system_name, file_name, file_name, options={} + self.printer.system_name, file_name, file_name, options={} ) def test_empty_label_contents(self): @@ -1170,3 +1170,24 @@ class TestPrintingLabelZpl2(TransactionCase): self.assertEqual( contents, "^XA\n" "^PW480\n" "^CI28\n" "^LH10,10\n" "^JUR\n" "^XZ" ) + + def test_zpl2_component_quick_move(self): + """ Check component quick move """ + label = self.new_label() + component = self.new_component( + { + "label_id": label.id, + "component_type": "zpl2_raw", + "data": '""', + "origin_x": 20, + "origin_y": 30, + } + ) + component.action_plus_origin_x() + self.assertEqual(30, component.origin_x) + component.action_minus_origin_x() + self.assertEqual(20, component.origin_x) + component.action_plus_origin_y() + self.assertEqual(40, component.origin_y) + component.action_minus_origin_y() + self.assertEqual(30, component.origin_y) diff --git a/printer_zpl2/tests/test_test_mode.py b/printer_zpl2/tests/test_test_mode.py index b5dc60a..d6d7c4e 100644 --- a/printer_zpl2/tests/test_test_mode.py +++ b/printer_zpl2/tests/test_test_mode.py @@ -51,16 +51,15 @@ class TestWizardPrintRecordLabel(TransactionCase): self.label.test_print_mode = True self.label.printer_id = self.printer self.label.record_id = 10 - file_name = 'test.zpl' - self.label.print_test_label() + file_name = "test.zpl" + self.label.print_test_label() cups.Connection().printFile.assert_called_once_with( - self.printer.system_name, file_name, file_name, options={} + self.printer.system_name, file_name, file_name, options={} ) def test_emulation_without_params(self): """ Check if not execute next if not in this mode """ self.label.test_labelary_mode = False - self.label._on_change_labelary() self.assertIs(self.label.labelary_image, False) def test_emulation_with_bad_header(self): @@ -72,7 +71,6 @@ class TestWizardPrintRecordLabel(TransactionCase): self.env["printing.label.zpl2.component"].create( {"name": "ZPL II Label", "label_id": self.label.id, "data": '"Test"'} ) - self.label._on_change_labelary() self.assertFalse(self.label.labelary_image) def test_emulation_with_bad_data_compute(self): @@ -84,7 +82,6 @@ class TestWizardPrintRecordLabel(TransactionCase): component = self.env["printing.label.zpl2.component"].create( {"name": "ZPL II Label", "label_id": self.label.id, "data": "wrong_data"} ) - self.label._on_change_labelary() component.unlink() self.assertIs(self.label.labelary_image, False) @@ -97,5 +94,4 @@ class TestWizardPrintRecordLabel(TransactionCase): self.env["printing.label.zpl2.component"].create( {"name": "ZPL II Label", "label_id": self.label.id, "data": '"good_data"'} ) - self.label._on_change_labelary() self.assertTrue(self.label.labelary_image) diff --git a/printer_zpl2/tests/test_wizard_print_record_label.py b/printer_zpl2/tests/test_wizard_print_record_label.py index 19ce42c..2c2308d 100644 --- a/printer_zpl2/tests/test_wizard_print_record_label.py +++ b/printer_zpl2/tests/test_wizard_print_record_label.py @@ -48,9 +48,9 @@ class TestWizardPrintRecordLabel(TransactionCase): self.assertEqual(wizard.printer_id, self.printer) self.assertEqual(wizard.label_id, self.label) wizard.print_label() - file_name = 'test.zpl' + file_name = "test.zpl" cups.Connection().printFile.assert_called_once_with( - self.printer.system_name, file_name, file_name, options={} + self.printer.system_name, file_name, file_name, options={} ) def test_wizard_multiple_printers_and_labels(self): diff --git a/printer_zpl2/views/printing_label_zpl2.xml b/printer_zpl2/views/printing_label_zpl2.xml index 71f7e60..d99917d 100644 --- a/printer_zpl2/views/printing_label_zpl2.xml +++ b/printer_zpl2/views/printing_label_zpl2.xml @@ -1,4 +1,4 @@ - + - + - - - - - - - - - + + + + + + + + + - + - - - - + + + + - + - - - - - + + + + + - + - - - - + + + + - - + +

Note : It is an emulation from http://labelary.com/, the result on printer can be different.

@@ -148,21 +258,42 @@ - - + + - - - - - + + + + + - + @@ -174,9 +305,13 @@ printing.label.zpl2 - - - + + + @@ -185,21 +320,32 @@ ir.actions.act_window printing.label.zpl2 tree,form - + [] {} - - - + + + form - + - - - + + + tree - + - + diff --git a/printer_zpl2/wizard/print_record_label.xml b/printer_zpl2/wizard/print_record_label.xml index f111f2c..c4fafaf 100644 --- a/printer_zpl2/wizard/print_record_label.xml +++ b/printer_zpl2/wizard/print_record_label.xml @@ -1,4 +1,4 @@ - +