сделал ограничение для метода list
This commit is contained in:
parent
965191dd90
commit
cd188d06a6
|
|
@ -3,7 +3,7 @@ from rest_framework.viewsets import ViewSet
|
|||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework import status
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.shortcuts import get_object_or_404, get_list_or_404
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
import requests
|
||||
|
||||
|
|
@ -13,21 +13,15 @@ from .serializers import SongSerializer, FavoriteSongSerializer
|
|||
|
||||
class SongViewSet(ViewSet):
|
||||
permission_classes_by_action = {
|
||||
'list': [IsAuthenticated],
|
||||
'retrieve': [IsAuthenticated],
|
||||
}
|
||||
def list(self, request):
|
||||
queryset = Song.objects.all()
|
||||
serializer = SongSerializer(queryset, many=True)
|
||||
user_pk = request.user.pk
|
||||
queryset = get_list_or_404(FavoriteSong, user=user_pk)
|
||||
serializer = FavoriteSongSerializer(queryset, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
def get_permissions(self):
|
||||
try:
|
||||
# return permission_classes depending on `action`
|
||||
return [permission() for permission in self.permission_classes_by_action[self.action]]
|
||||
except KeyError:
|
||||
# action is not set return default permission_classes
|
||||
return [permission() for permission in self.permission_classes]
|
||||
|
||||
def retrieve(self, request, pk=None):
|
||||
user_pk = request.user.pk
|
||||
song_obj = get_object_or_404(Song, azura_id=pk).pk
|
||||
|
|
@ -77,8 +71,6 @@ class SongViewSet(ViewSet):
|
|||
else:
|
||||
return Response(serializer_1.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
|
||||
@action(detail=False, methods=['post'], schema=DeleteSongSchema())
|
||||
def delete_song(self, request):
|
||||
song_id = request.data['song_id']
|
||||
|
|
@ -86,4 +78,10 @@ class SongViewSet(ViewSet):
|
|||
item.delete()
|
||||
return Response(status=status.HTTP_202_ACCEPTED)
|
||||
|
||||
def get_permissions(self):
|
||||
try:
|
||||
return [permission() for permission in self.permission_classes_by_action[self.action]]
|
||||
except KeyError:
|
||||
return [permission() for permission in self.permission_classes]
|
||||
|
||||
|
||||
Loading…
Reference in New Issue