Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Xiaowu Zhang
erp5
Commits
72780c2d
Commit
72780c2d
authored
Apr 26, 2024
by
Jérome Perrin
Committed by
Arnaud Fontaine
Jul 04, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
base: python3 support for Base_generateBarcodeImage
also add the missing tests
parent
9a572a74
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
15 deletions
+66
-15
bt5/erp5_base/ExtensionTemplateItem/portal_components/extension.erp5.BarcodeUtils.py
...lateItem/portal_components/extension.erp5.BarcodeUtils.py
+43
-15
bt5/erp5_core_test/TestTemplateItem/portal_components/test.erp5.testERP5Base.py
...tTemplateItem/portal_components/test.erp5.testERP5Base.py
+23
-0
No files found.
bt5/erp5_base/ExtensionTemplateItem/portal_components/extension.erp5.BarcodeUtils.py
View file @
72780c2d
import
io
import
six
from
Products.ERP5Type.Utils
import
str2bytes
# pylint:disable=import-error
def
generateBarcodeImage
(
self
,
barcode_type
,
data
,
REQUEST
=
None
):
def
generateBarcodeImage
(
self
,
barcode_type
,
data
,
REQUEST
=
None
):
# huBarcode's DataMatrix support has limitation for data size.
# type: (str, str, HTTPRequest) -> bytes
# huBarcode's QRCode support is broken.
# more 1-D barcode types can be added by pyBarcode library.
barcode_type
=
barcode_type
.
lower
()
barcode_type
=
barcode_type
.
lower
()
if
barcode_type
==
'datamatrix'
:
if
barcode_type
==
'datamatrix'
:
from
subprocess
import
Popen
,
PIPE
from
subprocess
import
Popen
,
PIPE
...
@@ -10,26 +13,51 @@ def generateBarcodeImage(self, barcode_type, data, REQUEST=None):
...
@@ -10,26 +13,51 @@ def generateBarcodeImage(self, barcode_type, data, REQUEST=None):
stdout
=
PIPE
,
stdout
=
PIPE
,
stderr
=
PIPE
,
stderr
=
PIPE
,
close_fds
=
True
)
close_fds
=
True
)
output
,
_
=
process
.
communicate
(
input
=
data
)
output
,
_
=
process
.
communicate
(
input
=
str2bytes
(
data
)
)
elif
barcode_type
==
'ean13'
:
elif
barcode_type
==
'ean13'
:
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
from
hubarcode.ean13
import
EAN13Encoder
encoder
=
EAN13Encoder
(
data
)
encoder
=
EAN13Encoder
(
data
)
output
=
encoder
.
get_imagedata
()
output
=
encoder
.
get_imagedata
()
elif
barcode_type
==
'code128'
:
elif
barcode_type
==
'code128'
:
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
from
hubarcode.code128
import
Code128Encoder
encoder
=
Code128Encoder
(
data
)
encoder
=
Code128Encoder
(
data
)
encoder
.
text
=
''
# get barcode image only
encoder
.
text
=
''
# get barcode image only
output
=
encoder
.
get_imagedata
()
output
=
encoder
.
get_imagedata
()
elif
barcode_type
==
'qrcode'
:
elif
barcode_type
==
'qrcode'
:
import
qrcode
import
qrcode
from
cStringIO
import
StringIO
fp
=
io
.
BytesIO
()
fp
=
StringIO
()
img
=
qrcode
.
make
(
str2bytes
(
data
))
img
=
qrcode
.
make
(
data
)
img
.
save
(
fp
,
'png'
)
img
.
save
(
fp
,
'png'
)
fp
.
seek
(
0
)
fp
.
seek
(
0
)
output
=
fp
.
read
()
output
=
fp
.
getvalue
()
else
:
else
:
raise
NotImplementedError
(
'barcode_type=%s is not supported'
%
barcode_type
)
raise
NotImplementedError
(
'barcode_type=%s is not supported'
%
barcode_type
)
if
REQUEST
is
not
None
:
if
REQUEST
is
not
None
:
REQUEST
.
RESPONSE
.
setHeader
(
'Content-Type'
,
'image/png'
)
REQUEST
.
RESPONSE
.
setHeader
(
'Content-Type'
,
'image/png'
)
return
output
return
output
# pylint:enable=import-error
bt5/erp5_core_test/TestTemplateItem/portal_components/test.erp5.testERP5Base.py
View file @
72780c2d
...
@@ -2178,3 +2178,26 @@ class TestImage(ERP5TypeTestCase):
...
@@ -2178,3 +2178,26 @@ class TestImage(ERP5TypeTestCase):
content_type
,
content_type
,
(
filename
,
image
.
getContentType
(),
content_type
))
(
filename
,
image
.
getContentType
(),
content_type
))
self
.
portal
.
manage_delObjects
([
self
.
id
()])
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'
\
x89
PNG'
),
png
)
def
test_ean13_png
(
self
):
png
=
self
.
portal
.
Base_generateBarcodeImage
(
barcode_type
=
'ean13'
,
data
=
'0799439112766'
)
self
.
assertTrue
(
png
.
startswith
(
b'
\
x89
PNG'
),
png
)
def
test_code128_png
(
self
):
png
=
self
.
portal
.
Base_generateBarcodeImage
(
barcode_type
=
'code128'
,
data
=
'123'
)
self
.
assertTrue
(
png
.
startswith
(
b'
\
x89
PNG'
),
png
)
def
test_qrcode_png
(
self
):
png
=
self
.
portal
.
Base_generateBarcodeImage
(
barcode_type
=
'qrcode'
,
data
=
'test'
)
self
.
assertTrue
(
png
.
startswith
(
b'
\
x89
PNG'
),
png
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment