99 lines
3.4 KiB
Python
99 lines
3.4 KiB
Python
"""Scryfall-specific MTG set helpers for the MTG connector."""
|
|
|
|
from odoo import api, fields, models
|
|
|
|
|
|
class MvdTcgMtgSet(models.Model):
|
|
"""Extend MTG sets with Scryfall import helpers."""
|
|
|
|
_inherit = "mvd.tcg.mtg.set"
|
|
|
|
mtg_scryfall_last_import_run_id = fields.Many2one(
|
|
"mvd.tcg.mtg.scryfall.import.run",
|
|
copy=False,
|
|
index=True,
|
|
ondelete="set null",
|
|
readonly=True,
|
|
)
|
|
mtg_scryfall_last_synced_at = fields.Datetime(
|
|
copy=False,
|
|
readonly=True,
|
|
)
|
|
|
|
def action_open_mtg_scryfall_last_import_run(self):
|
|
"""Open the most recent Scryfall import run linked to this set.
|
|
|
|
Returns:
|
|
dict | bool: Window action or ``False`` when no run is linked.
|
|
"""
|
|
self.ensure_one()
|
|
if not self.mtg_scryfall_last_import_run_id:
|
|
return False
|
|
|
|
return {
|
|
"type": "ir.actions.act_window",
|
|
"name": self.mtg_scryfall_last_import_run_id.display_name,
|
|
"res_model": "mvd.tcg.mtg.scryfall.import.run",
|
|
"view_mode": "form",
|
|
"res_id": self.mtg_scryfall_last_import_run_id.id,
|
|
"target": "current",
|
|
}
|
|
|
|
def action_mtg_scryfall_refresh_set(self):
|
|
"""Refresh the current MTG set from the Scryfall connector.
|
|
|
|
Returns:
|
|
dict: Window action for the created refresh run.
|
|
"""
|
|
self.ensure_one()
|
|
self.env["mvd.tcg.mtg.scryfall.api"]._mtg_scryfall_check_manager_access()
|
|
refresh_run = self.env["mvd.tcg.mtg.scryfall.import.run"].create_set_refresh_run(
|
|
self,
|
|
language_codes=self.env["mvd.tcg.mtg.scryfall.api"].get_import_language_codes(),
|
|
max_cards_per_set=self.env["mvd.tcg.mtg.scryfall.api"].get_default_max_cards_per_set(),
|
|
include_tokens=self.env["mvd.tcg.mtg.scryfall.api"].get_default_include_tokens(),
|
|
)
|
|
return refresh_run.action_execute_import()
|
|
|
|
@api.model
|
|
def mtg_scryfall_upsert_from_payload(self, payload, import_run=None):
|
|
"""Create or update one MTG set from a Scryfall card payload.
|
|
|
|
Args:
|
|
payload: Raw Scryfall card payload containing set metadata.
|
|
import_run: Optional import run record for traceability.
|
|
|
|
Returns:
|
|
mvd.tcg.mtg.set: Upserted MTG set record.
|
|
"""
|
|
set_code = (payload.get("set") or "").strip().lower()
|
|
if not set_code:
|
|
return self.browse()
|
|
|
|
mtg_game = self.env["mvd.tcg.game"]._mvd_tcg_get_mtg_game()
|
|
values = {
|
|
"active": True,
|
|
"code": set_code,
|
|
"game_id": mtg_game.id,
|
|
"name": payload.get("set_name") or set_code.upper(),
|
|
"released_on": payload.get("released_at") or False,
|
|
"set_type": payload.get("set_type") or False,
|
|
"official_card_count": payload.get("set_card_count") or 0,
|
|
"icon_svg_uri": payload.get("set_uri") or payload.get("scryfall_set_uri") or False,
|
|
}
|
|
if import_run:
|
|
values.update(
|
|
{
|
|
"mtg_scryfall_last_import_run_id": import_run.id,
|
|
"mtg_scryfall_last_synced_at": fields.Datetime.now(),
|
|
}
|
|
)
|
|
mtg_set = self.search(
|
|
[("game_id", "=", mtg_game.id), ("code", "=", set_code)],
|
|
limit=1,
|
|
)
|
|
if mtg_set:
|
|
mtg_set.write(values)
|
|
return mtg_set
|
|
return self.create(values)
|