def post(self, request, *args, **kwargs): c = Card() c.name = request.data.get('name') c.spell_type = request.data.get('spell_type') c.expansion = request.data.get('expansion') c.is_legendary = request.data.get('is_legendary') is 'true' c.card_number = request.data.get('card_number') c.creature_type = request.data.get('creature_type') c.abilities = request.data.get('abilities') c.text = request.data.get('text') c.flavor_text = request.data.get('flavor_text') c.power = request.data.get('power') c.defense = request.data.get('defense') c.loyalty = request.data.get('loyalty') c.artist = request.data.get('artist') c.save() c.image = request.data.get('file') for k, v in request.data.items(): if k.startswith('mana_'): m = Mana.objects.get(name=k[5:]) mfc = ManaForCard() mfc.mana = m mfc.card = c mfc.quantity = v mfc.save() c.save() return Response(self.serializer_class(c).data)
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 add(request): try: context = { "decks": Deck.objects.all().order_by('name'), "deck_types": DeckType.objects.all().order_by('sort_order', 'name') } name = request.POST['name'].strip() if len(name) > 0: d = Deck(name=name, notes=request.POST['notes'], deckType=DeckType.objects.get( pk=int(request.POST['deckType']))) d.save() p = re.compile("(\\d+)x (.+)") issue_list = [] cards = None if len(request.POST['list'].strip().replace('\r\n', '\n')) != 0: main = request.POST['list'].strip().replace('\r\n', '\n').split('\n') else: main = [] if len(request.POST['sideboard'].strip().replace('\r\n', '\n')) != 0: total = main + request.POST['sideboard'].strip().replace( '\r\n', '\n').split('\n') else: total = main print(total) for i, line in enumerate(total): m = p.match(line.strip()) if m is None: issue_list.append( "Line {} is badly formed. Card could not be added.". format(line)) else: c = Card.objects.filter(name__iexact=m.groups()[1]) if len(c) == 0: #try json if cards is None: f = open(os.path.join(settings.BASE_DIR, 'AllCards.json'), 'r', encoding='utf8') cards = json.load(f) f.close() if m.groups()[1] in cards: c = Card(name=m.groups()[1], text=cards[m.groups()[1]]['text'], notes="", addMana=False) #mana cost if "manaCost" in cards[m.groups()[1]]: c.manaCost = cards[ m.groups()[1]]['manaCost'].replace( '}{', ',').replace('{', '').replace( '}', '').replace('/', '') #ptl if "power" in cards[m.groups()[1]]: c.power = cards[m.groups()[1]]["power"] if "toughness" in cards[m.groups()[1]]: c.toughness = cards[m.groups()[1]]["toughness"] if "loyalty" in cards[m.groups()[1]]: c.loyalty = cards[m.groups()[1]]["loyalty"] c.save() #types if "supertypes" in cards[m.groups()[1]]: for type in cards[m.groups()[1]]['supertypes']: t = Type.objects.filter(name__exact=type, typeType__exact=2) if len(t) == 0: t = Type(name=type, typeType=2) t.save() else: t = t[0] c.types.add(t) if "types" in cards[m.groups()[1]]: for type in cards[m.groups()[1]]['types']: t = Type.objects.filter(name__exact=type, typeType__exact=3) if len(t) == 0: t = Type(name=type, typeType=3) t.save() else: t = t[0] c.types.add(t) if "subtypes" in cards[m.groups()[1]]: for type in cards[m.groups()[1]]['subtypes']: t = Type.objects.filter(name__exact=type, typeType__exact=1) if len(t) == 0: t = Type(name=type, typeType=1) t.save() else: t = t[0] c.types.add(t) #sets f = open( os.path.join(settings.BASE_DIR, 'SetList.json'), 'r') sets = json.load(f) f.close() if "printings" in cards[m.groups()[1]]: for code in cards[m.groups()[1]]["printings"]: for jset in sets: if jset["code"] == code: if jset["type"] not in bad_set_types: set = Set.objects.filter( name__exact=jset["name"]) if len(set) == 0: set = Set( name=jset["name"]) set.save() else: set = set[0] set.cards.add(c) break if i < len(main): dc = DeckCard(card=c, count=int(m.groups()[0]), deck=d) else: dc = DeckCard(card=c, sideboard_count=int( m.groups()[0]), deck=d) dc.save() else: issue_list.append( "Could not find a card named {}. Card could not be added." .format(m.groups()[1])) else: if i < len(main): dc = DeckCard(card=c[0], count=int(m.groups()[0]), deck=d) else: dc = DeckCard(card=c[0], sideboard_count=int(m.groups()[0]), deck=d) dc.save() if len(issue_list) > 0: context['saved'] = '\\n'.join(issue_list) context['new_deck'] = d.id else: return redirect('/decks/{}'.format(d.id)) else: context['error'] = "Deck must have a name." if mobile(request): return render(request, 'decks/m_add.html', context) else: return render(request, 'decks/add.html', context) except KeyError: if mobile(request): return render(request, 'decks/m_add.html', context) else: return render(request, 'decks/add.html', context) except OSError as ex: print(ex)