from django.contrib import messages from radio.models import Song from .exceptions import MakeRequestError from .models import RadioProfile class RequestSongActionMixin(object): '''This allows a song to be requested directly from an admin page.''' def get_inline_actions(self, request, obj=None): actions = super().get_inline_actions(request=request, obj=obj) actions.append('request_song') return actions def request_song(self, request, obj, parent_obj=None): # This is to get around the M2M 'through' table on Artists song = obj.song if not isinstance(obj, Song) else obj try: request.user.profile.make_request(song) except MakeRequestError as e: return messages.error( request, 'Unable to request the song: {}'.format(str(e)) ) return messages.success(request, 'Successfully queued song.') def get_request_song_label(self, obj): return 'Queue' class ToggleFavoriteActionsMixin(object): '''This allows a song to be [un]favorited directly from the admin page.''' def get_inline_actions(self, request, obj=None): actions = super().get_inline_actions(request=request, obj=obj) actions.append('toggle_favorite') return actions def toggle_favorite(self, request, obj, parent_obj=None): # This is to get around the M2M 'through' table song = obj.song if not isinstance(obj, Song) else obj found = request.user.profile.favorites.filter(pk=song.pk).exists() if found: request.user.profile.favorites.remove(song) else: 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): # 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'