Add making song requests from the API.

This commit is contained in:
Josh Washburne 2018-03-30 17:22:18 -04:00
parent de74f17fb0
commit 22f3aeb693
2 changed files with 21 additions and 1 deletions

View file

@ -2,7 +2,7 @@ from django.urls import path
from rest_framework.routers import DefaultRouter
from api.views.controls import JustPlayed, NextRequest
from api.views.controls import JustPlayed, MakeRequest, NextRequest
from api.views.radio import (AlbumViewSet, ArtistViewSet,
GameViewSet, SongViewSet)
@ -17,6 +17,7 @@ router.register(r'songs', SongViewSet, base_name='song')
urlpatterns = [
path('next/', NextRequest.as_view()),
path('played/', JustPlayed.as_view()),
path('request/', MakeRequest.as_view()),
]
urlpatterns += router.urls

View file

@ -5,10 +5,12 @@ from rest_framework import status
from rest_framework.authentication import (SessionAuthentication,
TokenAuthentication)
from rest_framework.generics import RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from core.utils import get_setting
from profiles.exceptions import MakeRequestError
from profiles.models import RadioProfile, SongRequest
from radio.models import Song
from ..permissions import IsAdminOrReadOnly, IsDJ
@ -64,3 +66,20 @@ class NextRequest(RetrieveAPIView):
serializer = GetRequestSerializer(next_play, many=False)
return Response(serializer.data)
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)
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)