fix: dynamic VALID_CATEGORIES from DB categories table
Same issue as canteen-asset-tracker — hardcoded category set was
{Furniture, Appliances, Utensils, Equipment, Other}, missing all
vending-machine categories (Bev, Snack, GF Bev, GF Food, etc.).
- admin_server.py: _load_categories() from DB at startup
- cantaloupe_sync.py: reads from categories table instead of
hardcoded set (was silently downgrading GF Bev→Other on sync!)
This commit is contained in:
+14
-3
@@ -31,15 +31,26 @@ from cantaloupe_sync import router as cantaloupe_router, create_sync_tables
|
|||||||
|
|
||||||
# ─── Config ─────────────────────────────────────────────────────────────────
|
# ─── Config ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
VALID_CATEGORIES = {"Furniture", "Appliances", "Utensils & Serveware", "Equipment", "Other"}
|
|
||||||
VALID_STATUSES = {"active", "maintenance", "retired"}
|
|
||||||
VALID_ROLES = {"admin", "technician", "readonly"}
|
|
||||||
DB_PATH = os.environ.get("CANTEEN_DB_PATH", str(Path(__file__).parent / "assets.db"))
|
DB_PATH = os.environ.get("CANTEEN_DB_PATH", str(Path(__file__).parent / "assets.db"))
|
||||||
UPLOADS_DIR = Path(os.environ.get("CANTEEN_UPLOADS_DIR", str(Path(__file__).parent / ".." / "canteen-asset-tracker" / "uploads")))
|
UPLOADS_DIR = Path(os.environ.get("CANTEEN_UPLOADS_DIR", str(Path(__file__).parent / ".." / "canteen-asset-tracker" / "uploads")))
|
||||||
ICON_MAX_SIZE = 2 * 1024 * 1024 # 2 MB
|
ICON_MAX_SIZE = 2 * 1024 * 1024 # 2 MB
|
||||||
ICON_ALLOWED_EXTS = {".png", ".jpg", ".jpeg", ".svg"}
|
ICON_ALLOWED_EXTS = {".png", ".jpg", ".jpeg", ".svg"}
|
||||||
PHOTO_ALLOWED_EXTS = {".png", ".jpg", ".jpeg"}
|
PHOTO_ALLOWED_EXTS = {".png", ".jpg", ".jpeg"}
|
||||||
|
|
||||||
|
def _load_categories() -> set:
|
||||||
|
"""Load valid category names from the categories lookup table."""
|
||||||
|
try:
|
||||||
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
names = {r[0] for r in conn.execute("SELECT name FROM categories").fetchall()}
|
||||||
|
conn.close()
|
||||||
|
return names if names else {"Other"}
|
||||||
|
except Exception:
|
||||||
|
return {"Furniture", "Appliances", "Utensils & Serveware", "Equipment", "Other"}
|
||||||
|
|
||||||
|
VALID_CATEGORIES = _load_categories()
|
||||||
|
VALID_STATUSES = {"active", "maintenance", "retired"}
|
||||||
|
VALID_ROLES = {"admin", "technician", "readonly"}
|
||||||
|
|
||||||
|
|
||||||
# ─── Database ───────────────────────────────────────────────────────────────
|
# ─── Database ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
+7
-3
@@ -721,10 +721,14 @@ def approve_batch(batch_id: int, request: Request):
|
|||||||
category = str(item.get("category", "Other")).strip()
|
category = str(item.get("category", "Other")).strip()
|
||||||
status = str(item.get("status", "active")).strip()
|
status = str(item.get("status", "active")).strip()
|
||||||
|
|
||||||
# Validate enums
|
# Validate enums against DB categories table
|
||||||
VALID_CATEGORIES = {"Furniture", "Appliances", "Utensils & Serveware", "Equipment", "Other"}
|
|
||||||
VALID_STATUSES = {"active", "maintenance", "retired"}
|
VALID_STATUSES = {"active", "maintenance", "retired"}
|
||||||
if category not in VALID_CATEGORIES:
|
try:
|
||||||
|
valid_cats = {r[0] for r in conn.execute(
|
||||||
|
"SELECT name FROM categories").fetchall()}
|
||||||
|
except Exception:
|
||||||
|
valid_cats = {"Other"}
|
||||||
|
if category not in valid_cats:
|
||||||
category = "Other"
|
category = "Other"
|
||||||
if status not in VALID_STATUSES:
|
if status not in VALID_STATUSES:
|
||||||
status = "active"
|
status = "active"
|
||||||
|
|||||||
Reference in New Issue
Block a user