Optimize import and make output user-friendly.

This commit is contained in:
Josh W 2020-03-11 11:58:50 -04:00
parent 17f79d2a4c
commit 855c747155

View file

@ -9,12 +9,16 @@ import os
import re
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone
from core.utils import path_to_iri
from radio.models import Album, Artist, Game, Store, Song
decimal.getcontext().prec = 8
PROGRESS = '\rAdding {}: [{:>{width}} / {}]'
COMPLETED = 'Imported {} {}'
class Command(BaseCommand):
'''Main "importoldreadio" command class'''
@ -40,32 +44,62 @@ class Command(BaseCommand):
}
# Fetching albums first
number = len(playlist['albums']) if 'albums' in playlist else 0
self.stdout.write('\n')
for album in playlist['albums']:
Album.objects.create(title=album['title'],
disabled=album['disabled'])
totals['albums'] += 1
self.stdout.write('Imported {} albums'.format(str(totals['albums'])))
self.stdout.write(PROGRESS.format(
'albums',
totals['albums'],
number,
width=len(str(number))
), ending='')
self.stdout.flush()
self.stdout.write('\n')
self.stdout.write(COMPLETED.format(str(totals['albums']), 'albums'))
# Next up, artists
number = len(playlist['artists']) if 'artists' in playlist else 0
self.stdout.write('\n')
for artist in playlist['artists']:
Artist.objects.create(alias=artist['alias'] or '',
first_name=artist['first_name'] or '',
last_name=artist['last_name'] or '',
disabled=artist['disabled'])
totals['artists'] += 1
self.stdout.write('Imported {} artists'.format(str(totals['artists'])))
self.stdout.write(PROGRESS.format(
'artists',
totals['artists'],
number,
width=len(str(number))
), ending='')
self.stdout.flush()
self.stdout.write('\n')
self.stdout.write(COMPLETED.format(str(totals['artists']), 'artists'))
# On to games
number = len(playlist['games']) if 'games' in playlist else 0
self.stdout.write('\n')
for game in playlist['games']:
Game.objects.create(title=game['title'],
disabled=game['disabled'])
totals['games'] += 1
self.stdout.write('Imported {} games'.format(str(totals['games'])))
self.stdout.write(PROGRESS.format(
'games',
totals['games'],
number,
width=len(str(number))
), ending='')
self.stdout.flush()
self.stdout.write('\n')
self.stdout.write(COMPLETED.format(str(totals['games']), 'games'))
# Followed by the songs
number = len(playlist['songs']) if 'songs' in playlist else 0
progress = '\rAdding requestables: [{:>{width}} / {}]'
self.stdout.write('\n')
for song in playlist['songs']:
try:
album = Album.objects.get(title__exact=song['album'])
@ -128,26 +162,32 @@ class Command(BaseCommand):
else:
totals['jingles'] += 1
self.stdout.write(
'Imported {} requestables ({} songs, {} jingles)'.format(
self.stdout.write(PROGRESS.format(
'requestables',
totals['songs'] + totals['jingles'],
number,
width=len(str(number))
), ending='')
self.stdout.flush()
self.stdout.write('\n')
self.stdout.write(COMPLETED.format(
str(totals['songs'] + totals['jingles']),
'requestables ({} songs, {} jingles)'.format(
str(totals['songs']),
str(totals['jingles'])
)
)
))
pub = input('Do you want to publish all imported objects as well? '
'[Y/N] ')
if pub in ('Y', 'y'):
for album in Album.objects.all():
album.publish()
for artist in Artist.objects.all():
artist.publish()
for game in Game.objects.all():
game.publish()
for song in Song.objects.all():
song.publish()
date = timezone.now()
Album.objects.all().update(published_date=date)
Artist.objects.all().update(published_date=date)
Game.objects.all().update(published_date=date)
Song.objects.all().update(published_date=date)
self.stdout.write('Published imported objects successfully')
else:
self.stdout.write('Skipped publishing songs')