from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import IsAuthenticated from playlist.models import Vote from playlist.serializers import VoteSerializer class VoteListView(APIView): authentication_classes = [SessionAuthentication] permission_classes = [IsAuthenticated] def get(self, request, user_id = None): if user_id: votes = Vote.objects.filter(user_id=user_id) else: votes = Vote.objects.all() serializer = VoteSerializer(votes, many=True, context={'request': request}) return Response(serializer.data) class VoteDetailView(APIView): authentication_classes = [SessionAuthentication] permission_classes = [IsAuthenticated] def get(self, request, id): vote = Vote.objects.get(pk=id) serializer = VoteSerializer(vote, context={'request': request}) return Response(serializer.data)