mirror of
https://github.com/inventree/InvenTree.git
synced 2026-07-04 06:00:38 +00:00
[report] Tweak datamatrix helper (#12240)
* [report] Tweak datamatrix helper - Add support for "rectangular" display - Support different image output formats * Fix unit tests
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
"""Template tags for rendering various barcodes."""
|
"""Template tags for rendering various barcodes."""
|
||||||
|
|
||||||
from django import template
|
from django import template
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
import barcode as python_barcode
|
import barcode as python_barcode
|
||||||
@@ -70,7 +71,7 @@ def qrcode(data: str, **kwargs) -> str:
|
|||||||
data = str(data).strip()
|
data = str(data).strip()
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
raise ValueError("No data provided to 'qrcode' template tag")
|
raise ValidationError("qrcode: No data provided to 'qrcode' template tag")
|
||||||
|
|
||||||
# Extract other arguments from kwargs
|
# Extract other arguments from kwargs
|
||||||
fill_color = kwargs.pop('fill_color', 'black')
|
fill_color = kwargs.pop('fill_color', 'black')
|
||||||
@@ -115,7 +116,7 @@ def barcode(data: str, barcode_class='code128', **kwargs) -> str:
|
|||||||
data = str(data).strip()
|
data = str(data).strip()
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
raise ValueError("No data provided to 'barcode' template tag")
|
raise ValidationError("barcode: No data provided to 'barcode' template tag")
|
||||||
|
|
||||||
constructor = python_barcode.get_barcode_class(barcode_class)
|
constructor = python_barcode.get_barcode_class(barcode_class)
|
||||||
|
|
||||||
@@ -134,17 +135,26 @@ def barcode(data: str, barcode_class='code128', **kwargs) -> str:
|
|||||||
|
|
||||||
|
|
||||||
@register.simple_tag()
|
@register.simple_tag()
|
||||||
def datamatrix(data: str, **kwargs) -> str:
|
def datamatrix(
|
||||||
|
data: str,
|
||||||
|
rectangular: bool = False,
|
||||||
|
fill_color: str = 'black',
|
||||||
|
back_color: str = 'white',
|
||||||
|
scale: float = 1.0,
|
||||||
|
border: int = 1,
|
||||||
|
fmt: str = 'PNG',
|
||||||
|
**kwargs,
|
||||||
|
) -> str:
|
||||||
"""Render a DataMatrix barcode.
|
"""Render a DataMatrix barcode.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
data: Data to encode
|
data: Data to encode
|
||||||
|
rectangular: Whether to generate a rectangular DataMatrix (default = False)
|
||||||
Keyword Arguments:
|
fill_color: Foreground color (default = 'black')
|
||||||
fill_color (str): Foreground color (default = 'black')
|
back_color: Background color (default = 'white')
|
||||||
back_color (str): Background color (default = 'white')
|
scale: Scaling factor (default = 1)
|
||||||
scale (float): Matrix scaling factor (default = 1)
|
border: Border width (default = 1)
|
||||||
border (int): Border width (default = 1)
|
fmt: Generated image format (default = 'PNG')
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
image (str): base64 encoded image data
|
image (str): base64 encoded image data
|
||||||
@@ -154,14 +164,11 @@ def datamatrix(data: str, **kwargs) -> str:
|
|||||||
data = str(data).strip()
|
data = str(data).strip()
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
raise ValueError("No data provided to 'datamatrix' template tag")
|
raise ValidationError(
|
||||||
|
"datamatrix: No data provided to 'datamatrix' template tag"
|
||||||
|
)
|
||||||
|
|
||||||
dm = DataMatrix(data)
|
dm = DataMatrix(data, rect=rectangular)
|
||||||
|
|
||||||
fill_color = kwargs.pop('fill_color', 'black')
|
|
||||||
back_color = kwargs.pop('back_color', 'white')
|
|
||||||
|
|
||||||
border = kwargs.pop('border', 1)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
border = int(border)
|
border = int(border)
|
||||||
@@ -180,8 +187,6 @@ def datamatrix(data: str, **kwargs) -> str:
|
|||||||
except Exception:
|
except Exception:
|
||||||
bg = ImageColor.getcolor('white', 'RGB')
|
bg = ImageColor.getcolor('white', 'RGB')
|
||||||
|
|
||||||
scale = kwargs.pop('scale', 1)
|
|
||||||
|
|
||||||
height = len(dm.matrix) + 2 * border
|
height = len(dm.matrix) + 2 * border
|
||||||
width = len(dm.matrix[0]) + 2 * border
|
width = len(dm.matrix[0]) + 2 * border
|
||||||
|
|
||||||
@@ -193,7 +198,8 @@ def datamatrix(data: str, **kwargs) -> str:
|
|||||||
if value:
|
if value:
|
||||||
img.putpixel((x + border, y + border), fg)
|
img.putpixel((x + border, y + border), fg)
|
||||||
|
|
||||||
if scale != 1:
|
img = img.resize(
|
||||||
img = img.resize((int(width * scale), int(height * scale)))
|
(int(width * scale), int(height * scale)), Image.Resampling.NEAREST
|
||||||
|
)
|
||||||
|
|
||||||
return image_data(img, fmt='PNG')
|
return image_data(img, fmt=fmt)
|
||||||
|
|||||||
@@ -440,7 +440,6 @@ class ReportTagTest(PartImageTestMixin, InvenTreeTestCase):
|
|||||||
]
|
]
|
||||||
|
|
||||||
for tz, fmt, locale, expected in tests:
|
for tz, fmt, locale, expected in tests:
|
||||||
print(tz, fmt, locale, expected)
|
|
||||||
result = report_tags.format_datetime(
|
result = report_tags.format_datetime(
|
||||||
time, timezone=tz, fmt=fmt, locale=locale
|
time, timezone=tz, fmt=fmt, locale=locale
|
||||||
)
|
)
|
||||||
@@ -857,7 +856,7 @@ class BarcodeTagTest(TestCase):
|
|||||||
self.assertTrue(barcode.startswith('data:image/bmp;'))
|
self.assertTrue(barcode.startswith('data:image/bmp;'))
|
||||||
|
|
||||||
# Test empty tag
|
# Test empty tag
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValidationError):
|
||||||
barcode_tags.barcode('')
|
barcode_tags.barcode('')
|
||||||
|
|
||||||
def test_qrcode(self):
|
def test_qrcode(self):
|
||||||
@@ -877,7 +876,7 @@ class BarcodeTagTest(TestCase):
|
|||||||
self.assertEqual(len(qrcode), 309720)
|
self.assertEqual(len(qrcode), 309720)
|
||||||
|
|
||||||
# Test empty tag
|
# Test empty tag
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValidationError):
|
||||||
barcode_tags.qrcode('')
|
barcode_tags.qrcode('')
|
||||||
|
|
||||||
def test_clean_barcode(self):
|
def test_clean_barcode(self):
|
||||||
@@ -908,7 +907,7 @@ class BarcodeTagTest(TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Test empty tag
|
# Test empty tag
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValidationError):
|
||||||
barcode_tags.datamatrix('')
|
barcode_tags.datamatrix('')
|
||||||
|
|
||||||
# Failure cases with wrong args
|
# Failure cases with wrong args
|
||||||
|
|||||||
Reference in New Issue
Block a user