diff --git a/src/backend/InvenTree/report/templatetags/barcode.py b/src/backend/InvenTree/report/templatetags/barcode.py index 72568421ad..9bcf0fd59b 100644 --- a/src/backend/InvenTree/report/templatetags/barcode.py +++ b/src/backend/InvenTree/report/templatetags/barcode.py @@ -3,12 +3,20 @@ from django import template import barcode as python_barcode -import qrcode as python_qrcode +import qrcode.constants as ECL +from qrcode.main import QRCode import report.helpers 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'): """Convert an image into HTML renderable data. @@ -22,36 +30,44 @@ def image_data(img, fmt='PNG'): def qrcode(data, **kwargs): """Return a byte-encoded QR code image. - kwargs: - fill_color: Fill color (default = black) - back_color: Background color (default = white) - version: Default = 1 - box_size: Default = 20 - border: Default = 1 + Arguments: + data: Data to encode + + Keyword Arguments: + version: QR code version, (None to auto detect) (default = None) + 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: base64 encoded image data """ - # Construct "default" values - params = {'box_size': 20, 'border': 1, 'version': 1} - + # Construct params fill_color = kwargs.pop('fill_color', 'black') back_color = kwargs.pop('back_color', 'white') + image_format = kwargs.pop('format', 'PNG') + optimize = kwargs.pop('optimize', 1) - format = kwargs.pop('format', 'PNG') - - params.update(**kwargs) - - qr = python_qrcode.QRCode(**params) - - qr.add_data(data, optimize=20) - qr.make(fit=True) + # Construct QR code params + qr = QRCode(**{ + 'box_size': 20, + 'border': 1, + 'version': None, + **kwargs, + 'error_correction': QR_ECL_LEVEL_MAP[kwargs.get('error_correction', 'Q')], + }) + 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) # Render to byte-encoded image - return image_data(qri, fmt=format) + return image_data(qri, fmt=image_format) @register.simple_tag() diff --git a/src/frontend/src/components/editors/TemplateEditor/CodeEditor/CodeEditor.tsx b/src/frontend/src/components/editors/TemplateEditor/CodeEditor/CodeEditor.tsx index e69d8136e7..789fbad4c1 100644 --- a/src/frontend/src/components/editors/TemplateEditor/CodeEditor/CodeEditor.tsx +++ b/src/frontend/src/components/editors/TemplateEditor/CodeEditor/CodeEditor.tsx @@ -25,12 +25,18 @@ const tags: Tag[] = [ description: 'Generate a QR code image', args: ['data'], kwargs: { - fill_color: 'Fill color (default = black)', - back_color: 'Background color (default = white)', - version: 'Version (default = 1)', - box_size: 'Box size (default = 20)', - border: 'Border width (default = 1)', - format: 'Format (default = PNG)' + version: 'QR code version, (None to auto detect) (default = None)', + 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: 'base64 encoded qr code image data' },