61 lines
2 KiB
Python
61 lines
2 KiB
Python
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):
|
|
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
|
|
|
|
try:
|
|
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 'Request Song'
|
|
|
|
|
|
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):
|
|
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
|
|
|
|
found = profile.favorites.filter(pk=song.pk).exists()
|
|
if found:
|
|
profile.favorites.remove(song)
|
|
else:
|
|
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'
|