[IMP] Add wizard to import ZPL2

This commit is contained in:
Florent de Labarre
2018-01-20 23:20:11 +01:00
committed by duongtq
parent a15b973a42
commit 66c3d88353
9 changed files with 576 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ To configure this module, you need to:
#. Go to *Settings > Printing > Labels > ZPL II* #. Go to *Settings > Printing > Labels > ZPL II*
#. Create new labels #. Create new labels
#. Import ZPL2 code
It's also possible to add a label printing wizard on any model by creating a new *ir.actions.act_window* record. 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 : For example, to add the printing wizard on the *product.product* model :

View File

@@ -18,6 +18,7 @@
'security/ir.model.access.csv', 'security/ir.model.access.csv',
'views/printing_label_zpl2.xml', 'views/printing_label_zpl2.xml',
'wizard/print_record_label.xml', 'wizard/print_record_label.xml',
'wizard/wizard_import_zpl2.xml',
], ],
'installable': True, 'installable': True,
} }

View File

@@ -272,6 +272,17 @@ class PrintingLabelZpl2(models.Model):
def unlink_action(self): def unlink_action(self):
self.mapped('action_window_id').unlink() self.mapped('action_window_id').unlink()
def import_zpl2(self):
self.ensure_one()
return {
'view_type': 'form',
'view_mode': 'form',
'res_model': 'wizard.import.zpl2',
'type': 'ir.actions.act_window',
'target': 'new',
'context': {'default_label_id': self.id},
}
def _get_record(self): def _get_record(self):
self.ensure_one() self.ensure_one()
Obj = self.env[self.model_id.model] Obj = self.env[self.model_id.model]

View File

@@ -5,3 +5,4 @@ from . import test_printing_label_zpl2
from . import test_wizard_print_record_label from . import test_wizard_print_record_label
from . import test_generate_action from . import test_generate_action
from . import test_test_mode from . import test_test_mode
from . import test_wizard_import_zpl2

View File

@@ -0,0 +1,97 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase
class TestWizardImportZpl2(TransactionCase):
def setUp(self):
super(TestWizardImportZpl2, self).setUp()
self.Model = self.env['wizard.print.record.label']
self.server = self.env['printing.server'].create({})
self.printer = self.env['printing.printer'].create({
'name': 'Printer',
'server_id': self.server.id,
'system_name': 'Sys Name',
'default': True,
'status': 'unknown',
'status_message': 'Msg',
'model': 'res.users',
'location': 'Location',
'uri': 'URI',
})
self.label = self.env['printing.label.zpl2'].create({
'name': 'ZPL II Label',
'model_id': self.env.ref(
'base_report_to_printer.model_printing_printer').id,
})
def test_open_wizard(self):
""" open wizard from label"""
res = self.label.import_zpl2()
self.assertEqual(
res.get('context').get('default_label_id'),
self.label.id)
def test_wizard_import_zpl2(self):
""" Import ZPL2 from wizard """
zpl_data = ("^XA\n"
"^CI28\n"
"^LH0,0\n"
"^CF0\n"
"^CFA,10\n"
"^CFB,10,10\n"
"^FO10,10^A0N,30,30^FDTEXT^FS\n"
"^BY2,3.0^FO600,60^BCN,30,N,N,N"
"^FDAJFGJAGJVJVHK^FS\n"
"^FO10,40^A0N,20,40^FB150,2,1,J,0^FDTEXT BLOCK^FS\n"
"^FO300,10^GC100,3,B^FS\n"
"^FO10,200^GB200,200,100,B,0^FS\n"
"^FO10,60^GFA,16.0,16.0,2.0,"
"b'FFC0FFC0FFC0FFC0FFC0FFC0FFC0FFC0'^FS\n"
"^FO10,200^GB300,100,6,W,0^FS\n"
"^BY2,3.0^FO300,10^B1N,N,30,N,N^FD678987656789^FS\n"
"^BY2,3.0^FO300,70^B2N,30,Y,Y,N^FD567890987768^FS\n"
"^BY2,3.0^FO300,120^B3N,N,30,N,N^FD98765456787656^FS\n"
"^BY2,3.0^FO300,200^BQN,2,5,Q,7"
"^FDMM,A876567897656787658654645678^FS\n"
"^BY2,3.0^FO400,250^BER,40,Y,Y^FD9876789987654567^FS\n"
"^BY2,3.0^FO350,250^B7N,20,0,0,0,N^FD8765678987656789^FS\n"
"^BY2,3.0^FO700,10^B9N,20,N,N,N^FD87657890987654^FS\n"
"^BY2,3.0^FO600,200^B4N,50,N^FD7654567898765678^FS\n"
"^BY2,3.0^FO600,300^BEN,50,Y,Y^FD987654567890876567^FS\n"
"^FO300,300^AGI,50,50^FR^FDINVERTED^FS\n"
"^BY2,3.0^FO700,200^B8,50,N,N^FD987609876567^FS\n"
"^JUR\n"
"^XZ")
vals = {'label_id': self.label.id,
'delete_component': True,
'data': zpl_data}
wizard = self.env['wizard.import.zpl2'].create(vals)
wizard.import_zpl2()
self.assertEqual(
18,
len(self.label.component_ids))
def test_wizard_import_zpl2_add(self):
""" Import ZPL2 from wizard ADD"""
self.env['printing.label.zpl2.component'].create({
'name': 'ZPL II Label',
'label_id': self.label.id,
'data': '"data"',
'sequence': 10})
zpl_data = ("^XA\n"
"^CI28\n"
"^LH0,0\n"
"^FO10,10^A0N,30,30^FDTEXT^FS\n"
"^JUR\n"
"^XZ")
vals = {'label_id': self.label.id,
'delete_component': False,
'data': zpl_data}
wizard = self.env['wizard.import.zpl2'].create(vals)
wizard.import_zpl2()
self.assertEqual(
2,
len(self.label.component_ids))

