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