Add inline admin action for requesting songs.

This commit is contained in:
Josh W 2020-01-21 14:51:27 -05:00
parent fbc8829927
commit 225a2c5b26
2 changed files with 24 additions and 2 deletions

View file

@ -3,6 +3,7 @@ asgiref>=3.2.3
cffi>=1.13.2 cffi>=1.13.2
dj-database-url>=0.5.0 dj-database-url>=0.5.0
Django>=3.0.2 Django>=3.0.2
django-inline-actions>=2.3.0
djangorestframework>=3.11.0 djangorestframework>=3.11.0
psycopg2-binary>=2.8.4 psycopg2-binary>=2.8.4
pycparser>=2.19 pycparser>=2.19

View file

@ -1,9 +1,13 @@
from django.contrib import admin from django.contrib import admin, messages
from django.db import models from django.db import models
from django.forms import TextInput from django.forms import TextInput
from inline_actions.admin import InlineActionsModelAdminMixin
from .actions import change_items, publish_items, remove_items from .actions import change_items, publish_items, remove_items
from .models import Album, Artist, Game, Song, Store from .models import Album, Artist, Game, Song, Store
from profiles.exceptions import MakeRequestError
from profiles.models import RadioProfile
class ArtistInline(admin.TabularInline): class ArtistInline(admin.TabularInline):
@ -137,7 +141,7 @@ class StoreAdmin(admin.ModelAdmin):
@admin.register(Song) @admin.register(Song)
class SongAdmin(admin.ModelAdmin): class SongAdmin(InlineActionsModelAdminMixin, admin.ModelAdmin):
formfield_overrides = { formfield_overrides = {
models.TextField: {'widget': TextInput(attrs={'size': 160, })}, models.TextField: {'widget': TextInput(attrs={'size': 160, })},
} }
@ -190,6 +194,7 @@ class SongAdmin(admin.ModelAdmin):
}) })
) )
inlines = [ArtistInline, StoreInline] inlines = [ArtistInline, StoreInline]
inline_actions = ['request_song']
def formfield_for_foreignkey(self, db_field, request, **kwargs): def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'active_store': if db_field.name == 'active_store':
@ -198,6 +203,8 @@ class SongAdmin(admin.ModelAdmin):
) )
return super().formfield_for_foreignkey(db_field, request, **kwargs) return super().formfield_for_foreignkey(db_field, request, **kwargs)
# Admin Actions
def artist_list(self, obj): def artist_list(self, obj):
return ', '.join([a.full_name for a in obj.artists.all()]) return ', '.join([a.full_name for a in obj.artists.all()])
@ -230,3 +237,17 @@ class SongAdmin(admin.ModelAdmin):
def publish_songs(self, request, queryset): def publish_songs(self, request, queryset):
publish_items(request, queryset) publish_items(request, queryset)
publish_songs.short_description = "Publish selected songs" publish_songs.short_description = "Publish selected songs"
# Inline Admin Actions
def request_song(self, request, obj, parent_obj=None):
profile = RadioProfile.objects.get(user=request.user)
try:
profile.make_request(obj)
except MakeRequestError as e:
return messages.error(
request,
'Unable to request the song: {}'.format(str(e))
)
return messages.success(request, 'Successfully queued song.')
request_song.short_description = 'Request Song'