View File

@@ -17,6 +17,9 @@
<field name="model">printing.label.zpl2</field> <field name="model">printing.label.zpl2</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="ZPL II Label"> <form string="ZPL II Label">
<header>
<button name="import_zpl2" string="Import ZPL2" type="object"/>
</header>
<sheet> <sheet>
<div class="oe_button_box" name="button_box"> <div class="oe_button_box" name="button_box">
<field name="action_window_id" invisible="1"/> <field name="action_window_id" invisible="1"/>

View File

@@ -2,3 +2,4 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from . import print_record_label from . import print_record_label
from . import wizard_import_zpl2

View File

@@ -0,0 +1,437 @@
import logging
import re
import base64
import binascii
import io
from PIL import Image, ImageOps
from odoo import models, api, fields, _
_logger = logging.getLogger(__name__)
try:
import zpl2
except ImportError:
_logger.debug('Cannot `import zpl2`.')
def _compute_arg(data, arg):
vals = {}
for i, d in enumerate(data.split(',')):
vals[arg[i]] = d
return vals
def _field_origin(data):
if data[:2] == 'FO':
position = data[2:]
vals = _compute_arg(position, ['origin_x', 'origin_y'])
return vals
return {}
def _font_format(data):
if data[:1] == 'A':
data = data.split(',')
vals = {}
if len(data[0]) > 1:
vals[zpl2.ARG_FONT] = data[0][1]
if len(data[0]) > 2:
vals[zpl2.ARG_ORIENTATION] = data[0][2]
if len(data) > 1:
vals[zpl2.ARG_HEIGHT] = data[1]
if len(data) > 2:
vals[zpl2.ARG_WIDTH] = data[2]
return vals
return {}
def _default_font_format(data):
if data[:2] == 'CF':
args = [
zpl2.ARG_FONT,
zpl2.ARG_HEIGHT,
zpl2.ARG_WIDTH,
]
vals = _compute_arg(data[2:], args)
if vals.get(zpl2.ARG_HEIGHT, False) \
and not vals.get(zpl2.ARG_WIDTH, False):
vals.update({zpl2.ARG_WIDTH: vals.get(zpl2.ARG_HEIGHT)})
else:
vals.update({zpl2.ARG_HEIGHT: 10, zpl2.ARG_HEIGHT: 10})
return vals
return {}
def _field_block(data):
if data[:2] == 'FB':
vals = {zpl2.ARG_IN_BLOCK: True}
args = [
zpl2.ARG_BLOCK_WIDTH,
zpl2.ARG_BLOCK_LINES,
zpl2.ARG_BLOCK_SPACES,
zpl2.ARG_BLOCK_JUSTIFY,
zpl2.ARG_BLOCK_LEFT_MARGIN,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _code11(data):
if data[:2] == 'B1':
vals = {'component_type': zpl2.BARCODE_CODE_11}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_CHECK_DIGITS,
zpl2.ARG_HEIGHT,
zpl2.ARG_INTERPRETATION_LINE,
zpl2.ARG_INTERPRETATION_LINE_ABOVE,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _interleaved2of5(data):
if data[:2] == 'B2':
vals = {'component_type': zpl2.BARCODE_INTERLEAVED_2_OF_5}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_HEIGHT,
zpl2.ARG_INTERPRETATION_LINE,
zpl2.ARG_INTERPRETATION_LINE_ABOVE,
zpl2.ARG_CHECK_DIGITS,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _code39(data):
if data[:2] == 'B3':
vals = {'component_type': zpl2.BARCODE_CODE_39}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_CHECK_DIGITS,
zpl2.ARG_HEIGHT,
zpl2.ARG_INTERPRETATION_LINE,
zpl2.ARG_INTERPRETATION_LINE_ABOVE,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _code49(data):
if data[:2] == 'B4':
vals = {'component_type': zpl2.BARCODE_CODE_49}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_HEIGHT,
zpl2.ARG_INTERPRETATION_LINE,
zpl2.ARG_STARTING_MODE,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _pdf417(data):
if data[:2] == 'B7':
vals = {'component_type': zpl2.BARCODE_PDF417}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_HEIGHT,
zpl2.ARG_SECURITY_LEVEL,
zpl2.ARG_COLUMNS_COUNT,
zpl2.ARG_ROWS_COUNT,
zpl2.ARG_TRUNCATE,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _ean8(data):
if data[:2] == 'B8':
vals = {'component_type': zpl2.BARCODE_EAN_8}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_HEIGHT,
zpl2.ARG_INTERPRETATION_LINE,
zpl2.ARG_INTERPRETATION_LINE_ABOVE,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _upce(data):
if data[:2] == 'B9':
vals = {'component_type': zpl2.BARCODE_UPC_E}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_HEIGHT,
zpl2.ARG_INTERPRETATION_LINE,
zpl2.ARG_INTERPRETATION_LINE_ABOVE,
zpl2.ARG_CHECK_DIGITS,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _code128(data):
if data[:2] == 'BC':
vals = {'component_type': zpl2.BARCODE_CODE_128}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_HEIGHT,
zpl2.ARG_INTERPRETATION_LINE,
zpl2.ARG_INTERPRETATION_LINE_ABOVE,
zpl2.ARG_CHECK_DIGITS,
zpl2.ARG_MODE,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _ean13(data):
if data[:2] == 'BE':
vals = {'component_type': zpl2.BARCODE_EAN_13}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_HEIGHT,
zpl2.ARG_INTERPRETATION_LINE,
zpl2.ARG_INTERPRETATION_LINE_ABOVE,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _qrcode(data):
if data[:2] == 'BQ':
vals = {'component_type': zpl2.BARCODE_QR_CODE}
args = [
zpl2.ARG_ORIENTATION,
zpl2.ARG_MODEL,
zpl2.ARG_MAGNIFICATION_FACTOR,
zpl2.ARG_ERROR_CORRECTION,
zpl2.ARG_MASK_VALUE,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _default_barcode_field(data):
if data[:2] == 'BY':
args = [
zpl2.ARG_MODULE_WIDTH,
zpl2.ARG_BAR_WIDTH_RATIO,
zpl2.ARG_HEIGHT,
]
return _compute_arg(data[2:], args)
return {}
def _field_reverse_print(data):
if data[:2] == 'FR':
return {zpl2.ARG_REVERSE_PRINT: True}
return {}
def _graphic_box(data):
if data[:2] == 'GB':
vals = {'component_type': 'rectangle'}
args = [
zpl2.ARG_WIDTH,
zpl2.ARG_HEIGHT,
zpl2.ARG_THICKNESS,
zpl2.ARG_COLOR,
zpl2.ARG_ROUNDING,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _graphic_circle(data):
if data[:2] == 'GC':
vals = {'component_type': 'circle'}
args = [
zpl2.ARG_WIDTH,
zpl2.ARG_THICKNESS,
zpl2.ARG_COLOR,
]
vals.update(_compute_arg(data[2:], args))
return vals
return {}
def _graphic_field(data):
if data[:3] == 'GFA':
vals = {}
args = [
'compression',
'total_bytes',
'total_bytes',
'bytes_per_row',
'ascii_data',
]
vals.update(_compute_arg(data[3:], args))
# Image
rawData = re.sub('[^A-F0-9]+', '', vals['ascii_data'])
rawData = binascii.unhexlify(rawData)
width = int(float(vals['bytes_per_row']) * 8)
height = int(float(vals['total_bytes']) / width) * 8
img = Image.frombytes(
'1', (width, height), rawData, 'raw').convert('L')
img = ImageOps.invert(img)
imgByteArr = io.BytesIO()
img.save(imgByteArr, format='PNG')
image = base64.b64encode(imgByteArr.getvalue())
return {
'component_type': 'graphic',
'graphic_image': image,
zpl2.ARG_WIDTH: width,
zpl2.ARG_HEIGHT: height,
}
return {}
def _get_data(data):
if data[:2] == 'FD':
return {'data': '"%s"' % data[2:]}
return {}
SUPPORTED_CODE = {
'FO': {'method': _field_origin},
'FD': {'method': _get_data},
'A': {'method': _font_format},
'FB': {'method': _field_block},
'B1': {'method': _code11},
'B2': {'method': _interleaved2of5},
'B3': {'method': _code39},
'B4': {'method': _code49},
'B7': {'method': _pdf417},
'B8': {'method': _ean8},
'B9': {'method': _upce},
'BC': {'method': _code128},
'BE': {'method': _ean13},
'BQ': {'method': _qrcode},
'BY': {
'method': _default_barcode_field,
'default': [
zpl2.BARCODE_CODE_11,
zpl2.BARCODE_INTERLEAVED_2_OF_5,
zpl2.BARCODE_CODE_39,
zpl2.BARCODE_CODE_49,
zpl2.BARCODE_PDF417,
zpl2.BARCODE_EAN_8,
zpl2.BARCODE_UPC_E,
zpl2.BARCODE_CODE_128,
zpl2.BARCODE_EAN_13,
zpl2.BARCODE_QR_CODE,
],
},
'CF': {'method': _default_font_format, 'default': ['text']},
'FR': {'method': _field_reverse_print},
'GB': {'method': _graphic_box},
'GC': {'method': _graphic_circle},
'GFA': {'method': _graphic_field},
}
class WizardImportZPl2(models.TransientModel):
_name = 'wizard.import.zpl2'
_description = 'Import ZPL2'
label_id = fields.Many2one(
comodel_name='printing.label.zpl2', string='Label',
required=True, readonly=True,)
data = fields.Text(
required=True, help='Printer used to print the labels.')
delete_component = fields.Boolean(
string='Delete existing components', default=False)
def _start_sequence(self):
sequences = self.mapped('label_id.component_ids.sequence')
if sequences:
return max(sequences) + 1
return 0
def import_zpl2(self):
Zpl2Component = self.env['printing.label.zpl2.component']
if self.delete_component:
self.mapped('label_id.component_ids').unlink()
Model = self.env['printing.label.zpl2.component']
self.model_fields = Model.fields_get()
sequence = self._start_sequence()
default = {}
for i, line in enumerate(self.data.split('\n')):
vals = {}
args = line.split('^')
for arg in args:
for key, code in SUPPORTED_CODE.items():
component_arg = code['method'](arg)
if component_arg:
if code.get('default', False):
for deft in code.get('default'):
default.update({deft: component_arg})
else:
vals.update(component_arg)
break
if vals:
if 'component_type' not in vals.keys():
vals.update({'component_type': 'text'})
if vals['component_type'] in default.keys():
vals.update(default[vals['component_type']])
vals = self._update_vals(vals)
seq = sequence + i * 10
vals.update({
'name': _('Import %s') % seq,
'sequence': seq,
'label_id': self.label_id.id,
})
Zpl2Component.create(vals)
def _update_vals(self, vals):
if 'orientation' in vals.keys() and vals['orientation'] == '':
vals['orientation'] = 'N'
# Field
component = {}
for field, value in vals.items():
if field in self.model_fields.keys():
field_type = self.model_fields[field].get('type', False)
if field_type == 'boolean':
if value == '' or value == zpl2.BOOL_NO:
value = False
else:
value = True
if field_type in ('integer', 'float'):
value = float(value)
if field == 'model':
value = int(float(value))
component.update({field: value})
return component

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_wizard_import_zpl2_form" model="ir.ui.view">
<field name="name">wizard.import.zpl2.form</field>
<field name="model">wizard.import.zpl2</field>
<field name="arch" type="xml">
<form string="Print Label">
<group>
<group>
<field name="label_id"/>
<field name="delete_component"/>
</group>
</group>
<group string="ZPL2">
<field name="data" widget="ace" nolabel="1"/>
</group>
<footer>
<button type="special" special="cancel" string="Cancel"/>
<button string="Import" type="object" name="import_zpl2" class="oe_highlight"/>
</footer>
</form>
</field>
</record>
</odoo>