Several changes to models and admin
This commit is contained in:
parent
bd9bbf457a
commit
a244068b1a
|
@ -11,7 +11,7 @@ from .models import Collection, Amiibo
|
|||
|
||||
|
||||
class CollectionAdmin(reversion.VersionAdmin):
|
||||
list_display = ('name', 'amiibo_number', )
|
||||
list_display = ('name_eu', 'amiibo_number', )
|
||||
|
||||
def amiibo_number(self, obj):
|
||||
return obj.amiibos.count()
|
||||
|
@ -19,13 +19,18 @@ class CollectionAdmin(reversion.VersionAdmin):
|
|||
|
||||
|
||||
class AmiiboAdmin(reversion.VersionAdmin):
|
||||
list_display_links = ('name', )
|
||||
list_display = ('statue_image', 'name', 'collection',)
|
||||
list_display_links = ('name_eu', )
|
||||
list_display = ('statue_image', 'box_image', 'name_eu', 'collection',)
|
||||
search_fields = ('collection__name_eu', 'name_eu', 'name_us', )
|
||||
|
||||
def statue_image(self, obj):
|
||||
return '<img src="{}" width="80" />'.format(obj.statue.url)
|
||||
return '<img src="{}" width="80" />'.format(obj.image_statue)
|
||||
statue_image.allow_tags = True
|
||||
|
||||
def box_image(self, obj):
|
||||
return '<img src="{}" width="80" />'.format(obj.image_box)
|
||||
box_image.allow_tags = True
|
||||
|
||||
|
||||
admin.site.register(Collection, CollectionAdmin)
|
||||
admin.site.register(Amiibo, AmiiboAdmin)
|
||||
|
|
|
@ -0,0 +1,180 @@
|
|||
# coding: utf-8
|
||||
|
||||
# python3
|
||||
from __future__ import unicode_literals
|
||||
|
||||
# python
|
||||
import uuid
|
||||
import re
|
||||
|
||||
# third party
|
||||
from openpyxl import load_workbook
|
||||
import requests
|
||||
|
||||
# django
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.utils import translation
|
||||
from django.utils.text import slugify
|
||||
|
||||
# amiibofindr
|
||||
from amiibofindr.apps.amiibo.models import Amiibo, Collection
|
||||
|
||||
|
||||
class AmiiboSheet(object):
|
||||
DOC_ID = '1uOZKhVW1S25GDETsqinf0Qzl1trbKcw2wT2swfcTAfE'
|
||||
|
||||
SHEET_NA_US = 'NA (US)'
|
||||
SHEET_NA_CA = 'NA (CA)'
|
||||
SHEET_EU = 'EU'
|
||||
SHEET_JP = 'JP'
|
||||
|
||||
SERIES_STRING_US = 'series'
|
||||
SERIES_STRING_EU = 'Collection'
|
||||
SERIES_STRING_JP = 'シリーズ'
|
||||
|
||||
LINK_REGEX = r'\=HYPERLINK\(\"(.*)\",\"(.*)\"\)'
|
||||
|
||||
SHEETS = {
|
||||
SHEET_NA_US: {
|
||||
'SERIES_STRING': SERIES_STRING_US,
|
||||
},
|
||||
# SHEET_NA_CA: {
|
||||
# 'SERIES_STRING': SERIES_STRING,
|
||||
# },
|
||||
SHEET_EU: {
|
||||
'SERIES_STRING': SERIES_STRING_EU,
|
||||
},
|
||||
SHEET_JP: {
|
||||
'SERIES_STRING': SERIES_STRING_JP,
|
||||
},
|
||||
}
|
||||
|
||||
DATA = {}
|
||||
|
||||
def __init__(self):
|
||||
self.file = '/tmp/{}.xlsx'.format(uuid.uuid4())
|
||||
self.download_xlsx()
|
||||
self.workbook = load_workbook(self.file)
|
||||
|
||||
def download_xlsx(self):
|
||||
r = requests.get(
|
||||
'https://docs.google.com/spreadsheets/d/{}/export?format=xlsx'.format(
|
||||
self.DOC_ID))
|
||||
|
||||
with open(self.file, 'wb') as fd:
|
||||
for chunk in r.iter_content(255):
|
||||
fd.write(chunk)
|
||||
|
||||
def parse(self):
|
||||
for sheet in self.SHEETS:
|
||||
sheet_conf = self.SHEETS[sheet]
|
||||
ws = self.workbook[sheet]
|
||||
current_series = ''
|
||||
self.DATA[ws.title] = []
|
||||
first = True
|
||||
|
||||
for row in ws.iter_rows():
|
||||
if first:
|
||||
first = False
|
||||
continue
|
||||
|
||||
amiibo = {}
|
||||
for cell in row:
|
||||
if cell.internal_value:
|
||||
# Discard bundles
|
||||
#if cell.column == 'A' and cell.internal_value == 'BUNDLE':
|
||||
# break;
|
||||
|
||||
if (
|
||||
sheet_conf['SERIES_STRING'] in unicode(cell.value)
|
||||
and cell.column == 'A'
|
||||
):
|
||||
current_series = cell.value.replace(
|
||||
sheet_conf['SERIES_STRING'], ''
|
||||
).strip()
|
||||
continue
|
||||
else:
|
||||
amiibo['collection'] = current_series
|
||||
|
||||
cell_head = ws['{}1'.format(cell.column)].value
|
||||
|
||||
if getattr(cell, 'data_type', None) == 'f':
|
||||
matches = re.match(
|
||||
self.LINK_REGEX, cell.internal_value)
|
||||
if matches:
|
||||
if cell_head == 'amiibo':
|
||||
cell_name = 'name'
|
||||
else:
|
||||
cell_name = 'id'
|
||||
amiibo[cell_head] = {
|
||||
'link': matches.group(1),
|
||||
cell_name: matches.group(2),
|
||||
}
|
||||
else:
|
||||
amiibo[cell_head] = unicode(
|
||||
cell.internal_value).replace('.0', '')
|
||||
|
||||
if amiibo.keys():
|
||||
self.DATA[ws.title].append(amiibo)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
self.xls_parser = AmiiboSheet()
|
||||
self.xls_parser.parse()
|
||||
|
||||
self.parse_data(self.xls_parser.DATA)
|
||||
|
||||
def parse_data(self, data):
|
||||
for sheet in self.xls_parser.SHEETS:
|
||||
print('==> SHEET: {}'.format(sheet))
|
||||
for amiibo in data[sheet]:
|
||||
print(amiibo)
|
||||
if 'Model No.' in amiibo:
|
||||
try:
|
||||
amiibo_obj = Amiibo.objects.get(
|
||||
model_number=amiibo['Model No.'])
|
||||
created = False
|
||||
except Amiibo.DoesNotExist:
|
||||
created = True
|
||||
amiibo_obj = Amiibo(model_number=amiibo['Model No.'])
|
||||
|
||||
if created:
|
||||
collection_obj, new = Collection.objects.get_or_create(
|
||||
name_eu=amiibo['collection']
|
||||
)
|
||||
|
||||
amiibo_obj.collection = collection_obj
|
||||
|
||||
if isinstance(amiibo['amiibo'], unicode):
|
||||
amiibo_name = amiibo['amiibo']
|
||||
amiibo_link = ''
|
||||
else:
|
||||
amiibo_name = amiibo['amiibo']['name']
|
||||
amiibo_link = amiibo['amiibo']['link']
|
||||
|
||||
if sheet == self.xls_parser.SHEET_NA_US:
|
||||
amiibo_obj.name_us = amiibo_name
|
||||
amiibo_obj.link_us = amiibo_link
|
||||
amiibo_obj.collection.name_us = amiibo['collection']
|
||||
amiibo_obj.collection.save()
|
||||
|
||||
if sheet == self.xls_parser.SHEET_EU:
|
||||
amiibo_obj.name_eu = amiibo_name
|
||||
amiibo_obj.link_eu = amiibo_link
|
||||
|
||||
if sheet == self.xls_parser.SHEET_JP:
|
||||
amiibo_obj.name_jp = amiibo_name
|
||||
amiibo_obj.link_jp = amiibo_link
|
||||
amiibo_obj.collection.name_jp = amiibo['collection']
|
||||
amiibo_obj.collection.save()
|
||||
|
||||
if amiibo_obj.name_eu:
|
||||
amiibo_obj.slug = slugify(amiibo_obj.name_eu)
|
||||
|
||||
try:
|
||||
amiibo_obj.collection_number = int(amiibo['#'])
|
||||
except:
|
||||
amiibo_obj.collection_number = None
|
||||
|
||||
amiibo_obj.save()
|
|
@ -0,0 +1,77 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('amiibo', '0002_amiibo_box'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='collection',
|
||||
old_name='name',
|
||||
new_name='name_eu',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='amiibo',
|
||||
name='name_es',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='amiibo',
|
||||
name='original_name',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='amiibo',
|
||||
name='collection_number',
|
||||
field=models.IntegerField(null=True, blank=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='amiibo',
|
||||
name='link_eu',
|
||||
field=models.CharField(max_length=255, null=True, blank=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='amiibo',
|
||||
name='link_jp',
|
||||
field=models.CharField(max_length=255, null=True, blank=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='amiibo',
|
||||
name='link_us',
|
||||
field=models.CharField(max_length=255, null=True, blank=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='amiibo',
|
||||
name='model_number',
|
||||
field=models.CharField(max_length=20, null=True, blank=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='amiibo',
|
||||
name='name_eu',
|
||||
field=models.CharField(max_length=64, null=True, blank=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='amiibo',
|
||||
name='name_jp',
|
||||
field=models.CharField(max_length=64, null=True, blank=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='amiibo',
|
||||
name='name_us',
|
||||
field=models.CharField(max_length=64, null=True, blank=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='collection',
|
||||
name='name_jp',
|
||||
field=models.CharField(max_length=128, null=True, blank=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='collection',
|
||||
name='name_us',
|
||||
field=models.CharField(max_length=128, null=True, blank=True),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,19 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('amiibo', '0003_auto_20150616_2025'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='amiibo',
|
||||
name='visible',
|
||||
field=models.BooleanField(default=True),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,22 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('amiibo', '0004_amiibo_visible'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='amiibo',
|
||||
name='box',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='amiibo',
|
||||
name='statue',
|
||||
),
|
||||
]
|
|
@ -24,32 +24,54 @@ def image_box_upload(self, filename):
|
|||
# Models
|
||||
class Collection(models.Model):
|
||||
slug = models.SlugField(max_length=128)
|
||||
name = models.CharField(max_length=128)
|
||||
name_eu = models.CharField(max_length=128)
|
||||
name_jp = models.CharField(max_length=128, blank=True, null=True)
|
||||
name_us = models.CharField(max_length=128, blank=True, null=True)
|
||||
|
||||
@property
|
||||
def amiibos(self):
|
||||
return self.amiibos_qs.all()
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
return unicode(self.name_eu) or u''
|
||||
|
||||
|
||||
class Amiibo(models.Model):
|
||||
collection = models.ForeignKey(Collection, related_name='amiibos_qs')
|
||||
collection_number = models.IntegerField(blank=True, null=True)
|
||||
|
||||
model_number = models.CharField(max_length=20, blank=True, null=True)
|
||||
|
||||
original_name = models.CharField(max_length=64)
|
||||
slug = models.SlugField(max_length=64)
|
||||
|
||||
statue = models.ImageField(upload_to=image_statue_upload)
|
||||
box = models.ImageField(upload_to=image_box_upload, blank=True, null=True)
|
||||
name_eu = models.CharField(max_length=64, blank=True, null=True)
|
||||
name_jp = models.CharField(max_length=64, blank=True, null=True)
|
||||
name_us = models.CharField(max_length=64, blank=True, null=True)
|
||||
|
||||
name_es = models.CharField(max_length=64)
|
||||
# Links
|
||||
link_eu = models.CharField(max_length=255, blank=True, null=True)
|
||||
link_jp = models.CharField(max_length=255, blank=True, null=True)
|
||||
link_us = models.CharField(max_length=255, blank=True, null=True)
|
||||
|
||||
release_date = models.DateField(null=True, blank=True)
|
||||
|
||||
visible = models.BooleanField(default=True)
|
||||
|
||||
@property
|
||||
def image_box(self):
|
||||
return '/static/images/amiibo/{}/{}-box.jpg'.format(
|
||||
self.collection.slug, self.slug
|
||||
)
|
||||
|
||||
@property
|
||||
def image_statue(self):
|
||||
return '/static/images/amiibo/{}/{}.png'.format(
|
||||
self.collection.slug, self.slug
|
||||
)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
return unicode(self.name_eu) or u''
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.original_name
|
||||
return self.name_eu
|
||||
|
|
|
@ -108,5 +108,8 @@ USE_TZ = True
|
|||
# https://docs.djangoproject.com/en/1.8/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
STATICFILES_DIRS = (
|
||||
os.path.join(BASE_DIR, 'static'),
|
||||
)
|
||||
|
||||
MEDIA_URL = '/media/'
|
||||
|
|
Loading…
Reference in New Issue