91 lines
3.2 KiB
Python
91 lines
3.2 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,
|
|
CalibrationIn, CalibrationOut,
|
|
)
|
|
from .calibration import load_calibration, save_calibration, DEFAULTS, for_api
|
|
|
|
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
|
|
|
|
|
|
# ─── Calibration ─────────────────────────────────────────
|
|
|
|
@api.get('/calibration', response=CalibrationOut, tags=['Calibration'])
|
|
def get_calibration(request):
|
|
return for_api(load_calibration())
|
|
|
|
|
|
@api.put('/calibration', response=CalibrationOut, tags=['Calibration'])
|
|
def put_calibration(request, payload: CalibrationIn):
|
|
data = payload.dict()
|
|
# Drop None optional keys so merge keeps defaults only when omitted
|
|
for field, vals in data.items():
|
|
data[field] = {k: v for k, v in vals.items() if v is not None}
|
|
return for_api(save_calibration(data))
|
|
|
|
|
|
@api.post('/calibration/reset', response=CalibrationOut, tags=['Calibration'])
|
|
def reset_calibration(request):
|
|
return for_api(save_calibration(DEFAULTS))
|