Files
product-slip/slips/api.py
Diaco Soltanpour 69ddfe4b60 Improve label print calibration and polish Lithium Label UI.
Align print/calibrate fields to the reference PDF (including bottom-row offset), fix partial box weights and page padding, and rename branding to Lithium Label Manager with Vazirmatn for Persian.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-01 21:38:28 +03:30

66 lines
2.3 KiB
Python

from typing import List, Optional
from django.shortcuts import get_object_or_404
from django.utils import timezone
from ninja import NinjaAPI, Query
from .models import Product, Order
from .schemas import ProductOut, ProductIn, OrderOut, OrderIn, OrderStatusIn
api = NinjaAPI(title='Lithium Label API', version='1.0.0', urls_namespace='slips_api')
# ─── Products ────────────────────────────────────────────
@api.get('/products', response=List[ProductOut], tags=['Products'])
def list_products(request):
return Product.objects.all()
@api.post('/products', response={201: ProductOut}, tags=['Products'])
def create_product(request, payload: ProductIn):
product = Product.objects.create(**payload.dict())
return 201, product
# ─── Orders ──────────────────────────────────────────────
@api.get('/orders', response=List[OrderOut], tags=['Orders'])
def list_orders(request, status: Optional[str] = Query(None)):
qs = Order.objects.select_related('product').all()
if status:
qs = qs.filter(status=status)
return qs
@api.post('/orders', response={201: OrderOut}, tags=['Orders'])
def create_order(request, payload: OrderIn):
product = get_object_or_404(Product, id=payload.product_id)
order = Order(
product=product,
batch_number=payload.batch_number,
production_date=payload.production_date,
next_control_date=payload.next_control_date,
total_weight=payload.total_weight,
canister_weight=payload.canister_weight,
canisters_per_box=payload.canisters_per_box,
)
order.save()
return 201, order
@api.patch('/orders/{order_id}', response=OrderOut, tags=['Orders'])
def update_order_status(request, order_id: int, payload: OrderStatusIn):
order = get_object_or_404(Order, id=order_id)
order.status = payload.status
if payload.status == 'done':
order.printed_at = timezone.now()
order.save()
return order
@api.delete('/orders/{order_id}', response={204: None}, tags=['Orders'])
def delete_order(request, order_id: int):
order = get_object_or_404(Order, id=order_id)
order.delete()
return 204, None