From ae20c0507fc513ebcdb3f4db02811158875a20ad Mon Sep 17 00:00:00 2001 From: Leo Date: Fri, 22 May 2026 19:46:36 -0400 Subject: [PATCH] fix: dynamic VALID_CATEGORIES from DB categories table MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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!) --- admin_server.py | 17 ++++++++++++++--- cantaloupe_sync.py | 10 +++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/admin_server.py b/admin_server.py index f456a2f..3f035e0 100644 --- a/admin_server.py +++ b/admin_server.py @@ -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 ─────────────────────────────────────────────────────────────── diff --git a/cantaloupe_sync.py b/cantaloupe_sync.py index 8d0780a..ab975fb 100644 --- a/cantaloupe_sync.py +++ b/cantaloupe_sync.py @@ -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"