mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 20:02:20 +01:00
Import history from old Marietje.
This commit is contained in:
33
marietje/marietje/management/commands/importhistory.py
Normal file
33
marietje/marietje/management/commands/importhistory.py
Normal file
@ -0,0 +1,33 @@
|
||||
import json
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth import get_user_model
|
||||
from songs.models import Song
|
||||
from queues.models import PlaylistSong, Queue
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Import history from a JSON file exported from a previous version of Marietje.'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('json_file')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
filename = options['json_file']
|
||||
User = get_user_model()
|
||||
|
||||
with open(filename) as fp:
|
||||
for line in fp:
|
||||
request = json.loads(line)
|
||||
|
||||
# Ignore random queue and requests without media (?).
|
||||
if request['b'] is None or request['m'] is None:
|
||||
continue
|
||||
|
||||
song = Song.objects.filter(old_id=request['m']['$oid']).first()
|
||||
user = User.objects.filter(username=request['b']).first()
|
||||
playlist = Queue.objects.first().playlist
|
||||
if song is None or user is None or playlist is None:
|
||||
continue
|
||||
playlist_song = PlaylistSong(playlist=playlist, song=song, user=user, state=2, order=0)
|
||||
playlist_song.save()
|
||||
print('Done importing history.')
|
||||
Reference in New Issue
Block a user