From 518887e82a71854c0a51cebba2b15f3275e4ffa2 Mon Sep 17 00:00:00 2001 From: Josh W Date: Tue, 10 Mar 2020 14:51:15 -0400 Subject: [PATCH] No more redundant queries for profile. Cleanup. --- savepointradio/profiles/actions.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/savepointradio/profiles/actions.py b/savepointradio/profiles/actions.py index b52da5a..6be1eee 100644 --- a/savepointradio/profiles/actions.py +++ b/savepointradio/profiles/actions.py @@ -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'