60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
"""Transient helpers for ad-hoc MTG Scryfall lookups."""
|
|
|
|
from odoo import _, fields, models
|
|
from odoo.exceptions import UserError
|
|
|
|
|
|
class MvdTcgMtgLookup(models.TransientModel):
|
|
"""Import one MTG card reference from Scryfall."""
|
|
|
|
_name = "mvd.tcg.mtg.lookup"
|
|
_description = "MTG Card Lookup"
|
|
|
|
query = fields.Char(required=True)
|
|
lookup_mode = fields.Selection(
|
|
selection=[
|
|
("url", "Direct Scryfall URL or card id"),
|
|
("exact", "Exact card name"),
|
|
("fuzzy", "Fuzzy card name"),
|
|
],
|
|
default="fuzzy",
|
|
required=True,
|
|
)
|
|
|
|
def action_lookup_card(self):
|
|
"""Look up one MTG card on Scryfall and open the upserted record.
|
|
|
|
Returns:
|
|
dict: Form action for the upserted MTG card.
|
|
|
|
Raises:
|
|
UserError: If the lookup query is empty or returns no card.
|
|
"""
|
|
self.ensure_one()
|
|
self.env["mvd.tcg.mtg.scryfall.api"]._mtg_scryfall_check_manager_access()
|
|
|
|
query = (self.query or "").strip()
|
|
if not query:
|
|
raise UserError(_("Enter a card name or Scryfall card URL first."))
|
|
|
|
scryfall_api = self.env["mvd.tcg.mtg.scryfall.api"]
|
|
payloads = scryfall_api.resolve_lookup_payloads(query, self.lookup_mode)
|
|
mtg_card = self.env["mvd.tcg.card"].mtg_scryfall_upsert_group_from_payloads(
|
|
payloads
|
|
)
|
|
if not mtg_card:
|
|
raise UserError(_("No MTG card could be created from the Scryfall payload."))
|
|
|
|
action = self.env.ref("mvd_tcg_mtg.mvd_tcg_mtg_card_action").read()[0]
|
|
action.update(
|
|
{
|
|
"view_mode": "form",
|
|
"views": [
|
|
(self.env.ref("mvd_tcg_mtg.mvd_tcg_mtg_card_view_form").id, "form")
|
|
],
|
|
"res_id": mtg_card.id,
|
|
}
|
|
)
|
|
action.pop("domain", None)
|
|
return action
|