2018-03-30 02:59:51 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2020-02-16 02:12:30 +00:00
|
|
|
from dynamic_preferences.registries import global_preferences_registry
|
2018-03-30 02:59:51 +00:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.authentication import (SessionAuthentication,
|
|
|
|
TokenAuthentication)
|
|
|
|
from rest_framework.generics import RetrieveAPIView
|
2018-03-30 21:22:18 +00:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2020-02-08 05:07:04 +00:00
|
|
|
from rest_framework.renderers import BrowsableAPIRenderer, JSONRenderer
|
2018-03-30 02:59:51 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
2018-03-30 21:22:18 +00:00
|
|
|
from profiles.exceptions import MakeRequestError
|
2018-03-30 02:59:51 +00:00
|
|
|
from profiles.models import RadioProfile, SongRequest
|
|
|
|
from radio.models import Song
|
2018-04-02 20:51:48 +00:00
|
|
|
from ..permissions import IsDJ
|
2020-02-08 05:07:04 +00:00
|
|
|
from ..renderers import build_annotate_string, LiquidsoapAnnotateRenderer
|
2018-03-30 02:59:51 +00:00
|
|
|
from ..serializers.controls import (JustPlayedSerializer,
|
|
|
|
MakeRequestSerializer,
|
|
|
|
GetRequestSerializer)
|
|
|
|
|
|
|
|
|
|
|
|
User = get_user_model()
|
2020-02-16 02:12:30 +00:00
|
|
|
radio_settings = global_preferences_registry.manager()
|
2018-03-30 02:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JustPlayed(APIView):
|
|
|
|
authentication_classes = [TokenAuthentication]
|
|
|
|
permission_classes = [IsDJ]
|
|
|
|
|
|
|
|
def post(self, request, format=None):
|
|
|
|
serializer = JustPlayedSerializer(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
request_pk = serializer.data['song_request']
|
2019-05-20 19:56:19 +00:00
|
|
|
try:
|
|
|
|
song_request = SongRequest.objects.get(pk=request_pk)
|
|
|
|
except SongRequest.DoesNotExist:
|
|
|
|
return Response({'detail': 'Song request does not exist.'},
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
if song_request.played_at:
|
|
|
|
return Response({'detail': 'Song request was already played.'},
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
2018-03-30 02:59:51 +00:00
|
|
|
song_request.played_at = timezone.now()
|
|
|
|
song_request.save(update_fields=['played_at'])
|
|
|
|
|
2019-05-20 19:56:19 +00:00
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
2018-03-30 02:59:51 +00:00
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
|
|
|
class NextRequest(RetrieveAPIView):
|
|
|
|
authentication_classes = [TokenAuthentication]
|
|
|
|
permission_classes = [IsDJ]
|
|
|
|
queryset = SongRequest.objects.all()
|
2020-02-08 05:07:04 +00:00
|
|
|
renderer_classes = [
|
|
|
|
JSONRenderer,
|
|
|
|
BrowsableAPIRenderer,
|
|
|
|
LiquidsoapAnnotateRenderer
|
|
|
|
]
|
2018-03-30 02:59:51 +00:00
|
|
|
serializer = GetRequestSerializer
|
|
|
|
|
|
|
|
def retrieve(self, request):
|
|
|
|
dj_profile = RadioProfile.objects.get(user__is_dj=True)
|
|
|
|
|
2020-02-16 02:12:30 +00:00
|
|
|
limit = radio_settings['general__songs_per_jingle']
|
2019-11-24 00:42:29 +00:00
|
|
|
queued_songs = SongRequest.music.get_queued_requests(limit)
|
|
|
|
if [j for j in queued_songs if j.song.is_jingle]:
|
|
|
|
if not SongRequest.music.new_requests().exists():
|
2018-03-30 02:59:51 +00:00
|
|
|
random_song = Song.music.get_random_requestable_song()
|
2020-02-15 18:33:48 +00:00
|
|
|
next_request = SongRequest.objects.create(profile=dj_profile,
|
|
|
|
song=random_song)
|
2018-03-30 02:59:51 +00:00
|
|
|
else:
|
2020-02-15 18:33:48 +00:00
|
|
|
next_request = SongRequest.music.next_request()
|
2018-03-30 02:59:51 +00:00
|
|
|
else:
|
|
|
|
random_jingle = Song.music.get_random_jingle()
|
2020-02-15 18:33:48 +00:00
|
|
|
next_request = SongRequest.objects.create(profile=dj_profile,
|
|
|
|
song=random_jingle)
|
2018-03-30 02:59:51 +00:00
|
|
|
|
2020-02-15 18:33:48 +00:00
|
|
|
next_request.queued_at = timezone.now()
|
|
|
|
next_request.save(update_fields=['queued_at'])
|
2018-03-30 02:59:51 +00:00
|
|
|
|
2020-02-15 18:33:48 +00:00
|
|
|
serializer = GetRequestSerializer(next_request, many=False)
|
2020-02-08 05:07:04 +00:00
|
|
|
|
|
|
|
media_type = request.accepted_renderer.media_type
|
|
|
|
if media_type == 'application/vnd.liquidsoap.annotate':
|
|
|
|
return Response(build_annotate_string(serializer.data))
|
2018-03-30 02:59:51 +00:00
|
|
|
return Response(serializer.data)
|
2018-03-30 21:22:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MakeRequest(APIView):
|
|
|
|
authentication_classes = [SessionAuthentication, TokenAuthentication]
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
|
|
|
|
def post(self, request, format=None):
|
|
|
|
serializer = MakeRequestSerializer(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
profile = RadioProfile.objects.get(user=request.user)
|
|
|
|
try:
|
|
|
|
profile.make_request(serializer.data['song'])
|
|
|
|
except MakeRequestError as e:
|
|
|
|
return Response({'detail': str(e)},
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
2019-05-20 19:56:19 +00:00
|
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
2018-03-30 21:22:18 +00:00
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|