From 0731bf8429bbe72a2f55d3c83d589d72f3d1c291 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Tue, 10 Jan 2023 23:27:19 -0500 Subject: [PATCH] Fix card name matching in deck import --- tutor/cli.py | 12 ++++-------- tutor/database.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tutor/cli.py b/tutor/cli.py index 9ee417c..939f15b 100644 --- a/tutor/cli.py +++ b/tutor/cli.py @@ -125,18 +125,14 @@ def import_deck(ctx, filename, name): for line in bar: if match := line_pattern.match(line.strip()): groups = match.groupdict() - copies = await tutor.database.advanced_search( - cursor, - tutor.search.Search( - [tutor.search.Name(text=groups["name"])] - ), - limit=1, + oracle_id = await tutor.database.oracle_id_by_name( + cursor, groups["name"] ) - if copies: + if oracle_id: await tutor.database.store_deck_card( cursor, deck_id, - copies[0].card.oracle_id, + oracle_id, int(groups["quantity"]), ) await conn.commit() diff --git a/tutor/database.py b/tutor/database.py index ad9d3d3..48a8818 100644 --- a/tutor/database.py +++ b/tutor/database.py @@ -243,6 +243,18 @@ async def advanced_search( ] +async def oracle_id_by_name( + db: psycopg.Cursor, name: str +) -> typing.Optional[uuid.UUID]: + db.row_factory = psycopg.rows.dict_row + await db.execute( + 'SELECT "oracle_id" FROM "oracle" WHERE "name" ILIKE %(name)s', {"name": name} + ) + row = await db.fetchone() + if row: + return row["oracle_id"] + + async def store_card(db: psycopg.Cursor, card: tutor.models.Card) -> None: await db.execute( """