Add ability to get song requests and inform when song is played.
This commit is contained in:
parent
339a74a453
commit
aa90232897
3 changed files with 110 additions and 1 deletions
34
savepointradio/api/serializers/controls.py
Normal file
34
savepointradio/api/serializers/controls.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
from rest_framework.serializers import (IntegerField,
|
||||
ModelSerializer,
|
||||
Serializer,
|
||||
StringRelatedField)
|
||||
|
||||
from profiles.models import SongRequest
|
||||
from radio.models import Song
|
||||
|
||||
|
||||
class JustPlayedSerializer(Serializer):
|
||||
song_request = IntegerField()
|
||||
|
||||
|
||||
class MakeRequestSerializer(Serializer):
|
||||
song = IntegerField()
|
||||
|
||||
|
||||
class NextSongSerializer(ModelSerializer):
|
||||
album = StringRelatedField(many=False)
|
||||
artists = StringRelatedField(many=True)
|
||||
game = StringRelatedField(many=False)
|
||||
|
||||
class Meta:
|
||||
model = Song
|
||||
fields = ('id', 'album', 'artists', 'game',
|
||||
'song_type', 'title', 'length', 'path')
|
||||
|
||||
|
||||
class GetRequestSerializer(ModelSerializer):
|
||||
song = NextSongSerializer(many=False)
|
||||
|
||||
class Meta:
|
||||
model = SongRequest
|
||||
fields = ('id', 'song')
|
|
@ -1,13 +1,22 @@
|
|||
from django.urls import path
|
||||
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from api.views.controls import JustPlayed, NextRequest
|
||||
from api.views.radio import (AlbumViewSet, ArtistViewSet,
|
||||
GameViewSet, SongViewSet)
|
||||
|
||||
|
||||
router = DefaultRouter()
|
||||
|
||||
router.register(r'albums', AlbumViewSet, base_name='album')
|
||||
router.register(r'artists', ArtistViewSet, base_name='artist')
|
||||
router.register(r'games', GameViewSet, base_name='game')
|
||||
router.register(r'songs', SongViewSet, base_name='song')
|
||||
|
||||
urlpatterns = router.urls
|
||||
urlpatterns = [
|
||||
path('next/', NextRequest.as_view()),
|
||||
path('played/', JustPlayed.as_view()),
|
||||
]
|
||||
|
||||
urlpatterns += router.urls
|
||||
|
|
66
savepointradio/api/views/controls.py
Normal file
66
savepointradio/api/views/controls.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
from django.contrib.auth import get_user_model
|
||||
from django.utils import timezone
|
||||
|
||||
from rest_framework import status
|
||||
from rest_framework.authentication import (SessionAuthentication,
|
||||
TokenAuthentication)
|
||||
from rest_framework.generics import RetrieveAPIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from core.utils import get_setting
|
||||
from profiles.models import RadioProfile, SongRequest
|
||||
from radio.models import Song
|
||||
from ..permissions import IsAdminOrReadOnly, IsDJ
|
||||
from ..serializers.controls import (JustPlayedSerializer,
|
||||
MakeRequestSerializer,
|
||||
GetRequestSerializer)
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
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']
|
||||
song_request = SongRequest.objects.get(pk=request_pk)
|
||||
song_request.played_at = timezone.now()
|
||||
song_request.save(update_fields=['played_at'])
|
||||
|
||||
return Response(serializer.data)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class NextRequest(RetrieveAPIView):
|
||||
authentication_classes = [TokenAuthentication]
|
||||
permission_classes = [IsDJ]
|
||||
queryset = SongRequest.objects.all()
|
||||
serializer = GetRequestSerializer
|
||||
|
||||
def retrieve(self, request):
|
||||
dj_profile = RadioProfile.objects.get(user__is_dj=True)
|
||||
|
||||
limit = get_setting('songs_per_jingle')
|
||||
played_songs = SongRequest.music.get_played_requests(limit)
|
||||
if [j for j in played_songs if j.song.song_type == 'J']:
|
||||
if not SongRequest.music.unplayed().exists():
|
||||
random_song = Song.music.get_random_requestable_song()
|
||||
next_play = SongRequest.objects.create(profile=dj_profile,
|
||||
song=random_song)
|
||||
else:
|
||||
next_play = SongRequest.music.next_request()
|
||||
else:
|
||||
random_jingle = Song.music.get_random_jingle()
|
||||
next_play = SongRequest.objects.create(profile=dj_profile,
|
||||
song=random_jingle)
|
||||
|
||||
next_play.queued_at = timezone.now()
|
||||
next_play.save()
|
||||
|
||||
serializer = GetRequestSerializer(next_play, many=False)
|
||||
return Response(serializer.data)
|
Loading…
Reference in a new issue