feat: end-point for pdf of all deal's barcodes, client and marketplace info in bill of payment
This commit is contained in:
@@ -1,26 +1,40 @@
|
||||
from typing import List, Dict
|
||||
|
||||
from barcodes.attributes import AttributeWriterFactory
|
||||
from barcodes.generator.base import BaseBarcodeGenerator
|
||||
from barcodes.pdf import PDFGenerator
|
||||
from models import ProductBarcode, Product, BarcodeTemplate
|
||||
from models import Product, BarcodeTemplate
|
||||
from schemas.barcode import PdfBarcodeGenData
|
||||
|
||||
|
||||
class DefaultBarcodeGenerator(BaseBarcodeGenerator):
|
||||
def generate(self,
|
||||
barcode: str,
|
||||
product: Product,
|
||||
template: BarcodeTemplate,
|
||||
quantity: int = 1):
|
||||
def generate(self, barcodes_data: List[Dict[str, str | Product | BarcodeTemplate | int]]):
|
||||
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)
|
||||
|
||||
pdf_barcodes_gen_data: List[PdfBarcodeGenData] = []
|
||||
|
||||
for barcode_data in barcodes_data:
|
||||
attributes = {}
|
||||
for attribute in barcode_data["template"].attributes:
|
||||
attribute_getter = AttributeWriterFactory.get_writer(attribute.key)
|
||||
if not attribute_getter:
|
||||
continue
|
||||
value = attribute_getter.get_value(barcode_data["product"])
|
||||
if not value or not value.strip():
|
||||
continue
|
||||
attributes[attribute.name] = value
|
||||
for additional_attribute in barcode_data["template"].additional_attributes:
|
||||
attributes[additional_attribute.name] = additional_attribute.value
|
||||
barcode_text = '<br/>'.join([f'{key}: {value}' for key, value in attributes.items()])
|
||||
|
||||
pdf_barcodes_gen_data.append(
|
||||
PdfBarcodeGenData(
|
||||
barcode_value=barcode_data["barcode"],
|
||||
text=barcode_text,
|
||||
num_duplicates=barcode_data["quantity"]
|
||||
)
|
||||
)
|
||||
|
||||
print(f"value = {barcode_data['barcode']}, text = {barcode_text}, num = {barcode_data['quantity']}")
|
||||
|
||||
return pdf_generator.generate(pdf_barcodes_gen_data)
|
||||
|
||||
Reference in New Issue
Block a user