Remove more redundant queries.
This commit is contained in:
parent
0a6422afcd
commit
c8568335b8
1 changed files with 8 additions and 12 deletions
|
@ -201,10 +201,9 @@ class SongViewSet(viewsets.ModelViewSet):
|
|||
def favorite(self, request, pk=None):
|
||||
'''Add a song to the user's favorites list.'''
|
||||
song = self.get_object()
|
||||
profile = RadioProfile.objects.get(user=request.user)
|
||||
if song not in profile.favorites.all():
|
||||
profile.favorites.add(song)
|
||||
profile.save()
|
||||
if song not in request.user.profile.favorites.all():
|
||||
request.user.profile.favorites.add(song)
|
||||
request.user.profile.save()
|
||||
return Response({'detail': 'Song has been added to favorites.'})
|
||||
message = 'Song is already a favorite.'
|
||||
return Response({'detail': message},
|
||||
|
@ -216,10 +215,9 @@ class SongViewSet(viewsets.ModelViewSet):
|
|||
def unfavorite(self, request, pk=None):
|
||||
'''Remove a song from the user's favorites list.'''
|
||||
song = self.get_object()
|
||||
profile = RadioProfile.objects.get(user=request.user)
|
||||
if song in profile.favorites.all():
|
||||
profile.favorites.remove(song)
|
||||
profile.save()
|
||||
if song in request.user.profile.favorites.all():
|
||||
request.user.profile.favorites.remove(song)
|
||||
request.user.profile.save()
|
||||
message = 'Song has been removed from favorites.'
|
||||
return Response({'detail': message})
|
||||
message = 'Song is already not a favorite.'
|
||||
|
@ -248,10 +246,9 @@ class SongViewSet(viewsets.ModelViewSet):
|
|||
serializer = RateSongSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
song = self.get_object()
|
||||
profile = RadioProfile.objects.get(user=request.user)
|
||||
if 'value' in serializer.data:
|
||||
rating, created = Rating.objects.update_or_create(
|
||||
profile=profile,
|
||||
profile=request.user.profile,
|
||||
song=song,
|
||||
defaults={'value': serializer.data['value']}
|
||||
)
|
||||
|
@ -270,8 +267,7 @@ class SongViewSet(viewsets.ModelViewSet):
|
|||
def unrate(self, request, pk=None):
|
||||
'''Remove a user's rating from a song.'''
|
||||
song = self.get_object()
|
||||
profile = RadioProfile.objects.get(user=request.user)
|
||||
rating = song.rating_set.filter(profile=profile)
|
||||
rating = song.rating_set.filter(profile=request.user.profile)
|
||||
if rating:
|
||||
rating.delete()
|
||||
return Response({'detail': 'Rating deleted from song.'})
|
||||
|
|
Loading…
Reference in a new issue