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