2019-06-03 14:59:18 +00:00
|
|
|
'''
|
|
|
|
Django management command to import old playlist data. This should only be used
|
|
|
|
for seeding a newly created database.
|
|
|
|
'''
|
|
|
|
|
|
|
|
import decimal
|
|
|
|
import json
|
2018-01-04 21:29:59 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
|
2019-06-03 14:59:18 +00:00
|
|
|
from core.utils import path_to_iri
|
|
|
|
from radio.models import Album, Artist, Game, Store, Song
|
2018-01-04 21:29:59 +00:00
|
|
|
|
2019-06-03 14:59:18 +00:00
|
|
|
decimal.getcontext().prec = 8
|
2018-01-04 21:29:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2019-06-03 14:59:18 +00:00
|
|
|
'''Main "importoldreadio" command class'''
|
|
|
|
help = 'Imports the old radio data from an exported playlist'
|
2018-01-04 21:29:59 +00:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2019-06-03 14:59:18 +00:00
|
|
|
parser.add_argument('playlist_file', nargs=1)
|
2018-01-04 21:29:59 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2019-06-03 14:59:18 +00:00
|
|
|
playlist_file = options['playlist_file'][0]
|
|
|
|
if not os.path.isfile(playlist_file):
|
2018-01-04 21:29:59 +00:00
|
|
|
raise CommandError('File does not exist')
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
with open(playlist_file, 'r', encoding='utf8') as pfile:
|
|
|
|
playlist = json.load(pfile, parse_float=decimal.Decimal)
|
|
|
|
|
|
|
|
totals = {
|
|
|
|
'albums': 0,
|
|
|
|
'artists': 0,
|
|
|
|
'games': 0,
|
|
|
|
'songs': 0,
|
|
|
|
'jingles': 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Fetching albums first
|
|
|
|
for album in playlist['albums']:
|
|
|
|
Album.objects.create(title=album['title'],
|
|
|
|
disabled=album['disabled'])
|
2019-06-03 19:02:43 +00:00
|
|
|
totals['albums'] += 1
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
self.stdout.write('Imported {} albums'.format(str(totals['albums'])))
|
|
|
|
|
|
|
|
# Next up, artists
|
|
|
|
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'])
|
2019-06-03 19:02:43 +00:00
|
|
|
totals['artists'] += 1
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
self.stdout.write('Imported {} artists'.format(str(totals['artists'])))
|
|
|
|
|
|
|
|
# On to games
|
|
|
|
for game in playlist['games']:
|
|
|
|
Game.objects.create(title=game['title'],
|
|
|
|
disabled=game['disabled'])
|
2019-06-03 19:02:43 +00:00
|
|
|
totals['games'] += 1
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
self.stdout.write('Imported {} games'.format(str(totals['games'])))
|
|
|
|
|
|
|
|
# Followed by the songs
|
|
|
|
for song in playlist['songs']:
|
|
|
|
try:
|
|
|
|
album = Album.objects.get(title__exact=song['album'])
|
|
|
|
except Album.DoesNotExist:
|
|
|
|
album = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
game = Game.objects.get(title__exact=song['game'])
|
|
|
|
except Game.DoesNotExist:
|
|
|
|
game = None
|
|
|
|
|
|
|
|
new_song = Song.objects.create(album=album,
|
|
|
|
game=game,
|
|
|
|
disabled=song['disabled'],
|
|
|
|
song_type=song['type'],
|
|
|
|
title=song['title'])
|
|
|
|
|
|
|
|
for artist in song['artists']:
|
|
|
|
new_artist = Artist.objects.get(
|
|
|
|
alias__exact=artist['alias'] or '',
|
|
|
|
first_name__exact=artist['first_name'] or '',
|
|
|
|
last_name__exact=artist['last_name'] or ''
|
2018-01-04 21:29:59 +00:00
|
|
|
)
|
2019-06-03 14:59:18 +00:00
|
|
|
new_song.artists.add(new_artist)
|
2018-01-04 21:29:59 +00:00
|
|
|
|
2019-06-03 14:59:18 +00:00
|
|
|
new_store = Store.objects.create(
|
|
|
|
iri=path_to_iri(song['store']['path']),
|
|
|
|
mime_type=song['store']['mime'],
|
|
|
|
file_size=song['store']['filesize'],
|
|
|
|
length=song['store']['length']
|
|
|
|
)
|
|
|
|
new_song.stores.add(new_store)
|
2019-06-04 16:22:58 +00:00
|
|
|
new_song.active_store = new_store
|
2019-06-03 14:59:18 +00:00
|
|
|
new_song.save()
|
|
|
|
if song['type'] == 'S':
|
|
|
|
totals['songs'] += 1
|
2018-01-04 21:29:59 +00:00
|
|
|
else:
|
2019-06-03 14:59:18 +00:00
|
|
|
totals['jingles'] += 1
|
|
|
|
|
|
|
|
self.stdout.write(
|
|
|
|
'Imported {} requestables ({} songs, {} jingles)'.format(
|
|
|
|
str(totals['songs'] + totals['jingles']),
|
|
|
|
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()
|
|
|
|
self.stdout.write('Published imported objects successfully')
|
|
|
|
else:
|
|
|
|
self.stdout.write('Skipped publishing songs')
|