Example #1
0
    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 handle(self, *args, **options):
        try:
            file = io.open('cardlist.txt', 'rt')
            errors = io.open('failed.txt', 'w')

            booster = None
            count = 0
            fail_count = 0

            for line in file:
                if line.strip() == '':
                    continue
                if line.startswith('###'):
                    booster_name = line.strip('# \n')
                    print booster_name
                    booster = Booster.objects.get(name=booster_name)
                    continue

                count += 1

                try:
                    response = requests.get('http://yugiohprices.com/api/card_data/' + line.strip())
                    json = response.json()
                except Exception, e:
                    bugsnag.notify(e, context='CardImport', meta_data={ 'card': line, 'booster_name': booster_name, 'response': response.text })
                    json = None

                card = Card(name=line.strip())

                if json and json['status'] == 'success':
                    card.description = json['data']['text']

                    if json['data']['card_type'] == 'monster':
                        card.attack = json['data']['atk']
                        card.defense = json['data']['def']
                        card.attribute = json['data']['family'].lower()
                        card.level = json['data']['level']
                        card.save()

                        types = []
                        for t in json['data']['type'].split('/'):
                            ct = CardType.objects.get(name=t.strip())
                            types.append(ct.id)
                        card.card_types = types
                    else:
                        card.effect_type = ('{} {}'.format(json['data']['property'], json['data']['card_type'])).lower()
                else:
                    errors.write(line)
                    errors.flush()
                    fail_count += 1

                card.save()
                card.boosters = [ booster.id ]
                card.save()

                print '{} - {} ({})'.format(booster.name, line.strip(), str(count))
        except Exception, e:
            bugsnag.notify(e, context='CardImport', meta_data={ 'card': line, 'booster_name': booster_name, 'response': response.text })