def create_test_card(fields: Optional[Dict[str, Any]] = None) -> Card: """ Creates a test card with fields from the given dict :param fields: The fields to populate :return: A card object """ card = Card() card.scryfall_oracle_id = uuid.uuid4() card.name = uuid.uuid1() card.cmc = 0 card.num_power = 0 card.num_toughness = 0 card.num_loyalty = 0 card.colour_flags = 0 card.colour_identity_flags = 0 card.colour_count = 0 card.colour_identity_count = 0 card.colour_sort_key = 0 card.colour_weight = 0 card.layout = "normal" card.is_reserved = False card.is_token = False card.converted_mana_cost = 0 for key, value in (fields or {}).items(): assert hasattr(card, key) setattr(card, key, value) card.full_clean() card.save() return card
def update_card(self, staged_card: StagedCard) -> Card: """ Updates or creates the Card object for the given StagedCard :param staged_card: The staging information for this card :return: The updated or created Card """ try: if staged_card.is_token: card = Card.objects.get(name=staged_card.get_name(), scryfall_oracle_id=staged_card.get_scryfall_oracle_id(), is_token=True, side=staged_card.get_side()) else: card = Card.objects.get(name=staged_card.get_name(), is_token=False) if not self.force_update and staged_card.get_name() in self.updated_cards: logger.info('%s has already been updated', card) self.increment_ignores('Card') return card logger.info('Updating existing card %s', card) self.increment_updated('Card') except Card.DoesNotExist: card = Card(name=staged_card.get_name()) logger.info('Creating new card %s', card) self.increment_created('Card') self.updated_cards.append(staged_card.get_name()) card.cost = staged_card.get_mana_cost() card.cmc = staged_card.get_cmc() card.colour_flags = staged_card.get_colour() card.colour_identity_flags = staged_card.get_colour_identity() card.colour_count = staged_card.get_colour_count() card.colour_sort_key = staged_card.get_colour_sort_key() card.colour_weight = staged_card.get_colour_weight() card.power = staged_card.get_power() card.toughness = staged_card.get_toughness() card.num_power = staged_card.get_num_power() card.num_toughness = staged_card.get_num_toughness() card.loyalty = staged_card.get_loyalty() card.num_loyalty = staged_card.get_num_loyalty() card.type = staged_card.get_types() card.subtype = staged_card.get_subtypes() card.original_type = staged_card.get_original_type() card.rules_text = staged_card.get_rules_text() card.original_text = staged_card.get_original_text() card.layout = staged_card.get_layout() card.side = staged_card.get_side() card.scryfall_oracle_id = staged_card.get_scryfall_oracle_id() card.is_reserved = staged_card.is_reserved() card.is_token = staged_card.is_token card.full_clean() card.save() return card
def create_test_card(fields: dict) -> Card: """ Creates a test card with fields from the given dict :param fields: The fields to populate :return: A card object """ card = Card() card.name = 'undefined' card.cmc = 0 card.num_power = 0 card.num_toughness = 0 card.num_loyalty = 0 card.colour_count = 0 card.colour_sort_key = 0 card.colour_weight = 0 card.is_reserved = False for key, value in fields.items(): card.__dict__[key] = value card.save() return card