Add collection information

This commit is contained in:
Correl Roush 2022-08-03 00:30:58 -04:00
parent a90496bd0b
commit e781bec46a
6 changed files with 67 additions and 44 deletions

View file

@ -58,6 +58,7 @@ async def load(
card=found[0], card=found[0],
foil=is_foil, foil=is_foil,
language=row["Language"] or "English", language=row["Language"] or "English",
collection=row.get("List name") or "Default",
) )
logging.info((quantity, card)) logging.info((quantity, card))
for i in range(quantity): for i in range(quantity):

View file

@ -183,14 +183,14 @@ async def advanced_search(
] ]
if sort_by == "price": if sort_by == "price":
orderings = [ orderings = [
"CAST(COALESCE(CASE WHEN \"copies\".\"isFoil\" THEN card_prices.usd_foil ELSE card_prices.usd END, 0) as decimal) DESC", 'CAST(COALESCE(CASE WHEN "copies"."isFoil" THEN card_prices.usd_foil ELSE card_prices.usd END, 0) as decimal) DESC',
*orderings, *orderings,
] ]
params["last_update_key"] = "last_update" params["last_update_key"] = "last_update"
query = " ".join( query = " ".join(
[ [
"SELECT cards.*, card_prices.*", "SELECT cards.*, card_prices.*, copies.*",
", CASE WHEN \"copies\".\"isFoil\" THEN card_prices.usd_foil", ', CASE WHEN "copies"."isFoil" THEN card_prices.usd_foil',
" ELSE card_prices.usd END AS usd", " ELSE card_prices.usd END AS usd",
"FROM cards", "FROM cards",
" ".join(joins), " ".join(joins),
@ -212,7 +212,8 @@ async def advanced_search(
return None return None
return [ return [
tutor.models.Card( tutor.models.CardCopy(
card=tutor.models.Card(
scryfall_id=row["scryfall_id"], scryfall_id=row["scryfall_id"],
oracle_id=row["oracle_id"], oracle_id=row["oracle_id"],
name=row["name"], name=row["name"],
@ -232,6 +233,9 @@ async def advanced_search(
price_eur=convert_price(row["eur"]), price_eur=convert_price(row["eur"]),
price_eur_foil=convert_price(row["eur_foil"]), price_eur_foil=convert_price(row["eur_foil"]),
price_tix=convert_price(row["tix"]), price_tix=convert_price(row["tix"]),
),
foil=row["isFoil"] if row["isFoil"] is not None else False,
collection=row["collection"] or "Default",
) )
for row in rows for row in rows
] ]
@ -300,12 +304,13 @@ async def store_set(db: psycopg.Cursor, set_code: str, name: str) -> None:
async def store_copy(db: psycopg.Cursor, copy: tutor.models.CardCopy) -> None: async def store_copy(db: psycopg.Cursor, copy: tutor.models.CardCopy) -> None:
await db.execute( await db.execute(
""" """
INSERT INTO copies ("scryfall_id", "isFoil", "condition") INSERT INTO copies ("scryfall_id", "isFoil", "collection", "condition")
VALUES (%(scryfall_id)s, %(foil)s, %(condition)s) VALUES (%(scryfall_id)s, %(foil)s, %(collection)s, %(condition)s)
""", """,
{ {
"scryfall_id": str(copy.card.scryfall_id), "scryfall_id": str(copy.card.scryfall_id),
"foil": copy.foil, "foil": copy.foil,
"collection": copy.collection,
"condition": copy.condition, "condition": copy.condition,
}, },
) )

View file

@ -90,4 +90,5 @@ class CardCopy:
card: Card card: Card
foil: bool foil: bool
language: str = "English" language: str = "English"
collection: str = "Default"
condition: typing.Optional[str] = None condition: typing.Optional[str] = None

View file

@ -64,7 +64,7 @@ class SearchHandler(tornado.web.RequestHandler):
limit = int(self.get_argument("limit", 10)) limit = int(self.get_argument("limit", 10))
sort_by = self.get_argument("sort_by", "rarity") sort_by = self.get_argument("sort_by", "rarity")
search = tutor.search.search.parse(query) search = tutor.search.search.parse(query)
cards = await tutor.database.advanced_search( copies = await tutor.database.advanced_search(
cursor, cursor,
search, search,
limit=limit + 1, limit=limit + 1,
@ -74,7 +74,7 @@ class SearchHandler(tornado.web.RequestHandler):
if in_collection if in_collection
else None, else None,
) )
has_more = cards and len(cards) > limit has_more = copies and len(copies) > limit
self.set_header("Content-Type", "application/json") self.set_header("Content-Type", "application/json")
self.set_header("Access-Control-Allow-Origin", "*") self.set_header("Access-Control-Allow-Origin", "*")
links = {} links = {}
@ -92,24 +92,26 @@ class SearchHandler(tornado.web.RequestHandler):
json.dumps( json.dumps(
[ [
{ {
"scryfall_id": str(card.scryfall_id), "scryfall_id": str(copy.card.scryfall_id),
"name": card.name, "name": copy.card.name,
"set_code": card.set_code, "set_code": copy.card.set_code,
"collector_number": card.collector_number, "collector_number": copy.card.collector_number,
"rarity": str(card.rarity), "rarity": str(copy.card.rarity),
"color_identity": tutor.models.Color.to_string( "color_identity": tutor.models.Color.to_string(
card.color_identity copy.card.color_identity
), ),
"oracle_text": card.oracle_text, "oracle_text": copy.card.oracle_text,
"prices": { "prices": {
"usd": price(card.price_usd), "usd": price(copy.card.price_usd),
"usd_foil": price(card.price_usd_foil), "usd_foil": price(copy.card.price_usd_foil),
"eur": price(card.price_eur), "eur": price(copy.card.price_eur),
"eur_foil": price(card.price_eur_foil), "eur_foil": price(copy.card.price_eur_foil),
"tix": price(card.price_tix), "tix": price(copy.card.price_tix),
},
"foil": copy.foil,
"collection": copy.collection,
} }
} for copy in copies[:limit]
for card in cards[:limit]
] ]
) )
) )

View file

@ -326,6 +326,9 @@ colors =
white = white =
E.rgb255 255 255 255 E.rgb255 255 255 255
offwhite =
E.rgb255 200 200 200
mythic = mythic =
E.rgb255 205 55 0 E.rgb255 205 55 0
@ -345,7 +348,9 @@ colors =
, sidebar = lighterGrey , sidebar = lighterGrey
, selected = darkGrey , selected = darkGrey
, hover = grey , hover = grey
, text = white , title = white
, subtitle = offwhite
, text = offwhite
, mythic = mythic , mythic = mythic
, rare = rare , rare = rare
, uncommon = uncommon , uncommon = uncommon
@ -458,6 +463,7 @@ viewCardBrowser model =
, E.padding 2 , E.padding 2
, Font.family [ Font.typeface "sans" ] , Font.family [ Font.typeface "sans" ]
, Font.size 10 , Font.size 10
, Font.color colors.title
] ]
<| <|
E.text text E.text text
@ -514,7 +520,7 @@ viewCardBrowser model =
E.el [ E.centerX ] E.el [ E.centerX ]
(viewCard { width = 192, height = 272 } card) (viewCard { width = 192, height = 272 } card)
:: (E.row [ E.spacing 5, E.centerX ] <| List.map priceBadge (prices card)) :: (E.row [ E.spacing 5, E.centerX ] <| List.map priceBadge (prices card))
:: E.paragraph [ Font.heavy, Font.size 24, Font.center ] [ E.text card.name ] :: E.paragraph [ Font.heavy, Font.size 24, Font.center, Font.color colors.title ] [ E.text card.name ]
:: List.map (\text -> E.paragraph [ Font.size 16 ] [ E.text text ]) :: List.map (\text -> E.paragraph [ Font.size 16 ] [ E.text text ])
(String.lines card.oracleText) (String.lines card.oracleText)
@ -551,7 +557,10 @@ viewCardBrowser model =
] ]
, description = cardModel.name , description = cardModel.name
} }
, E.el [ E.centerY, E.height E.fill, E.width E.fill, E.clipX ] <| E.text cardModel.name , E.column [ E.centerY, E.height E.fill, E.width E.fill, E.clipX ]
[ E.el [ Font.color colors.title ] <| E.text cardModel.name
, E.el [ Font.size 16, Font.italic, Font.color colors.subtitle ] <| E.text cardModel.collection
]
, E.column [ E.alignRight, E.height E.fill ] <| , E.column [ E.alignRight, E.height E.fill ] <|
setBadge cardModel setBadge cardModel
:: List.map priceBadge (prices cardModel) :: List.map priceBadge (prices cardModel)

View file

@ -20,8 +20,11 @@ type alias Card =
, rarity : String , rarity : String
, oracleText : String , oracleText : String
, prices : Prices , prices : Prices
, collection : String
, foil : Bool
} }
decode : Json.Decode.Decoder Card decode : Json.Decode.Decoder Card
decode = decode =
Json.Decode.succeed Card Json.Decode.succeed Card
@ -34,6 +37,8 @@ decode =
|> Json.Decode.map (Maybe.withDefault "") |> Json.Decode.map (Maybe.withDefault "")
) )
|> JDP.required "prices" decodePrices |> JDP.required "prices" decodePrices
|> JDP.required "collection" Json.Decode.string
|> JDP.required "foil" Json.Decode.bool
decodePrices : Json.Decode.Decoder Prices decodePrices : Json.Decode.Decoder Prices