Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Converter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
XCG
Odoo modules
Converter
Commits
680ffa95
Commit
680ffa95
authored
4 weeks ago
by
Axel Prel
Browse files
Options
Downloads
Patches
Plain Diff
add qrcode converter
parent
b6839072
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitlab-ci.yml
+2
-2
2 additions, 2 deletions
.gitlab-ci.yml
__init__.py
+1
-0
1 addition, 0 deletions
__init__.py
pyproject.toml
+1
-0
1 addition, 0 deletions
pyproject.toml
qr_code.py
+97
-0
97 additions, 0 deletions
qr_code.py
with
101 additions
and
2 deletions
.gitlab-ci.yml
+
2
−
2
View file @
680ffa95
...
...
@@ -9,7 +9,7 @@
mypy
:
variables
:
INSTALL_SECTION
:
"
[test,schema-validation]"
INSTALL_SECTION
:
"
[test,schema-validation
,qrcode
]"
test
:
variables
:
...
...
@@ -13,7 +13,7 @@
test
:
variables
:
INSTALL_SECTION
:
"
[test,schema-validation]"
INSTALL_SECTION
:
"
[test,schema-validation
,qrcode
]"
test-no-schema-validation
:
extends
:
test
...
...
This diff is collapsed.
Click to expand it.
__init__.py
+
1
−
0
View file @
680ffa95
...
...
@@ -54,3 +54,4 @@
)
from
.xref
import
Xref
,
JsonLD_ID
from
.image
import
ImageFile
,
ImageDataURL
from
.qr_code
import
QRCodeFile
,
QRCodeDataURL
,
stringToQRCode
,
QRToImage
This diff is collapsed.
Click to expand it.
pyproject.toml
+
1
−
0
View file @
680ffa95
...
...
@@ -19,6 +19,7 @@
[project.optional-dependencies]
schema-validation
=
[
"fastjsonschema"
]
qrcode
=
[
"qrcode"
]
doc
=
[
"sphinx"
]
test
=
[]
...
...
This diff is collapsed.
Click to expand it.
qr_code.py
0 → 100644
+
97
−
0
View file @
680ffa95
##############################################################################
#
# Converter Odoo module
# Copyright © 2020, 2025 XCG Consulting <https://xcg-consulting.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import
base64
import
io
from
collections.abc
import
Mapping
from
typing
import
Any
from
odoo
import
models
# type: ignore[import-untyped]
from
.base
import
Converter
_qrcode
:
None
|
Exception
=
None
try
:
import
qrcode
# type: ignore[import-untyped]
except
ImportError
as
e
:
_qrcode
=
e
def
QRToImage
(
qr
):
"""
Generate a image dict from a QR Code
"""
img
=
qr
.
make_image
(
fill
=
"
black
"
,
back_color
=
"
white
"
)
buffered
=
io
.
BytesIO
()
img
.
save
(
buffered
,
format
=
"
PNG
"
)
body
=
base64
.
b64encode
(
buffered
.
getvalue
()).
decode
(
"
ascii
"
)
return
{
"
body
"
:
body
,
"
mime-type
"
:
"
image/png
"
}
def
stringToQRCode
(
data
:
str
):
"""
Generate a QR code from a string and return it as a base64-encoded image.
"""
if
not
data
:
return
False
if
_qrcode
is
not
None
:
raise
_qrcode
qr
=
qrcode
.
QRCode
(
version
=
1
,
error_correction
=
qrcode
.
constants
.
ERROR_CORRECT_L
,
box_size
=
10
,
border
=
4
,
)
qr
.
add_data
(
data
)
qr
.
make
(
fit
=
True
)
return
qr
class
QRCodeFile
(
Converter
):
def
__init__
(
self
,
fieldname
):
self
.
fieldname
=
fieldname
def
odoo_to_message
(
self
,
instance
:
models
.
Model
,
ctx
:
Mapping
|
None
=
None
)
->
Any
:
value
=
getattr
(
instance
,
self
.
fieldname
)
if
not
value
:
return
{}
return
QRToImage
(
stringToQRCode
(
value
))
class
QRCodeDataURL
(
Converter
):
def
__init__
(
self
,
fieldname
):
self
.
fieldname
=
fieldname
def
odoo_to_message
(
self
,
instance
:
models
.
Model
,
ctx
:
Mapping
|
None
=
None
)
->
Any
:
value
=
getattr
(
instance
,
self
.
fieldname
)
if
not
value
:
return
""
image
=
QRToImage
(
stringToQRCode
(
value
))
return
"
data:{};base64,{}
"
.
format
(
image
[
"
mime-type
"
],
image
[
"
body
"
],
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment