Remove more redundant queries.

This commit is contained in:
Josh W 2020-03-11 12:15:57 -04:00
parent 0a6422afcd
commit c8568335b8

View file

@ -201,10 +201,9 @@ class SongViewSet(viewsets.ModelViewSet):
def favorite(self, request, pk=None): def favorite(self, request, pk=None):
'''Add a song to the user's favorites list.''' '''Add a song to the user's favorites list.'''
song = self.get_object() song = self.get_object()
profile = RadioProfile.objects.get(user=request.user) if song not in request.user.profile.favorites.all():
if song not in profile.favorites.all(): request.user.profile.favorites.add(song)
profile.favorites.add(song) request.user.profile.save()
profile.save()
return Response({'detail': 'Song has been added to favorites.'}) return Response({'detail': 'Song has been added to favorites.'})
message = 'Song is already a favorite.' message = 'Song is already a favorite.'
return Response({'detail': message}, return Response({'detail': message},
@ -216,10 +215,9 @@ class SongViewSet(viewsets.ModelViewSet):
def unfavorite(self, request, pk=None): def unfavorite(self, request, pk=None):
'''Remove a song from the user's favorites list.''' '''Remove a song from the user's favorites list.'''
song = self.get_object() song = self.get_object()
profile = RadioProfile.objects.get(user=request.user) if song in request.user.profile.favorites.all():
if song in profile.favorites.all(): request.user.profile.favorites.remove(song)
profile.favorites.remove(song) request.user.profile.save()
profile.save()
message = 'Song has been removed from favorites.' message = 'Song has been removed from favorites.'
return Response({'detail': message}) return Response({'detail': message})
message = 'Song is already not a favorite.' message = 'Song is already not a favorite.'
@ -248,10 +246,9 @@ class SongViewSet(viewsets.ModelViewSet):
serializer = RateSongSerializer(data=request.data) serializer = RateSongSerializer(data=request.data)
if serializer.is_valid(): if serializer.is_valid():
song = self.get_object() song = self.get_object()
profile = RadioProfile.objects.get(user=request.user)
if 'value' in serializer.data: if 'value' in serializer.data:
rating, created = Rating.objects.update_or_create( rating, created = Rating.objects.update_or_create(
profile=profile, profile=request.user.profile,
song=song, song=song,
defaults={'value': serializer.data['value']} defaults={'value': serializer.data['value']}
) )
@ -270,8 +267,7 @@ class SongViewSet(viewsets.ModelViewSet):
def unrate(self, request, pk=None): def unrate(self, request, pk=None):
'''Remove a user's rating from a song.''' '''Remove a user's rating from a song.'''
song = self.get_object() song = self.get_object()
profile = RadioProfile.objects.get(user=request.user) rating = song.rating_set.filter(profile=request.user.profile)
rating = song.rating_set.filter(profile=profile)
if rating: if rating:
rating.delete() rating.delete()
return Response({'detail': 'Rating deleted from song.'}) return Response({'detail': 'Rating deleted from song.'})