Filter and tokens update

This commit is contained in:
2026-06-13 12:28:58 -05:00
parent 1c4659c1ae
commit 593a6756f6
6 changed files with 338 additions and 154 deletions
+56 -36
View File
@@ -6,6 +6,7 @@ from fastapi import APIRouter, HTTPException, Request
import db
import worker
from config import OUTPUTS
from worker import is_scry_url
router = APIRouter()
@@ -20,6 +21,13 @@ def valid_slug(slug: str) -> str:
return slug
def line_to_entry(line: str) -> dict:
line = line.strip()
if is_scry_url(line):
return {"name": "", "scry_url": line}
return {"name": line, "scry_url": None}
@router.get("/decks")
async def list_decks():
return [dict(d) for d in db.get_decks()]
@@ -40,20 +48,28 @@ async def create_deck(request: Request):
name = body.get("deck_name", "").strip()
card_text = body.get("cards", "").strip()
commander = body.get("commander", "").strip()
if not name or not card_text:
raise HTTPException(400, "deck_name and cards required")
is_tokens = bool(body.get("is_tokens"))
if not name:
raise HTTPException(400, "deck_name required")
entries = [line_to_entry(l) for l in card_text.splitlines() if l.strip()]
if not is_tokens and not entries:
raise HTTPException(400, "cards required")
if is_tokens:
commander = ""
else:
if not commander:
commander = next((e["name"] for e in entries if e["name"]), "")
if commander:
idx = next((i for i, e in enumerate(entries) if e["name"] == commander), None)
if idx is not None:
entries.insert(0, entries.pop(idx))
else:
entries.insert(0, {"name": commander, "scry_url": None})
slug = slugify(name)
card_names = [l.strip() for l in card_text.splitlines() if l.strip()]
if not card_names:
raise HTTPException(400, "no cards")
if not commander:
commander = card_names[0]
# Commander always at position 0; remove any existing occurrence first
card_names = [n for n in card_names if n != commander]
card_names.insert(0, commander)
existing = db.get_deck(slug)
if existing and existing["status"] in ("queued", "running"):
raise HTTPException(409, "deck already processing")
@@ -61,14 +77,14 @@ async def create_deck(request: Request):
with db.conn() as c:
c.execute(
"INSERT OR REPLACE INTO decks (slug, deck_name, status, commander, price_usd, done, total) VALUES (?,?,?,?,0,0,?)",
(slug, name, "queued", commander, len(card_names))
(slug, name, "queued", commander or None, len(entries))
)
c.execute("DELETE FROM cards WHERE deck_slug=?", (slug,))
c.execute("DELETE FROM logs WHERE deck_slug=?", (slug,))
for i, card_name in enumerate(card_names):
for i, e in enumerate(entries):
c.execute(
"INSERT INTO cards (deck_slug, name, position, fetch_status) VALUES (?,?,?,'pending')",
(slug, card_name, i + 1)
"INSERT INTO cards (deck_slug, name, position, scry_url, fetch_status) VALUES (?,?,?,?,'pending')",
(slug, e["name"], i + 1, e["scry_url"])
)
await worker.queue.put(slug)
@@ -86,41 +102,45 @@ async def edit_deck(slug: str, request: Request):
body = await request.json()
remove_ids: list[int] = body.get("remove", [])
add_names: list[str] = [n.strip() for n in body.get("add", []) if n.strip()]
add_entries = [line_to_entry(n) for n in body.get("add", []) if n.strip()]
new_name: str = body.get("deck_name", deck["deck_name"]).strip() or deck["deck_name"]
new_commander: str = body.get("commander", deck["commander"]).strip() or deck["commander"]
raw_cmd = body.get("commander", None)
if raw_cmd is not None:
new_commander = raw_cmd.strip() or (deck["commander"] or None)
else:
new_commander = deck["commander"]
with db.conn() as c:
for cid in remove_ids:
row = c.execute(
"SELECT filename FROM cards WHERE id=? AND deck_slug=?", (cid, slug)
"SELECT filename, back_filename FROM cards WHERE id=? AND deck_slug=?", (cid, slug)
).fetchone()
if row and row["filename"]:
p = OUTPUTS / slug / row["filename"]
if p.exists():
p.unlink()
if row:
for fn in (row["filename"], row["back_filename"]):
if fn and (OUTPUTS / slug / fn).exists():
(OUTPUTS / slug / fn).unlink()
c.execute("DELETE FROM cards WHERE id=? AND deck_slug=?", (cid, slug))
max_pos = c.execute(
"SELECT COALESCE(MAX(position), 0) FROM cards WHERE deck_slug=?", (slug,)
).fetchone()[0]
for i, card_name in enumerate(add_names):
for i, e in enumerate(add_entries):
c.execute(
"INSERT INTO cards (deck_slug, name, position, fetch_status) VALUES (?,?,?,'pending')",
(slug, card_name, max_pos + i + 1)
"INSERT INTO cards (deck_slug, name, position, scry_url, fetch_status) VALUES (?,?,?,?,'pending')",
(slug, e["name"], max_pos + i + 1, e["scry_url"])
)
# Ensure commander has a card row; if not, insert at position 0 so it sorts first
existing_commander = c.execute(
"SELECT id FROM cards WHERE deck_slug=? AND name=? LIMIT 1",
(slug, new_commander)
).fetchone()
if not existing_commander:
c.execute(
"INSERT INTO cards (deck_slug, name, position, fetch_status) VALUES (?,?,0,'pending')",
(slug, new_commander)
)
if new_commander:
row = c.execute(
"SELECT id FROM cards WHERE deck_slug=? AND name=? LIMIT 1", (slug, new_commander)
).fetchone()
if not row:
c.execute(
"INSERT INTO cards (deck_slug, name, position, fetch_status) VALUES (?,?,0,'pending')",
(slug, new_commander)
)
c.execute(
"UPDATE decks SET deck_name=?, commander=?, status='queued' WHERE slug=?",