from django.utils import timezone from django.http import JsonResponse from django.shortcuts import get_object_or_404 from django.views.decorators.csrf import csrf_exempt from marietje.utils import playlist_song_to_dict from queues.models import Queue from songs.models import Song from .decorators import token_required @csrf_exempt @token_required def queue(request): queue = get_object_or_404(Queue, id=request.POST.get('queue')) commands = queue.queuecommand_set.filter(executed=False) for command in commands: command.executed = True command.save() return JsonResponse({ 'current_song': playlist_song_to_dict(queue.current_song(), hash=True, replaygain=True), 'queue': [playlist_song_to_dict(playlist_song, hash=True, replaygain=True) for playlist_song in queue.queue()[:1]], 'commands': [command.command for command in commands] }) @csrf_exempt @token_required def play(request): queue = get_object_or_404(Queue, id=request.POST.get('queue')) queue.started_at = timezone.now() queue.save() current_song = queue.current_song() current_song.played_at = queue.started_at current_song.save() return JsonResponse({}) @csrf_exempt @token_required def next(request): queue = get_object_or_404(Queue, id=request.POST.get('queue')) player_song = queue.current_song() player_song.state = 2 player_song.save() return JsonResponse({}) @csrf_exempt @token_required def analysed(request): song = get_object_or_404(Song, id=request.POST.get('song')) if 'gain' in request.POST: song.rg_gain = request.POST.get('gain') if 'peak' in request.POST: song.rg_peak = request.POST.get('peak') song.save() return JsonResponse({})