Added script to fetch amiibo cards for AC.
And fixed some errors in the way
This commit is contained in:
parent
5af7d353f6
commit
8cef585489
|
@ -77,7 +77,7 @@ class AmiiboCardAdmin(ImportExportModelAdmin, reversion.VersionAdmin):
|
|||
'model_number')
|
||||
|
||||
def image_image(self, obj):
|
||||
if obj.statue:
|
||||
if obj.image:
|
||||
return '<img src="{}" width="80" />'.format(obj.image.url)
|
||||
else:
|
||||
return ''
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
# coding: utf-8
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import requests
|
||||
|
||||
from django.core.files.base import ContentFile
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.utils.text import slugify
|
||||
|
||||
from amiibofindr.apps.amiibo.models import AmiiboCard, Collection
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
collection_slug = 'animal-crossing'
|
||||
url = 'https://api.import.io/store/data/d7057867-dd48-4f5a-80ba-7051daf72bf4/_query?input/webpage/url=http%3A%2F%2Fanimalcrossingworld.com%2Fanimal-crossing-happy-home-designer-amiibo-cards%2F&_user=4519bdcf-aa0b-4b78-b014-e51476ba977c&_apikey=4519bdcfaa0b4b78b014e51476ba977c42d9d75fd621c0ccd433708c1f742487e763cb6a68202c4a5c7aa32c3ef0c99bc18dbc96548bc0c4fd1470abd0418732c82cf53dba34076decc64b68c504026e'
|
||||
dice = {
|
||||
'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6,
|
||||
}
|
||||
rps = {
|
||||
'rock': 1, 'paper': 2, 'scissors': 3,
|
||||
}
|
||||
|
||||
def handle_image(self, image_url):
|
||||
image = requests.get(image_url)
|
||||
f = ContentFile(image.content, image_url.split('/')[-1])
|
||||
return f
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
collection = Collection.objects.get(slug=self.collection_slug)
|
||||
data = requests.get(self.url).json()
|
||||
|
||||
for item in data['results']:
|
||||
amiibo, created = AmiiboCard.objects.get_or_create(
|
||||
collection_id=collection.pk,
|
||||
collection_number=item['number_number']
|
||||
)
|
||||
print(' => {} {}'.format(collection.slug, amiibo.collection_number))
|
||||
amiibo.type = AmiiboCard.CARD
|
||||
if created:
|
||||
amiibo.name_en = item['name_value']
|
||||
amiibo.name_eu = amiibo.name_en
|
||||
amiibo.name_us = amiibo.name_en
|
||||
|
||||
amiibo.collection_number = item['number_number']
|
||||
amiibo.collection = collection
|
||||
|
||||
amiibo.dice = self.dice[item['dice_value'].lower()]
|
||||
amiibo.rps = self.rps[item['rps_value'].lower()]
|
||||
|
||||
amiibo.image = self.handle_image(item['cardphoto_link'])
|
||||
|
||||
amiibo.slug = slugify(amiibo.name_en)
|
||||
|
||||
amiibo.save()
|
|
@ -28,8 +28,8 @@ def image_box_upload(self, filename):
|
|||
|
||||
def image_card_upload(self, filename):
|
||||
name, extension = os.path.splitext(filename)
|
||||
return 'amiibos/{}/card-{}-{}{}'.format(
|
||||
self.collection.slug, self.number, self.slug, extension)
|
||||
return 'amiibos/c/{}/{}_{}{}'.format(
|
||||
self.collection.pk, self.collection_number, self.pk, extension)
|
||||
|
||||
|
||||
#
|
||||
|
@ -113,10 +113,6 @@ class Amiibo(models.Model):
|
|||
|
||||
return result
|
||||
|
||||
@models.permalink
|
||||
def get_absolute_url(self):
|
||||
return ('amiibo:figure-detail', [self.collection.slug, self.slug])
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.name_eu) or u''
|
||||
|
||||
|
@ -131,7 +127,7 @@ class AmiiboFigure(Amiibo):
|
|||
|
||||
@models.permalink
|
||||
def get_absolute_url(self):
|
||||
return ('amiibo:card-detail', [self.slug])
|
||||
return ('amiibo:figure-detail', [self.collection.slug, self.slug])
|
||||
|
||||
@property
|
||||
def image_box(self):
|
||||
|
@ -164,8 +160,12 @@ class AmiiboCard(Amiibo):
|
|||
class Meta:
|
||||
ordering = ('collection', 'collection_number', 'name_eu', )
|
||||
|
||||
@models.permalink
|
||||
def get_absolute_url(self):
|
||||
return ('amiibo:card-detail', [self.collection.slug, self.slug])
|
||||
|
||||
def __unicode__(self):
|
||||
return u"{} {}".format(self.number, self.name)
|
||||
return u"{} {}".format(self.collection_number, self.slug)
|
||||
|
||||
|
||||
class AmiiboShop(models.Model):
|
||||
|
|
|
@ -5,8 +5,10 @@ from django.conf.urls import url, patterns
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
# home
|
||||
from .views import AmiiboView, CollectionView
|
||||
|
||||
from .views import (
|
||||
AmiiboView, AmiiboCardView, AmiiboFigureView,
|
||||
CollectionView
|
||||
)
|
||||
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
|
@ -24,9 +26,9 @@ urlpatterns = patterns(
|
|||
AmiiboView.as_view(),
|
||||
name='amiibo'),
|
||||
url(_(r'^cards/(?P<collection>[\w\d\-]+)/(?P<amiibo>[\w\d\-]+)$'),
|
||||
AmiiboView.as_view(),
|
||||
AmiiboCardView.as_view(),
|
||||
name='card-detail'),
|
||||
url(_(r'^figures/(?P<collection>[\w\d\-]+)/(?P<amiibo>[\w\d\-]+)$'),
|
||||
AmiiboView.as_view(),
|
||||
AmiiboFigureView.as_view(),
|
||||
name='figure-detail'),
|
||||
)
|
||||
|
|
|
@ -16,6 +16,7 @@ class HomeModel:
|
|||
|
||||
class CollectionView(View):
|
||||
template = 'amiibo/collection.html'
|
||||
type = Amiibo.FIGURE
|
||||
|
||||
def get(self, request, collection='all'):
|
||||
if collection != _('all'):
|
||||
|
@ -32,16 +33,33 @@ class CollectionView(View):
|
|||
})
|
||||
|
||||
|
||||
class FigureCollectionView(CollectionView):
|
||||
type = Amiibo.FIGURE
|
||||
|
||||
|
||||
class CardCollectionView(CollectionView):
|
||||
type = Amiibo.CARD
|
||||
|
||||
|
||||
class AmiiboView(View):
|
||||
template = 'amiibo/amiibo.html'
|
||||
type = Amiibo.FIGURE
|
||||
|
||||
def get(self, request, collection=None, amiibo=None):
|
||||
amiibo_obj = get_object_or_404(Amiibo,
|
||||
slug=amiibo,
|
||||
collection__slug=collection)
|
||||
collection__slug=collection,
|
||||
type=self.type)
|
||||
|
||||
return render(request, self.template, {
|
||||
'selected_collection': amiibo_obj.collection,
|
||||
'amiibo': amiibo_obj,
|
||||
'item': amiibo_obj
|
||||
})
|
||||
|
||||
class AmiiboFigureView(AmiiboView):
|
||||
type = Amiibo.FIGURE
|
||||
|
||||
|
||||
class AmiiboCardView(AmiiboView):
|
||||
type = Amiibo.CARD
|
||||
|
|
Loading…
Reference in New Issue