Gegenstands-Produkte koennen als Einzelstuecke gefuehrt werden (Product.individual):
jedes physische Stueck ist ein Item mit eigener kurzer UID (fuer QR), eigenem
Lagerort, Kaufdatum, Garantie, Bezugsquelle und Notiz. So laesst sich dasselbe
Modell mehrfach getrennt fuehren (Powerbank 2024 + 2025).
Neu: models.Item + Product.individual (+Migration), Schemas, services/items.py
(UID-Erzeugung, Anreicherung), routers/items.py (CRUD, /items/by-uid/{uid} zur
QR-Aufloesung, /items/{id}/remove mit Grund als Bewegung). current_stock zaehlt
bei Einzelstueck-Produkten die Items. Tests + HTTP-Smoke gruen.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
"""Einzelstücke (Items): UID-Erzeugung und Anreicherung für die Ausgabe."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import secrets
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..models import Item
|
|
from ..schemas import ItemOut
|
|
|
|
# Ohne 0/O/1/I/L, damit die UID auf einem Etikett eindeutig lesbar bleibt.
|
|
_ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"
|
|
|
|
|
|
def generate_uid(db: Session, length: int = 8) -> str:
|
|
"""Kurze, eindeutige UID erzeugen (bei Kollision neu würfeln)."""
|
|
while True:
|
|
code = "".join(secrets.choice(_ALPHABET) for _ in range(length))
|
|
if db.query(Item).filter(Item.uid == code).first() is None:
|
|
return code
|
|
|
|
|
|
def item_to_out(item: Item) -> ItemOut:
|
|
"""ItemOut inkl. Lagerort-/Shop-/Produktnamen für die Anzeige."""
|
|
out = ItemOut.model_validate(item)
|
|
out.location_name = item.location.name if item.location else None
|
|
out.shop_name = item.shop.name if item.shop else None
|
|
out.product_name = item.product.name if item.product else None
|
|
out.product_brand = item.product.brand if item.product else None
|
|
return out
|