Adding Profile API functionality.
This commit is contained in:
parent
81c948783b
commit
62a4b8db40
3 changed files with 85 additions and 0 deletions
38
savepointradio/api/serializers/profiles.py
Normal file
38
savepointradio/api/serializers/profiles.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
from django.contrib.auth import get_user_model
|
||||
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
|
||||
from profiles.models import (RadioProfile, SongRequest)
|
||||
|
||||
|
||||
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
|
||||
fields = ('id', 'name', 'is_staff')
|
||||
|
||||
|
||||
class FullUserSerializer(ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('id', 'email', 'name', 'is_staff', 'is_active', 'last_login')
|
||||
|
||||
|
||||
class BasicProfileSerializer(ModelSerializer):
|
||||
user = BasicUserSerializer()
|
||||
|
||||
class Meta:
|
||||
model = RadioProfile
|
||||
fields = ('id', 'user')
|
||||
|
||||
|
||||
class FullProfileSerializer(BasicProfileSerializer):
|
||||
user = FullUserSerializer()
|
|
@ -3,6 +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.radio import (AlbumViewSet, ArtistViewSet,
|
||||
GameViewSet, SongViewSet)
|
||||
|
||||
|
@ -21,6 +22,8 @@ class OptionalSlashRouter(DefaultRouter):
|
|||
|
||||
router = OptionalSlashRouter()
|
||||
|
||||
router.register(r'profiles', ProfileViewSet, base_name='profile')
|
||||
|
||||
router.register(r'albums', AlbumViewSet, base_name='album')
|
||||
router.register(r'artists', ArtistViewSet, base_name='artist')
|
||||
router.register(r'games', GameViewSet, base_name='game')
|
||||
|
|
44
savepointradio/api/views/profiles.py
Normal file
44
savepointradio/api/views/profiles.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from rest_framework import status, viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.response import Response
|
||||
|
||||
from profiles.models import RadioProfile, Rating, SongRequest
|
||||
from ..permissions import IsAdminOwnerOrReadOnly
|
||||
from ..serializers.profiles import (BasicProfileSerializer,
|
||||
FullProfileSerializer,
|
||||
HistorySerializer)
|
||||
|
||||
|
||||
class ProfileViewSet(viewsets.ModelViewSet):
|
||||
permission_classes = [IsAdminOwnerOrReadOnly]
|
||||
queryset = RadioProfile.objects.all()
|
||||
serializer_class = BasicProfileSerializer
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.is_owner = False
|
||||
|
||||
def get_serializer_class(self):
|
||||
'''
|
||||
Choose a different serializer based on if the requesting user is an
|
||||
admin or is the same person as the profile requested.
|
||||
'''
|
||||
if self.request.user.is_staff or self.is_owner:
|
||||
return FullProfileSerializer
|
||||
return BasicProfileSerializer
|
||||
|
||||
def get_object(self):
|
||||
'''
|
||||
Grab the object as normal, but let us know if the requesting user is
|
||||
the owner.
|
||||
'''
|
||||
obj = get_object_or_404(self.get_queryset(), **self.kwargs)
|
||||
if self.request.user.pk == obj.user.pk:
|
||||
self.is_owner = True
|
||||
else:
|
||||
self.is_owner = False
|
||||
self.check_object_permissions(self.request, obj)
|
||||
return obj
|
Loading…
Reference in a new issue