Add Song Request History functionality to the API.

This commit is contained in:
Josh Washburne 2018-04-05 16:23:01 -04:00
parent a94b75a663
commit 068e9e05a8
3 changed files with 21 additions and 10 deletions

View file

@ -2,18 +2,13 @@ from django.contrib.auth import get_user_model
from rest_framework.serializers import ModelSerializer
from profiles.models import (RadioProfile, SongRequest)
from profiles.models import RadioProfile, SongRequest
from .radio import BasicSongRetrieveSerializer
User = get_user_model()
class HistorySerializer(ModelSerializer):
class Meta:
model = SongRequest
fields = ('created_date', 'played_at', 'profile', 'song')
class BasicUserSerializer(ModelSerializer):
class Meta:
model = User
@ -36,3 +31,12 @@ class BasicProfileSerializer(ModelSerializer):
class FullProfileSerializer(BasicProfileSerializer):
user = FullUserSerializer()
class HistorySerializer(ModelSerializer):
profile = BasicProfileSerializer()
song = BasicSongRetrieveSerializer()
class Meta:
model = SongRequest
fields = ('created_date', 'played_at', 'profile', 'song')

View file

@ -3,7 +3,7 @@ from django.urls import path
from rest_framework.routers import DefaultRouter
from api.views.controls import JustPlayed, MakeRequest, NextRequest
from api.views.profiles import ProfileViewSet
from api.views.profiles import HistoryViewSet, ProfileViewSet
from api.views.radio import (AlbumViewSet, ArtistViewSet,
GameViewSet, SongViewSet)
@ -22,6 +22,7 @@ class OptionalSlashRouter(DefaultRouter):
router = OptionalSlashRouter()
router.register(r'history', HistoryViewSet, base_name='history')
router.register(r'profiles', ProfileViewSet, base_name='profile')
router.register(r'albums', AlbumViewSet, base_name='album')

View file

@ -1,8 +1,8 @@
from django.shortcuts import get_object_or_404
from rest_framework import status, viewsets
from rest_framework import mixins, status, viewsets
from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from profiles.models import RadioProfile, Rating, SongRequest
@ -42,3 +42,9 @@ class ProfileViewSet(viewsets.ModelViewSet):
self.is_owner = False
self.check_object_permissions(self.request, obj)
return obj
class HistoryViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
permission_classes = [AllowAny]
queryset = SongRequest.objects.all()
serializer_class = HistorySerializer