from rest_framework.permissions import IsAuthenticated, AllowAny from django.shortcuts import get_object_or_404 from django.db import transaction from rest_framework.viewsets import ViewSet from rest_framework import viewsets, status, views from rest_framework.response import Response from rest_framework.decorators import api_view, permission_classes, action from django.contrib.auth.models import User from rest_framework_simplejwt.authentication import JWTAuthentication from rest_framework_simplejwt.exceptions import InvalidToken, AuthenticationFailed from .models import Profile, Team from .serializers import ProfileSerializer, TeamSerializer class ProfileViewSet(viewsets.ViewSet): queryset = Profile.objects.all() def list(self, request): queryset = Profile.objects.all() serializer = ProfileSerializer(queryset, many=True) return Response(serializer.data) @action(detail=False, methods=['post']) def AddSong(self, request): if request.method == 'POST': user = request.user.pk new_song = request.data.get('new_song') # new_song should be a dict profile = get_object_or_404(Profile, user=user) serializer = ProfileSerializer(profile, data={'likedSongs': new_song}, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @action(detail=False, methods=['post']) def DeleteSong(self, request): user = request.user.pk song_id = request.data.get('id') # Assuming each song has a unique 'id' field if not song_id: return Response({'error': 'Song ID is required'}, status=status.HTTP_400_BAD_REQUEST) profile = get_object_or_404(Profile, user=user) if profile.likedSongs: # Filter out the song with the given ID updated_songs = [song for song in profile.likedSongs if song.get('id') != song_id] profile.likedSongs = updated_songs profile.save() return Response({'message': 'Song deleted'}, status=status.HTTP_204_NO_CONTENT) else: return Response({'error': 'No songs to delete'}, status=status.HTTP_404_NOT_FOUND) @action( detail=False, methods=['get'], url_path='getlikedsongs/', url_name='liked_songs', ) def GetLikedSongs(request): if request.method == 'GET': user = request.user.pk profile = get_object_or_404(Profile, user=user) serializer = ProfileSerializer(profile) return Response(serializer.data['likedSongs'], status=status.HTTP_200_OK) class TeamViewSet(ViewSet): def list(self, request): queryset = Team.objects.all() serializer = TeamSerializer(queryset, many=True) return Response(serializer.data, status=status.HTTP_200_OK)