From 172cc678d37cdccea64220e418560291e6d426bd Mon Sep 17 00:00:00 2001 From: Piotr Oleszczyk Date: Sun, 22 Jun 2025 20:10:25 +0200 Subject: [PATCH] handle data without due date --- app.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index de38850..8435fc8 100644 --- a/app.py +++ b/app.py @@ -27,7 +27,7 @@ class PrintRequest(BaseModel): # grocy payload: grocycode: str = Field(..., description="Raw GrocyCode") product: str = Field(..., description="Product name") - due_date: str = Field(None, description="Due date from stock entry") + due_date: Optional[str] = Field(None, description="Due date from stock entry") class Config: # ignore any other fields Grocy might send @@ -35,7 +35,9 @@ class PrintRequest(BaseModel): @lru_cache(maxsize=256) -def make_code128(grocycode: str, product_name: str, due_date: Optional[str]) -> Image.Image: +def make_code128( + grocycode: str, product_name: str, due_date: Optional[str] +) -> Image.Image: # 1) Instantiate the barcode object (checksum auto-added for Code128) code = Code128(grocycode, writer=ImageWriter()) @@ -49,7 +51,9 @@ def make_code128(grocycode: str, product_name: str, due_date: Optional[str]) -> "text_distance": 5.0, # gap between bars and text # you can also pass 'format': 'PNG' here if needed }, - text=" | ".join([product_name, due_date]), # explicitly draw your data string under the bars + text=( + " | ".join([product_name, due_date]) if due_date else product_name + ), # explicitly draw your data string under the bars ) # 3) Convert to 1-bit for your Brother QL workflow @@ -128,7 +132,9 @@ async def print_from_grocy(req: PrintRequest): try: loop = asyncio.get_running_loop() code_img, dm_img = await asyncio.gather( - loop.run_in_executor(None, make_code128, req.grocycode, req.product, req.due_date), + loop.run_in_executor( + None, make_code128, req.grocycode, req.product, req.due_date + ), loop.run_in_executor(None, make_datamatrix, req.grocycode), ) label_img = compose_label([code_img, dm_img])