Fix card name matching in deck import

This commit is contained in:
Correl Roush 2023-01-10 23:27:19 -05:00
parent 740f488460
commit 0731bf8429
2 changed files with 16 additions and 8 deletions

View file

@ -125,18 +125,14 @@ def import_deck(ctx, filename, name):
for line in bar: for line in bar:
if match := line_pattern.match(line.strip()): if match := line_pattern.match(line.strip()):
groups = match.groupdict() groups = match.groupdict()
copies = await tutor.database.advanced_search( oracle_id = await tutor.database.oracle_id_by_name(
cursor, cursor, groups["name"]
tutor.search.Search(
[tutor.search.Name(text=groups["name"])]
),
limit=1,
) )
if copies: if oracle_id:
await tutor.database.store_deck_card( await tutor.database.store_deck_card(
cursor, cursor,
deck_id, deck_id,
copies[0].card.oracle_id, oracle_id,
int(groups["quantity"]), int(groups["quantity"]),
) )
await conn.commit() await conn.commit()

View file

@ -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: async def store_card(db: psycopg.Cursor, card: tutor.models.Card) -> None:
await db.execute( await db.execute(
""" """