Files
mvd_tcg_deck/models/mvd_tcg_card.py
2026-04-03 23:08:57 +02:00

68 lines
1.9 KiB
Python

"""Deck integration on top of game-neutral card references."""
from odoo import _, api, fields, models
class MvdTcgCard(models.Model):
"""Extend cards with deck usage helpers."""
_inherit = "mvd.tcg.card"
deck_line_ids = fields.One2many(
"mvd.tcg.deck.line",
"card_id",
string="Deck Lines",
readonly=True,
)
deck_count = fields.Integer(
compute="_compute_deck_count",
readonly=True,
)
@api.depends("deck_line_ids.deck_id")
def _compute_deck_count(self):
"""Compute how many distinct decks reference each card.
Returns:
None: The method updates records in place.
"""
for card in self:
card.deck_count = len(card.deck_line_ids.mapped("deck_id"))
def action_open_decks(self):
"""Open all decks that reference the current card.
Returns:
dict: Window action filtered to linked decks.
"""
self.ensure_one()
deck_ids = self.deck_line_ids.mapped("deck_id").ids
action = self.env["ir.actions.actions"]._for_xml_id(
"mvd_tcg_deck.mvd_tcg_deck_action"
)
action["domain"] = [("id", "in", deck_ids or [0])]
if self.game_id:
action["context"] = {"default_game_id": self.game_id.id}
return action
def action_open_add_to_deck_wizard(self):
"""Open the deck-assignment wizard for the current card.
Returns:
dict: Modal wizard action.
"""
self.ensure_one()
return {
"type": "ir.actions.act_window",
"name": _("Add to Deck"),
"res_model": "mvd.tcg.add.to.deck",
"view_mode": "form",
"target": "new",
"context": {
"default_card_id": self.id,
"default_game_id": self.game_id.id,
"active_model": self._name,
"active_id": self.id,
},
}