feat: get deal barcodes pdf
This commit is contained in:
@ -5,9 +5,15 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from modules.fulfillment_base.barcodes_pdf_gen import BarcodePdfGenerator, BarcodeData
|
||||
from modules.fulfillment_base.barcodes_pdf_gen.types import PdfBarcodeImageGenData
|
||||
from modules.fulfillment_base.models import Product
|
||||
from modules.fulfillment_base.repositories import ProductRepository
|
||||
from modules.fulfillment_base.schemas.product import GetProductBarcodePdfRequest
|
||||
from modules.fulfillment_base.models import Product, DealProduct
|
||||
from modules.fulfillment_base.repositories import (
|
||||
ProductRepository,
|
||||
DealProductRepository,
|
||||
)
|
||||
from modules.fulfillment_base.schemas.product import (
|
||||
GetProductBarcodePdfRequest,
|
||||
GetDealBarcodesPdfRequest,
|
||||
)
|
||||
|
||||
|
||||
class BarcodePrinterService:
|
||||
@ -16,7 +22,7 @@ class BarcodePrinterService:
|
||||
def __init__(self, session: AsyncSession):
|
||||
self.session = session
|
||||
|
||||
async def generate_pdf(
|
||||
async def generate_product_pdf(
|
||||
self, request: GetProductBarcodePdfRequest
|
||||
) -> tuple[str, BytesIO]:
|
||||
product: Product = await ProductRepository(self.session).get_by_id(
|
||||
@ -41,9 +47,50 @@ class BarcodePrinterService:
|
||||
generator = BarcodePdfGenerator(size.width, size.height)
|
||||
return filename, await generator.generate([barcode_data])
|
||||
|
||||
async def generate_base64(
|
||||
async def generate_product_base64(
|
||||
self, request: GetProductBarcodePdfRequest
|
||||
) -> tuple[str, str]:
|
||||
filename, pdf_buffer = await self.generate_pdf(request)
|
||||
filename, pdf_buffer = await self.generate_product_pdf(request)
|
||||
base64_string = base64.b64encode(pdf_buffer.read()).decode("utf-8")
|
||||
return filename, base64_string
|
||||
|
||||
async def generate_deal_pdf(
|
||||
self, request: GetDealBarcodesPdfRequest
|
||||
) -> tuple[str, BytesIO]:
|
||||
deal_product_repo = DealProductRepository(self.session)
|
||||
deal_products: list[DealProduct] = await deal_product_repo.get_all(
|
||||
request.deal_id
|
||||
)
|
||||
|
||||
if len(deal_products) == 0:
|
||||
return "no_content.pdf", BytesIO()
|
||||
|
||||
barcodes_data: list[BarcodeData | PdfBarcodeImageGenData] = []
|
||||
|
||||
for deal_product in deal_products:
|
||||
if deal_product.product.barcode_image:
|
||||
barcode_data: PdfBarcodeImageGenData = {
|
||||
"barcode_image_url": deal_product.product.barcode_image.image_url,
|
||||
"num_duplicates": deal_product.quantity,
|
||||
}
|
||||
barcodes_data.append(barcode_data)
|
||||
elif len(deal_product.product.barcodes) > 0:
|
||||
barcode_data: BarcodeData = {
|
||||
"barcode": deal_product.product.barcodes[0].barcode,
|
||||
"template": deal_product.product.barcode_template,
|
||||
"product": deal_product.product,
|
||||
"num_duplicates": deal_product.quantity,
|
||||
}
|
||||
barcodes_data.append(barcode_data)
|
||||
|
||||
size = deal_products[0].product.barcode_template.size
|
||||
generator = BarcodePdfGenerator(size.width, size.height)
|
||||
filename = "deal_barcodes.pdf"
|
||||
return filename, await generator.generate(barcodes_data)
|
||||
|
||||
async def generate_deal_base64(
|
||||
self, request: GetDealBarcodesPdfRequest
|
||||
) -> tuple[str, str]:
|
||||
filename, pdf_buffer = await self.generate_deal_pdf(request)
|
||||
base64_string = base64.b64encode(pdf_buffer.read()).decode("utf-8")
|
||||
return filename, base64_string
|
||||
|
||||
Reference in New Issue
Block a user