27 lines
1.2 KiB
Python
27 lines
1.2 KiB
Python
from barcodes.attributes import AttributeWriterFactory
|
|
from barcodes.generator.base import BaseBarcodeGenerator
|
|
from barcodes.pdf import PDFGenerator
|
|
from models import ProductBarcode, Product, BarcodeTemplate
|
|
|
|
|
|
class DefaultBarcodeGenerator(BaseBarcodeGenerator):
|
|
def generate(self,
|
|
barcode: str,
|
|
product: Product,
|
|
template: BarcodeTemplate,
|
|
quantity: int = 1):
|
|
pdf_generator = PDFGenerator()
|
|
attributes = {}
|
|
for attribute in template.attributes:
|
|
attribute_getter = AttributeWriterFactory.get_writer(attribute.key)
|
|
if not attribute_getter:
|
|
continue
|
|
value = attribute_getter.get_value(product)
|
|
if not value or not value.strip():
|
|
continue
|
|
attributes[attribute.name] = value
|
|
for additional_attribute in template.additional_attributes:
|
|
attributes[additional_attribute.name] = additional_attribute.value
|
|
barcode_text = '<br/>'.join([f'{key}: {value}' for key, value in attributes.items()])
|
|
return pdf_generator.generate(barcode, barcode_text, num_duplicates=quantity)
|