RECORDGOD/app/collections_routes.py
type-two 10af3464ac feat(disc): mirror WowPlatter Discogs metadata into recordgod (disc_sync.py) + locator matches on collection genre/style ids
- disc_sync.py: MariaDB clone -> recordgod disc_* (30.8k releases, genre/style/label/format/identifier/artist/track/master), keeping WowPlatter's own genre/style ids
- locator reads disc_release.genre_ids/style_ids + parses collection 'ID,0' id format
- images stay with DealGod; masters sparse (backfill from Ultra later)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 21:19:47 +10:00

199 lines
9.2 KiB
Python

import json
from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel
from sqlalchemy import text
from .auth import require_token
from .db import get_db
# Collections = named crate zones + filing rules (genre/style/label/format + price/year) + priority.
# Migrated verbatim into virtual_collection. Powers the Collections tab AND the fallback locator:
# a record with no exact crate still resolves to "where it should be" via the highest-priority matching rule.
# See RECORDGOD_NAVIGATOR_PLAN.md §6b. (JSON list columns came across as text — parse in Python.)
router = APIRouter(prefix="/nav", tags=["collections"])
def _arr(v):
if isinstance(v, list):
return v
try:
return json.loads(v) if v else []
except Exception:
return []
def _ids(v):
"""Collection genre_ids/style_ids are stored as "ID,0" strings (WowPlatter's own ids) — take the lead int."""
out = []
for x in _arr(v):
try:
out.append(int(str(x).split(",")[0]))
except Exception:
pass
return out
class CollectionIn(BaseModel):
id: int | None = None
name: str
color: str = "#3498db"
crate_ids: list[int] = []
genre_ids: list[int] = []
style_ids: list[int] = []
label_ids: list[int] = []
format_descriptions: list[str] = []
price_min: float | None = None
price_max: float | None = None
year_min: int | None = None
year_max: int | None = None
sort_method: str = "alpha_artist"
priority: int = 0
is_active: bool = True
@router.get("/collections")
async def list_collections(ident=Depends(require_token), db=Depends(get_db)):
rows = (await db.execute(text("""
SELECT id, name, color, priority, (is_active::int <> 0) AS is_active,
crate_ids, style_ids, genre_ids, price_min::float, price_max::float, year_min, year_max
FROM virtual_collection ORDER BY priority DESC, name
"""))).mappings()
out = []
for r in rows:
d = dict(r)
out.append({"id": d["id"], "name": d["name"], "color": d["color"], "priority": d["priority"],
"is_active": d["is_active"], "crate_count": len(_arr(d["crate_ids"])),
"n_styles": len(_arr(d["style_ids"])), "n_genres": len(_arr(d["genre_ids"])),
"price_min": d["price_min"], "price_max": d["price_max"],
"year_min": d["year_min"], "year_max": d["year_max"]})
return {"collections": out}
@router.get("/collections/{cid}")
async def get_collection(cid: int, ident=Depends(require_token), db=Depends(get_db)):
c = (await db.execute(text("SELECT * FROM virtual_collection WHERE id=:i"), {"i": cid})).mappings().first()
if not c:
raise HTTPException(404, "not found")
crate_ids = _arr(c["crate_ids"])
crates = []
if crate_ids:
crates = [dict(r) for r in (await db.execute(text("""
SELECT id, name, label_text,
(SELECT count(*) FROM inventory i WHERE i.crate_id = c.id AND i.in_stock) AS items
FROM virtual_crate c WHERE c.id = ANY(:ids)
"""), {"ids": crate_ids})).mappings()]
order = {cid_: n for n, cid_ in enumerate(crate_ids)} # keep click order
crates.sort(key=lambda x: order.get(x["id"], 999))
return {"collection": {
"id": c["id"], "name": c["name"], "color": c["color"], "priority": c["priority"],
"is_active": bool(c["is_active"]), "sort_method": c["sort_method"],
"crate_ids": crate_ids, "genre_ids": _arr(c["genre_ids"]), "style_ids": _arr(c["style_ids"]),
"label_ids": _arr(c["label_ids"]), "format_descriptions": _arr(c["format_descriptions"]),
"price_min": float(c["price_min"]) if c["price_min"] is not None else None,
"price_max": float(c["price_max"]) if c["price_max"] is not None else None,
"year_min": c["year_min"], "year_max": c["year_max"]},
"crates": crates}
@router.post("/collections")
async def save_collection(body: CollectionIn, ident=Depends(require_token), db=Depends(get_db)):
p = {"name": body.name, "color": body.color, "crates": json.dumps(body.crate_ids),
"genres": json.dumps(body.genre_ids), "styles": json.dumps(body.style_ids),
"labels": json.dumps(body.label_ids), "formats": json.dumps(body.format_descriptions),
"pmin": body.price_min, "pmax": body.price_max, "ymin": body.year_min, "ymax": body.year_max,
"sort": body.sort_method, "prio": body.priority, "active": 1 if body.is_active else 0}
if body.id:
await db.execute(text("""
UPDATE virtual_collection SET name=:name, color=:color, crate_ids=:crates, genre_ids=:genres,
style_ids=:styles, label_ids=:labels, format_descriptions=:formats, price_min=:pmin,
price_max=:pmax, year_min=:ymin, year_max=:ymax, sort_method=:sort, priority=:prio,
is_active=:active, updated_at=now() WHERE id=:id
"""), {**p, "id": body.id})
cid = body.id
else:
cid = (await db.execute(text("""
INSERT INTO virtual_collection (name, color, crate_ids, genre_ids, style_ids, label_ids,
format_descriptions, price_min, price_max, year_min, year_max, sort_method, priority, is_active)
VALUES (:name,:color,:crates,:genres,:styles,:labels,:formats,:pmin,:pmax,:ymin,:ymax,:sort,:prio,:active)
RETURNING id
"""), p)).first()[0]
await db.commit()
return {"ok": True, "id": cid}
@router.delete("/collections/{cid}")
async def delete_collection(cid: int, ident=Depends(require_token), db=Depends(get_db)):
await db.execute(text("DELETE FROM virtual_collection WHERE id=:i"), {"i": cid})
await db.commit()
return {"ok": True}
@router.get("/locate")
async def locate(release_id: int | None = Query(None), sku: str | None = Query(None),
ident=Depends(require_token), db=Depends(get_db)):
"""Where IS this record (exact crate+slot) and, failing that, where SHOULD it be (matching collection)."""
if sku:
inv = (await db.execute(text("SELECT * FROM inventory WHERE sku=:s"), {"s": sku})).mappings().first()
elif release_id:
inv = (await db.execute(text(
"SELECT * FROM inventory WHERE release_id=:r AND in_stock ORDER BY updated_at DESC LIMIT 1"
), {"r": release_id})).mappings().first()
else:
raise HTTPException(400, "release_id or sku required")
if not inv:
raise HTTPException(404, "not in inventory")
exact = None
if inv.get("crate_id"):
cr = (await db.execute(text("""
SELECT c.id, c.label_text, c.name, c.rack_id, c.rack_level_index::int AS level, r.name AS rack
FROM virtual_crate c LEFT JOIN virtual_rack r ON r.id=c.rack_id WHERE c.id=:i
"""), {"i": inv["crate_id"]})).mappings().first()
if cr:
exact = {"crate_id": cr["id"], "crate": cr["label_text"] or cr["name"], "rack": cr["rack"],
"level": cr["level"], "slot": inv.get("slot_number")}
# fallback: match the record against active collections by priority. genre/style needs the disc_cache
# enrichment (added but backfilled separately) — until then a style-ruled collection won't match.
meta = (await db.execute(text(
"SELECT year, genre_ids, style_ids FROM disc_release WHERE id=:r"
), {"r": inv.get("release_id")})).mappings().first() if inv.get("release_id") else None
price = float(inv["price"]) if inv.get("price") is not None else None
year = meta["year"] if meta else None
rg = set(meta["genre_ids"] or []) if meta else set() # the record's genre ids (WowPlatter id space)
rs = set(meta["style_ids"] or []) if meta else set()
suggestion = None
cols = (await db.execute(text("""
SELECT id, name, color, crate_ids, genre_ids, style_ids, price_min::float AS pmin,
price_max::float AS pmax, year_min, year_max
FROM virtual_collection WHERE is_active::int <> 0 ORDER BY priority DESC, name
"""))).mappings()
for c in cols:
gmin, gmax = c["pmin"], c["pmax"]
if gmin is not None and (price is None or price < gmin):
continue
if gmax is not None and (price is None or price > gmax):
continue
if c["year_min"] is not None and (year is None or year < c["year_min"]):
continue
if c["year_max"] is not None and (year is None or year > c["year_max"]):
continue
cgen, csty = set(_ids(c["genre_ids"])), set(_ids(c["style_ids"]))
if cgen and not (rg & cgen):
continue
if csty and not (rs & csty):
continue
if not (gmin or gmax or c["year_min"] or c["year_max"] or cgen or csty):
continue # a ruleless collection matches nothing
ids = _arr(c["crate_ids"])
names = [dict(r) for r in (await db.execute(text(
"SELECT id, label_text, name FROM virtual_crate WHERE id = ANY(:ids)"), {"ids": ids})).mappings()]
suggestion = {"id": c["id"], "name": c["name"], "color": c["color"],
"crates": [{"id": n["id"], "label": n["label_text"] or n["name"]} for n in names]}
break
return {"ok": True, "sku": inv["sku"], "release_id": inv.get("release_id"),
"exact": exact, "suggested": suggestion}