"""Magic: The Gathering set models for the TCG suite.""" from odoo import api, fields, models class MvdTcgMtgSet(models.Model): """Represent a Magic: The Gathering set within the MTG adapter.""" _name = "mvd.tcg.mtg.set" _description = "MTG Set" _order = "released_on desc, code, id" name = fields.Char(required=True, translate=True, index="trigram") code = fields.Char(required=True, index=True) active = fields.Boolean(default=True) sequence = fields.Integer(default=10) game_id = fields.Many2one( "mvd.tcg.game", required=True, index=True, default=lambda self: self._default_game_id(), ondelete="restrict", ) released_on = fields.Date(index=True) set_type = fields.Char(index=True) official_card_count = fields.Integer() icon_svg_uri = fields.Char() note = fields.Text(translate=True) mtg_card_ids = fields.One2many( "mvd.tcg.card", "mtg_set_id", string="Cards", ) mtg_card_count = fields.Integer( string="Card Count", compute="_compute_mtg_card_count", ) _code_unique = models.Constraint( "UNIQUE (game_id, code)", "The MTG set code must be unique per game.", ) @api.model def _default_game_id(self): """Return the seeded MTG game record.""" return self.env["mvd.tcg.game"]._mvd_tcg_get_mtg_game().id @api.depends("mtg_card_ids") def _compute_mtg_card_count(self): """Compute how many MTG cards are currently linked to each set.""" card_data = self.env["mvd.tcg.card"]._read_group( [("mtg_set_id", "in", self.ids)], ["mtg_set_id"], ["__count"], ) counts_by_set = {record.id: count for record, count in card_data} for mtg_set in self: mtg_set.mtg_card_count = counts_by_set.get(mtg_set.id, 0) def action_open_cards(self): """Open the MTG cards catalog filtered on the selected set.""" self.ensure_one() action = self.env["ir.actions.actions"]._for_xml_id( "mvd_tcg_mtg.mvd_tcg_mtg_card_action" ) action["domain"] = [("mtg_set_id", "=", self.id)] action["context"] = { "default_game_id": self.game_id.id, "default_mtg_set_id": self.id, } return action