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): current_queue = get_object_or_404(Queue, id=request.POST.get('queue')) commands = current_queue.queuecommand_set.filter(executed=False) for command in commands: command.executed = True command.save() return JsonResponse({ 'current_song': playlist_song_to_dict(current_queue.current_song(), include_hash=True, include_replaygain=True), 'queue': [playlist_song_to_dict(playlist_song, include_hash=True, include_replaygain=True) for playlist_song in current_queue.queue()[:1]], 'commands': [command.command for command in commands] }) @csrf_exempt @token_required def play(request): current_queue = get_object_or_404(Queue, id=request.POST.get('queue')) current_queue.started_at = timezone.now() current_queue.save() current_song = current_queue.current_song() current_song.played_at = current_queue.started_at current_song.save() return JsonResponse({}) # pylint: disable=redefined-builtin @csrf_exempt @token_required def next(request): current_queue = get_object_or_404(Queue, id=request.POST.get('queue')) player_song = current_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({})