No more redundant queries for profile. Cleanup.

This commit is contained in:
Josh W 2020-03-10 14:51:15 -04:00
parent 2fdcb38aa8
commit 518887e82a

View file

@ -13,15 +13,11 @@ class RequestSongActionMixin(object):
return actions
def request_song(self, request, obj, parent_obj=None):
profile = RadioProfile.objects.get(user=request.user)
# This is to get around the M2M 'through' table on Artists
song = obj
if not isinstance(song, Song):
song = obj.song
song = obj.song if not isinstance(obj, Song) else obj
try:
profile.make_request(song)
request.user.profile.make_request(song)
except MakeRequestError as e:
return messages.error(
request,
@ -30,7 +26,7 @@ class RequestSongActionMixin(object):
return messages.success(request, 'Successfully queued song.')
def get_request_song_label(self, obj):
return 'Request Song'
return 'Queue'
class ToggleFavoriteActionsMixin(object):
@ -41,21 +37,22 @@ class ToggleFavoriteActionsMixin(object):
return actions
def toggle_favorite(self, request, obj, parent_obj=None):
profile = RadioProfile.objects.get(user=request.user)
# This is to get around the M2M 'through' table
song = obj
if not isinstance(song, Song):
song = obj.song
song = obj.song if not isinstance(obj, Song) else obj
found = profile.favorites.filter(pk=song.pk).exists()
found = request.user.profile.favorites.filter(pk=song.pk).exists()
if found:
profile.favorites.remove(song)
request.user.profile.favorites.remove(song)
else:
profile.favorites.add(song)
request.user.profile.favorites.add(song)
status = 'removed from favorites' if found else 'added to favorites'
messages.success(request, 'Song successfully {}.'.format(status))
def get_toggle_favorite_label(self, obj):
return 'Toggle Favorite'
# This is to get around the M2M 'through' table
song = obj.song if not isinstance(obj, Song) else obj
if self._request.user.profile.favorites.filter(pk=song.pk).exists():
return 'Unfavorite'
return 'Favorite'