Commit 01d0a7a0 authored by Jérome Perrin's avatar Jérome Perrin

base: python3 support for Base_generateBarcodeImage

also add the missing tests
parent a59da23e
Pipeline #34245 failed with stage
in 0 seconds
import io
import six
from Products.ERP5Type.Utils import str2bytes
def generateBarcodeImage(self, barcode_type, data, REQUEST=None):
# type: (str, str, HTTPRequest) -> bytes
# huBarcode's DataMatrix support has limitation for data size.
# huBarcode's QRCode support is broken.
# more 1-D barcode types can be added by pyBarcode library.
......@@ -10,24 +15,48 @@ def generateBarcodeImage(self, barcode_type, data, REQUEST=None):
stdout=PIPE,
stderr=PIPE,
close_fds=True)
output, _ = process.communicate(input=data)
output, _ = process.communicate(input=str2bytes(data))
elif barcode_type == 'ean13':
from hubarcode.ean13 import EAN13Encoder
encoder = EAN13Encoder(data)
output = encoder.get_imagedata()
if six.PY3:
import barcode.ean
import barcode.writer
fp = io.BytesIO()
barcode.ean.EuropeanArticleNumber13(
data, writer=barcode.writer.ImageWriter()
).render().save(fp, format='png')
output = fp.getvalue()
else:
from hubarcode.ean13 import EAN13Encoder
encoder = EAN13Encoder(data)
output = encoder.get_imagedata()
elif barcode_type == 'code128':
from hubarcode.code128 import Code128Encoder
encoder = Code128Encoder(data)
encoder.text = '' # get barcode image only
output = encoder.get_imagedata()
if six.PY3:
import barcode.codex
import barcode.writer
class NoTextImageWriter(barcode.writer.ImageWriter):
def _paint_text(self, *args):
pass
fp = io.BytesIO()
barcode.codex.Code128(
data, writer=NoTextImageWriter()
).render(
# we also set a font_size of 0, so that the bounding box of the
# image does not include white space for the text.
writer_options={'font_size': 0}
).save(fp, format='png')
output = fp.getvalue()
else:
from hubarcode.code128 import Code128Encoder
encoder = Code128Encoder(data)
encoder.text = '' # get barcode image only
output = encoder.get_imagedata()
elif barcode_type == 'qrcode':
import qrcode
from six.moves import cStringIO as StringIO
fp = StringIO()
img = qrcode.make(data)
fp = io.BytesIO()
img = qrcode.make(str2bytes(data))
img.save(fp, 'png')
fp.seek(0)
output = fp.read()
output = fp.getvalue()
else:
raise NotImplementedError('barcode_type=%s is not supported' % barcode_type)
if REQUEST is not None:
......
......@@ -2178,3 +2178,26 @@ class TestImage(ERP5TypeTestCase):
content_type,
(filename, image.getContentType(), content_type))
self.portal.manage_delObjects([self.id()])
class Base_generateBarcodeImageTest(ERP5TypeTestCase):
def test_datamatrix_png(self):
png = self.portal.Base_generateBarcodeImage(
barcode_type='datamatrix', data='test')
self.assertTrue(png.startswith(b'\x89PNG'), png)
def test_ean13_png(self):
png = self.portal.Base_generateBarcodeImage(
barcode_type='ean13', data='0799439112766')
self.assertTrue(png.startswith(b'\x89PNG'), png)
def test_code128_png(self):
png = self.portal.Base_generateBarcodeImage(
barcode_type='code128', data='123')
self.assertTrue(png.startswith(b'\x89PNG'), png)
def test_qrcode_png(self):
png = self.portal.Base_generateBarcodeImage(
barcode_type='qrcode', data='test')
self.assertTrue(png.startswith(b'\x89PNG'), png)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment