From 840189f600a09c82b4d6e22ed7870859001e4f34 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 | 31 +++++++++---------- .../models/printing_label_zpl2_component.py | 16 ++++++++++ .../tests/test_printing_label_zpl2.py | 19 ++++++++++++ printer_zpl2/tests/test_test_mode.py | 8 ++--- printer_zpl2/views/printing_label_zpl2.xml | 4 +++ 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/printer_zpl2/models/printing_label_zpl2.py b/printer_zpl2/models/printing_label_zpl2.py index e3de9d2..d69c548 100644 --- a/printer_zpl2/models/printing_label_zpl2.py +++ b/printer_zpl2/models/printing_label_zpl2.py @@ -55,7 +55,8 @@ class PrintingLabelZpl2(models.Model): 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)'), @@ -312,15 +313,19 @@ class PrintingLabelZpl2(models.Model): if record: label.print_label(label.printer_id, record, **extra) - @api.onchange( + @api.depends( 'record_id', 'labelary_dpmm', 'labelary_width', 'labelary_height', - 'component_ids', 'origin_x', 'origin_y') - def _on_change_labelary(self): + '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 and self.record_id and self.labelary_width and self.labelary_height 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 @@ -349,17 +354,11 @@ 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 c2fde3b..13e45b3 100644 --- a/printer_zpl2/models/printing_label_zpl2_component.py +++ b/printer_zpl2/models/printing_label_zpl2_component.py @@ -184,3 +184,19 @@ class PrintingLabelZpl2Component(models.Model): string='Image', attachment=True, 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 b944696..01a3c28 100644 --- a/printer_zpl2/tests/test_printing_label_zpl2.py +++ b/printer_zpl2/tests/test_printing_label_zpl2.py @@ -1135,3 +1135,22 @@ class TestPrintingLabelZpl2(TransactionCase): '^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 7fada83..5c0651f 100644 --- a/printer_zpl2/tests/test_test_mode.py +++ b/printer_zpl2/tests/test_test_mode.py @@ -52,8 +52,7 @@ class TestWizardPrintRecordLabel(TransactionCase): 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, None) + self.assertIs(self.label.labelary_image, False) def test_emulation_with_bad_header(self): """ Check if bad header """ @@ -65,7 +64,6 @@ class TestWizardPrintRecordLabel(TransactionCase): '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): @@ -78,9 +76,8 @@ class TestWizardPrintRecordLabel(TransactionCase): '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, None) + self.assertIs(self.label.labelary_image, False) def test_emulation_with_good_data(self): """ Check if ok """ @@ -92,5 +89,4 @@ class TestWizardPrintRecordLabel(TransactionCase): '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/views/printing_label_zpl2.xml b/printer_zpl2/views/printing_label_zpl2.xml index 69008ba..c2d793e 100644 --- a/printer_zpl2/views/printing_label_zpl2.xml +++ b/printer_zpl2/views/printing_label_zpl2.xml @@ -49,7 +49,11 @@ +