This commit is contained in:
2026-08-29 10:52:44 +00:00
parent 73fba17711
commit 59bf2c5ca9
11 changed files with 25 additions and 33 deletions
+5 -5
View File
@@ -8,8 +8,9 @@ PROJECT_NAME=barker
MAX_WORKERS=4
REDIS_HOST=localhost
REDIS_PORT=6379
NATS_URL=nats://localhost:4222
SUBJECT="barker.print"
STREAM="barker_print_stream"
# openssl rand -hex 32
SECRET_KEY=611f9393e58f85521d16d9497bfd847ced70f2b99e12a9c6af40b7bd05cc7b1d
@@ -24,6 +25,5 @@ TIMEZONE_OFFSET_MINUTES=330
ALEMBIC_LOG_LEVEL=INFO
ALEMBIC_SQLALCHEMY_LOG_LEVEL=WARN
WORKER_REDIS_HOST=
WORKER_REDIS_PORT=6379
WORKER_QUEUE_NAME=""
FASTAPI_ENV=development
+2 -2
View File
@@ -1,9 +1,9 @@
{
"python.defaultInterpreterPath": "/home/tanshu/programming/barker/barker/.venv/bin/python",
"python.defaultInterpreterPath": "barker/.venv/bin/python",
"python.terminal.useEnvFile": true,
"python.interpreterPath": "/home/tanshu/programming/barker/barker/.venv/bin/python",
"python.venvFolders": [
"barker/.venv",
"frank/.venv",
".venv"
],
"python.venvPath": "${workspaceFolder}/barker",
+2 -1
View File
@@ -22,7 +22,8 @@ class Settings(BaseSettings):
ALEMBIC_SQLALCHEMY_LOG_LEVEL: str = "WARN"
NATS_URL: str = "nats://127.0.0.1:4222"
WORKER_QUEUE_NAME: str = "arq:queue"
SUBJECT: str = "barker.print"
STREAM: str = "barker_print_stream"
model_config = SettingsConfigDict(case_sensitive=True, env_file=".env")
+8 -8
View File
@@ -19,24 +19,24 @@ class RoutedJetStreamBroker(PullBasedJetStreamBroker):
broker = RoutedJetStreamBroker(
settings.NATS_URL,
subject="barker.print.>",
stream_name="barker_print_stream",
subject=f"{settings.SUBJECT}.>",
stream_name=f"{settings.STREAM}",
stream_config=StreamConfig(
name="barker_print_stream",
subjects=["barker.print.>"],
name=f"{settings.STREAM}",
subjects=[f"{settings.SUBJECT}.>"],
),
)
main_loop: asyncio.AbstractEventLoop | None = None
@broker.task(task_name="sent_to_printer")
async def sent_to_printer(data: str, address: str, cut_code: str) -> None:
@broker.task(task_name="nat_print")
async def nat_print(data: str, cut_code: str) -> None:
pass
def enqueue_print_job_sync(printer_name: str, data: str, address: str, cut_code: str) -> None:
coro = sent_to_printer.kicker().with_labels(queue=f"barker.print.{printer_name}").kiq(data, address, cut_code)
def enqueue_print_job_sync(printer_name: str, data: str, cut_code: str) -> None:
coro = nat_print.kicker().with_labels(queue=f"{settings.SUBJECT}.{printer_name}").kiq(data, cut_code)
if main_loop is not None and main_loop.is_running():
asyncio.run_coroutine_threadsafe(coro, main_loop)
+2 -2
View File
@@ -49,9 +49,9 @@ def print_bill(voucher_id: uuid.UUID, db: Session) -> None:
if len(bills) > 1:
total = design_total(voucher, regimes, db)
enqueue_print_job_sync(printer.name, total, printer.address, printer.cut_code)
enqueue_print_job_sync(printer.name, total, printer.cut_code)
for bill in bills:
enqueue_print_job_sync(printer.name, bill, printer.address, printer.cut_code)
enqueue_print_job_sync(printer.name, bill, printer.cut_code)
def get_inventories(voucher: Voucher) -> list[Inventory]:
+2 -2
View File
@@ -25,8 +25,8 @@ def print_cashier_report(report: CashierReport, device_id: uuid.UUID, db: Sessio
.where(SectionPrinter.sale_category_id == None) # noqa: E711
).scalar_one()
enqueue_print_job_sync(printer.name, summary, printer.address, printer.cut_code)
enqueue_print_job_sync(printer.name, details, printer.address, printer.cut_code)
enqueue_print_job_sync(printer.name, summary, printer.cut_code)
enqueue_print_job_sync(printer.name, details, printer.cut_code)
def design_cashier_report_summary(report: CashierReport) -> str:
+1 -1
View File
@@ -21,7 +21,7 @@ def print_discount_report(report: DiscountReport, device_id: uuid.UUID, db: Sess
.where(SectionPrinter.sale_category_id == None) # noqa: E711
).scalar_one()
enqueue_print_job_sync(printer.name, data, printer.address, printer.cut_code)
enqueue_print_job_sync(printer.name, data, printer.cut_code)
def design_discount_report(report: DiscountReport) -> str:
+1 -1
View File
@@ -83,4 +83,4 @@ def print_kot(voucher_id: uuid.UUID, db: Session) -> None:
printer, items = value
for c in range(int(copies)):
data = design_kot(voucher, kot, items, c)
enqueue_print_job_sync(printer.name, data, printer.address, printer.cut_code)
enqueue_print_job_sync(printer.name, data, printer.cut_code)
@@ -23,7 +23,7 @@ def print_product_sale_report(report: ProductSaleReport, device_id: uuid.UUID, d
.where(SectionPrinter.sale_category_id == None) # noqa: E711
).scalar_one()
enqueue_print_job_sync(printer.name, data, printer.address, printer.cut_code)
enqueue_print_job_sync(printer.name, data, printer.cut_code)
def design_product_sale_report(report: ProductSaleReport) -> str:
+1 -1
View File
@@ -24,7 +24,7 @@ def print_sale_report(report: SaleReport, device_id: uuid.UUID, db: Session) ->
.where(SectionPrinter.sale_category_id == None) # noqa: E711
).scalar_one()
enqueue_print_job_sync(printer.name, data, printer.address, printer.cut_code)
enqueue_print_job_sync(printer.name, data, printer.cut_code)
def design_sale_report(report: SaleReport) -> str:
@@ -82,15 +82,6 @@ def check_gaps(data: TemporalProduct) -> None:
skus: dict[uuid.UUID, list[SkuModel]] = defaultdict(list)
for sku in data.skus:
skus[sku.id_].append(sku)
for sku_list in skus.values():
if (
data.products[0].valid_from != sku_list[0].valid_from
or data.products[-1].valid_till != sku_list[-1].valid_till
):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Product and SKU versions validity do not match",
)
for i, p_item in enumerate(data.products[1:], start=1):
if data.products[i - 1].valid_till + timedelta(days=1) != p_item.valid_from: # type: ignore[operator]
raise HTTPException(