Added favorites API functionality.

This commit is contained in:
Josh Washburne 2018-04-06 12:24:13 -04:00
parent 89d2616c65
commit d3f13e470f
2 changed files with 50 additions and 1 deletions

View file

@ -1,6 +1,7 @@
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from rest_framework.serializers import ModelSerializer from rest_framework.serializers import (IntegerField, ModelSerializer,
Serializer)
from profiles.models import RadioProfile, SongRequest from profiles.models import RadioProfile, SongRequest
from .radio import BasicSongRetrieveSerializer from .radio import BasicSongRetrieveSerializer
@ -33,6 +34,10 @@ class FullProfileSerializer(BasicProfileSerializer):
user = FullUserSerializer() user = FullUserSerializer()
class FavoriteSongSerializer(Serializer):
song = IntegerField()
class HistorySerializer(ModelSerializer): class HistorySerializer(ModelSerializer):
profile = BasicProfileSerializer() profile = BasicProfileSerializer()
song = BasicSongRetrieveSerializer() song = BasicSongRetrieveSerializer()

View file

@ -6,10 +6,13 @@ from rest_framework.permissions import AllowAny
from rest_framework.response import Response from rest_framework.response import Response
from profiles.models import RadioProfile, Rating, SongRequest from profiles.models import RadioProfile, Rating, SongRequest
from radio.models import Song
from ..permissions import IsAdminOwnerOrReadOnly from ..permissions import IsAdminOwnerOrReadOnly
from ..serializers.profiles import (BasicProfileSerializer, from ..serializers.profiles import (BasicProfileSerializer,
FullProfileSerializer, FullProfileSerializer,
FavoriteSongSerializer,
HistorySerializer) HistorySerializer)
from ..serializers.radio import BasicSongRetrieveSerializer
class ProfileViewSet(viewsets.ModelViewSet): class ProfileViewSet(viewsets.ModelViewSet):
@ -43,6 +46,47 @@ class ProfileViewSet(viewsets.ModelViewSet):
self.check_object_permissions(self.request, obj) self.check_object_permissions(self.request, obj)
return obj return obj
@action(detail=True, permission_classes=[AllowAny])
def favorites(self, request, pk=None):
profile = self.get_object()
favorites = profile.favorites.all().order_by('sorted_title')
page = self.paginate_queryset(favorites)
if page is not None:
serializer = BasicSongRetrieveSerializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = BasicSongRetrieveSerializer(favorites, many=True)
return Response(serializer.data)
def _favorite_change(self, request, remove=False):
profile = self.get_object()
serializer = FavoriteSongSerializer(data=request.data)
if serializer.is_valid():
song = Song.objects.get(pk=serializer.data['song'])
if remove:
profile.favorites.remove(song)
else:
profile.favorites.add(song)
profile.save()
message = 'Song {} favorites.'.format(('added to',
'removed from')[remove])
return Response({'detail': message})
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
@action(methods=['post'],
detail=True,
permission_classes=[IsAdminOwnerOrReadOnly])
def favorite_add(self, request, pk=None):
return self._favorite_change(request)
@action(methods=['post'],
detail=True,
permission_classes=[IsAdminOwnerOrReadOnly])
def favorite_remove(self, request, pk=None):
return self._favorite_change(request, remove=True)
class HistoryViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): class HistoryViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
permission_classes = [AllowAny] permission_classes = [AllowAny]