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:
Leo
2026-05-22 19:46:36 -04:00
parent a1dfe3665a
commit ae20c0507f
2 changed files with 21 additions and 6 deletions
+14 -3
View File
@@ -31,15 +31,26 @@ from cantaloupe_sync import router as cantaloupe_router, create_sync_tables
# ─── 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"))
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_ALLOWED_EXTS = {".png", ".jpg", ".jpeg", ".svg"}
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 ───────────────────────────────────────────────────────────────
+7 -3
View File
@@ -721,10 +721,14 @@ def approve_batch(batch_id: int, request: Request):
category = str(item.get("category", "Other")).strip()
status = str(item.get("status", "active")).strip()
# Validate enums
VALID_CATEGORIES = {"Furniture", "Appliances", "Utensils & Serveware", "Equipment", "Other"}
# Validate enums against DB categories table
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"
if status not in VALID_STATUSES:
status = "active"