From 2f9de1f5f68a44ecb0c531f9fe2d0997c0e2701e Mon Sep 17 00:00:00 2001 From: Florent de Labarre Date: Tue, 23 Jan 2018 00:41:35 +0100 Subject: [PATCH] [IMP] Add a test mode to print a label on write --- printer_zpl2/README.rst | 1 + printer_zpl2/models/printing_label_zpl2.py | 11 +++++++++++ printer_zpl2/tests/test_test_mode.py | 10 ++++++++++ printer_zpl2/views/printing_label_zpl2.xml | 9 +++++++-- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/printer_zpl2/README.rst b/printer_zpl2/README.rst index e488011..5400be5 100644 --- a/printer_zpl2/README.rst +++ b/printer_zpl2/README.rst @@ -25,6 +25,7 @@ To configure this module, you need to: #. Go to *Settings > Printing > Labels > ZPL II* #. Create new labels #. Import ZPL2 code +#. Use the Test Mode tab during the creation It's also possible to add a label printing wizard on any model by creating a new *ir.actions.act_window* record. For example, to add the printing wizard on the *product.product* model : diff --git a/printer_zpl2/models/printing_label_zpl2.py b/printer_zpl2/models/printing_label_zpl2.py index d175154..e6b9609 100644 --- a/printer_zpl2/models/printing_label_zpl2.py +++ b/printer_zpl2/models/printing_label_zpl2.py @@ -49,9 +49,12 @@ class PrintingLabelZpl2(models.Model): default=True) action_window_id = fields.Many2one( comodel_name='ir.actions.act_window', string='Action', readonly=True) + test_print_mode = fields.Boolean(string='Mode Print') test_labelary_mode = fields.Boolean(string='Mode Labelary') 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_dpmm = fields.Selection( selection=[ @@ -293,6 +296,14 @@ class PrintingLabelZpl2(models.Model): return record + def print_test_label(self): + for label in self: + if label.test_print_mode and label.record_id and label.printer_id: + record = label._get_record() + extra = safe_eval(label.extra, {'env': self.env}) + 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') diff --git a/printer_zpl2/tests/test_test_mode.py b/printer_zpl2/tests/test_test_mode.py index 20abdb5..870a837 100644 --- a/printer_zpl2/tests/test_test_mode.py +++ b/printer_zpl2/tests/test_test_mode.py @@ -1,4 +1,5 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import mock from odoo.tests.common import TransactionCase @@ -38,6 +39,15 @@ class TestWizardPrintRecordLabel(TransactionCase): record = Obj.search([], limit=1, order='id desc') self.assertEqual(res, record) + @mock.patch('%s.cups' % model) + def test_print_label_test(self, cups): + """ Check if print test """ + self.label.test_print_mode = True + self.label.printer_id = self.printer + self.label.record_id = 10 + self.label.print_test_label() + cups.Connection().printFile.assert_called_once() + def test_emulation_without_params(self): """ Check if not execute next if not in this mode """ self.label.test_labelary_mode = False diff --git a/printer_zpl2/views/printing_label_zpl2.xml b/printer_zpl2/views/printing_label_zpl2.xml index 0272173..d22cea1 100644 --- a/printer_zpl2/views/printing_label_zpl2.xml +++ b/printer_zpl2/views/printing_label_zpl2.xml @@ -38,6 +38,9 @@ + +