mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-16 12:05:53 +00:00
improve qrcode templatetag
This commit is contained in:
@ -3,12 +3,20 @@
|
|||||||
from django import template
|
from django import template
|
||||||
|
|
||||||
import barcode as python_barcode
|
import barcode as python_barcode
|
||||||
import qrcode as python_qrcode
|
import qrcode.constants as ECL
|
||||||
|
from qrcode.main import QRCode
|
||||||
|
|
||||||
import report.helpers
|
import report.helpers
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
|
||||||
|
QR_ECL_LEVEL_MAP = {
|
||||||
|
'L': ECL.ERROR_CORRECT_L,
|
||||||
|
'M': ECL.ERROR_CORRECT_M,
|
||||||
|
'Q': ECL.ERROR_CORRECT_Q,
|
||||||
|
'H': ECL.ERROR_CORRECT_H,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def image_data(img, fmt='PNG'):
|
def image_data(img, fmt='PNG'):
|
||||||
"""Convert an image into HTML renderable data.
|
"""Convert an image into HTML renderable data.
|
||||||
@ -22,36 +30,44 @@ def image_data(img, fmt='PNG'):
|
|||||||
def qrcode(data, **kwargs):
|
def qrcode(data, **kwargs):
|
||||||
"""Return a byte-encoded QR code image.
|
"""Return a byte-encoded QR code image.
|
||||||
|
|
||||||
kwargs:
|
Arguments:
|
||||||
fill_color: Fill color (default = black)
|
data: Data to encode
|
||||||
back_color: Background color (default = white)
|
|
||||||
version: Default = 1
|
Keyword Arguments:
|
||||||
box_size: Default = 20
|
version: QR code version, (None to auto detect) (default = None)
|
||||||
border: Default = 1
|
error_correction: Error correction level (L: 7%, M: 15%, Q: 25%, H: 30%) (default = 'Q')
|
||||||
|
box_size: pixel dimensions for one black square pixel in the QR code (default = 20)
|
||||||
|
border: count white QR square pixels around the qr code, needed as padding (default = 1)
|
||||||
|
optimize: data will be split into multiple chunks of at least this length using different modes (text, alphanumeric, binary) to optimize the QR code size. Set to `0` to disable. (default = 1)
|
||||||
|
format: Image format (default = 'PNG')
|
||||||
|
fill_color: Fill color (default = "black")
|
||||||
|
back_color: Background color (default = "white")
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
base64 encoded image data
|
base64 encoded image data
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# Construct "default" values
|
# Construct params
|
||||||
params = {'box_size': 20, 'border': 1, 'version': 1}
|
|
||||||
|
|
||||||
fill_color = kwargs.pop('fill_color', 'black')
|
fill_color = kwargs.pop('fill_color', 'black')
|
||||||
back_color = kwargs.pop('back_color', 'white')
|
back_color = kwargs.pop('back_color', 'white')
|
||||||
|
image_format = kwargs.pop('format', 'PNG')
|
||||||
|
optimize = kwargs.pop('optimize', 1)
|
||||||
|
|
||||||
format = kwargs.pop('format', 'PNG')
|
# Construct QR code params
|
||||||
|
qr = QRCode(**{
|
||||||
params.update(**kwargs)
|
'box_size': 20,
|
||||||
|
'border': 1,
|
||||||
qr = python_qrcode.QRCode(**params)
|
'version': None,
|
||||||
|
**kwargs,
|
||||||
qr.add_data(data, optimize=20)
|
'error_correction': QR_ECL_LEVEL_MAP[kwargs.get('error_correction', 'Q')],
|
||||||
qr.make(fit=True)
|
})
|
||||||
|
qr.add_data(data, optimize=optimize)
|
||||||
|
qr.make(fit=False) # if version is None, it will automatically use fit=True
|
||||||
|
|
||||||
qri = qr.make_image(fill_color=fill_color, back_color=back_color)
|
qri = qr.make_image(fill_color=fill_color, back_color=back_color)
|
||||||
|
|
||||||
# Render to byte-encoded image
|
# Render to byte-encoded image
|
||||||
return image_data(qri, fmt=format)
|
return image_data(qri, fmt=image_format)
|
||||||
|
|
||||||
|
|
||||||
@register.simple_tag()
|
@register.simple_tag()
|
||||||
|
@ -25,12 +25,18 @@ const tags: Tag[] = [
|
|||||||
description: 'Generate a QR code image',
|
description: 'Generate a QR code image',
|
||||||
args: ['data'],
|
args: ['data'],
|
||||||
kwargs: {
|
kwargs: {
|
||||||
fill_color: 'Fill color (default = black)',
|
version: 'QR code version, (None to auto detect) (default = None)',
|
||||||
back_color: 'Background color (default = white)',
|
error_correction:
|
||||||
version: 'Version (default = 1)',
|
"Error correction level (L: 7%, M: 15%, Q: 25%, H: 30%) (default = 'Q')",
|
||||||
box_size: 'Box size (default = 20)',
|
box_size:
|
||||||
border: 'Border width (default = 1)',
|
'pixel dimensions for one black square pixel in the QR code (default = 20)',
|
||||||
format: 'Format (default = PNG)'
|
border:
|
||||||
|
'count white QR square pixels around the qr code, needed as padding (default = 1)',
|
||||||
|
optimize:
|
||||||
|
'data will be split into multiple chunks of at least this length using different modes (text, alphanumeric, binary) to optimize the QR code size. Set to `0` to disable. (default = 1)',
|
||||||
|
format: "Image format (default = 'PNG')",
|
||||||
|
fill_color: 'Fill color (default = "black")',
|
||||||
|
back_color: 'Background color (default = "white")'
|
||||||
},
|
},
|
||||||
returns: 'base64 encoded qr code image data'
|
returns: 'base64 encoded qr code image data'
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user