2020-02-19 20:30:47 +00:00
|
|
|
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
|
2020-03-10 18:51:15 +00:00
|
|
|
song = obj.song if not isinstance(obj, Song) else obj
|
2020-02-19 20:30:47 +00:00
|
|
|
|
|
|
|
try:
|
2020-03-10 18:51:15 +00:00
|
|
|
request.user.profile.make_request(song)
|
2020-02-19 20:30:47 +00:00
|
|
|
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):
|
2020-03-10 18:51:15 +00:00
|
|
|
return 'Queue'
|
2020-02-19 20:30:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2020-03-10 18:51:15 +00:00
|
|
|
song = obj.song if not isinstance(obj, Song) else obj
|
2020-02-19 20:30:47 +00:00
|
|
|
|
2020-03-10 18:51:15 +00:00
|
|
|
found = request.user.profile.favorites.filter(pk=song.pk).exists()
|
2020-02-19 20:30:47 +00:00
|
|
|
if found:
|
2020-03-10 18:51:15 +00:00
|
|
|
request.user.profile.favorites.remove(song)
|
2020-02-19 20:30:47 +00:00
|
|
|
else:
|
2020-03-10 18:51:15 +00:00
|
|
|
request.user.profile.favorites.add(song)
|
2020-02-19 20:30:47 +00:00
|
|
|
|
|
|
|
status = 'removed from favorites' if found else 'added to favorites'
|
|
|
|
messages.success(request, 'Song successfully {}.'.format(status))
|
|
|
|
|
|
|
|
def get_toggle_favorite_label(self, obj):
|
2020-03-10 18:51:15 +00:00
|
|
|
# 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'
|