Fix card name matching in deck import
This commit is contained in:
parent
740f488460
commit
0731bf8429
2 changed files with 16 additions and 8 deletions
12
tutor/cli.py
12
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()
|
||||
|
|
|
@ -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(
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue