|
@@ -9,8 +9,6 @@ from rest_framework.reverse import reverse as drf_reverse
|
|
|
|
|
|
from .models import Album, Artist, Track
|
|
|
|
|
|
-# test..
|
|
|
-
|
|
|
|
|
|
def get_api_url(obj, view="detail", params=None, title=None, request=None):
|
|
|
path = drf_reverse(
|
|
@@ -55,10 +53,15 @@ class ArtistDecadeActiveListFilter(admin.SimpleListFilter):
|
|
|
)
|
|
|
|
|
|
def queryset(self, request, queryset):
|
|
|
+ value = self.value()
|
|
|
+
|
|
|
+ if not value:
|
|
|
+ return queryset
|
|
|
+
|
|
|
try:
|
|
|
- year_since = int(self.value())
|
|
|
+ year_since = int(value)
|
|
|
year_until = year_since + 10
|
|
|
- except (TypeError, ValueError):
|
|
|
+ except ValueError:
|
|
|
return queryset
|
|
|
|
|
|
return queryset.filter(
|
|
@@ -76,11 +79,11 @@ class ArtistAlbumInline(admin.TabularInline):
|
|
|
queryset = super().get_queryset(request)
|
|
|
return queryset.annotate(track_count=Count("tracks"))
|
|
|
|
|
|
+ @admin.display(description=_("Album"))
|
|
|
def album_admin_link(self, album):
|
|
|
return get_admin_url(album)
|
|
|
|
|
|
- album_admin_link.short_description = _("Album")
|
|
|
-
|
|
|
+ @admin.display(description=_("Tracks"))
|
|
|
def tracks_admin_link(self, album):
|
|
|
return get_admin_url(
|
|
|
Track,
|
|
@@ -89,8 +92,6 @@ class ArtistAlbumInline(admin.TabularInline):
|
|
|
title=album.track_count,
|
|
|
)
|
|
|
|
|
|
- tracks_admin_link.short_description = _("Tracks")
|
|
|
-
|
|
|
|
|
|
class AlbumTrackInline(admin.TabularInline):
|
|
|
model = Track
|
|
@@ -110,24 +111,23 @@ class ArtistAdmin(admin.ModelAdmin):
|
|
|
def get_queryset(self, request):
|
|
|
self.request = request
|
|
|
queryset = super().get_queryset(request)
|
|
|
- return queryset.prefetch_related("albums").annotate(album_count=Count("albums"))
|
|
|
+ return queryset.prefetch_related("albums").annotate(
|
|
|
+ album_count=Count("albums", distinct=True)
|
|
|
+ )
|
|
|
|
|
|
+ @admin.display(description=_("Albums"), ordering="album_count")
|
|
|
def albums_admin_link(self, artist):
|
|
|
return get_admin_url(
|
|
|
Album,
|
|
|
view="changelist",
|
|
|
params={"artist": artist.pk},
|
|
|
- title=artist.album_count,
|
|
|
+ title=artist.album_count or "0",
|
|
|
)
|
|
|
|
|
|
- albums_admin_link.short_description = _("Albums")
|
|
|
- albums_admin_link.admin_order_field = "album_count"
|
|
|
-
|
|
|
+ @admin.display(description=_("API"))
|
|
|
def artist_api_link(self, artist):
|
|
|
return get_api_url(artist, request=self.request)
|
|
|
|
|
|
- artist_api_link.short_description = _("API")
|
|
|
-
|
|
|
|
|
|
@admin.register(Album)
|
|
|
class AlbumAdmin(admin.ModelAdmin):
|
|
@@ -151,41 +151,38 @@ class AlbumAdmin(admin.ModelAdmin):
|
|
|
list_select_related = ("artist",)
|
|
|
inlines = (AlbumTrackInline,)
|
|
|
|
|
|
+ def has_add_permission(self, request):
|
|
|
+ # Add albums from the Artist album inline
|
|
|
+ return False
|
|
|
+
|
|
|
def get_queryset(self, request):
|
|
|
self.request = request
|
|
|
queryset = super().get_queryset(request)
|
|
|
return queryset.annotate(track_count=Count("tracks"))
|
|
|
|
|
|
+ @admin.display(description=_("Year"), ordering="year")
|
|
|
def album_year(self, album):
|
|
|
return get_admin_url(
|
|
|
Album, view="changelist", params={"year": album.year}, title=album.year
|
|
|
)
|
|
|
|
|
|
- album_year.short_description = _("Year")
|
|
|
- album_year.admin_order_field = "year"
|
|
|
-
|
|
|
+ @admin.display(description=_("Artist"), ordering="artist__name")
|
|
|
def artist_admin_link(self, album):
|
|
|
return get_admin_url(album.artist)
|
|
|
|
|
|
- artist_admin_link.short_description = _("Artist")
|
|
|
- artist_admin_link.admin_order_field = "artist__name"
|
|
|
-
|
|
|
+ @admin.display(description=_("Tracks"), ordering="track_count")
|
|
|
def tracks_admin_link(self, album):
|
|
|
return get_admin_url(
|
|
|
Track,
|
|
|
view="changelist",
|
|
|
params={"album": album.pk},
|
|
|
- title=album.track_count,
|
|
|
+ title=album.track_count or "0",
|
|
|
)
|
|
|
|
|
|
- tracks_admin_link.short_description = _("Tracks")
|
|
|
- tracks_admin_link.admin_order_field = "track_count"
|
|
|
-
|
|
|
+ @admin.display(description=_("API"))
|
|
|
def album_api_link(self, album):
|
|
|
return get_api_url(album, request=self.request)
|
|
|
|
|
|
- album_api_link.short_description = _("API")
|
|
|
-
|
|
|
|
|
|
@admin.register(Track)
|
|
|
class TrackAdmin(admin.ModelAdmin):
|
|
@@ -210,23 +207,24 @@ class TrackAdmin(admin.ModelAdmin):
|
|
|
list_select_related = ("album", "album__artist")
|
|
|
ordering = ("name", "album__artist__name", "album__name")
|
|
|
|
|
|
+ def has_add_permission(self, request):
|
|
|
+ # Add tracks from the Album track inline
|
|
|
+ return False
|
|
|
+
|
|
|
def get_queryset(self, request):
|
|
|
self.request = request
|
|
|
queryset = super().get_queryset(request)
|
|
|
return queryset.annotate(album_year=F("album__year"))
|
|
|
|
|
|
+ @admin.display(description=_("Album"), ordering="album__name")
|
|
|
def album_admin_link(self, track):
|
|
|
return get_admin_url(track.album)
|
|
|
|
|
|
- album_admin_link.short_description = _("Album")
|
|
|
- album_admin_link.admin_order_field = "album__name"
|
|
|
-
|
|
|
+ @admin.display(description=_("Artist"), ordering="album__artist__name")
|
|
|
def artist_admin_link(self, track):
|
|
|
return get_admin_url(track.album.artist)
|
|
|
|
|
|
- artist_admin_link.short_description = _("Artist")
|
|
|
- artist_admin_link.admin_order_field = "album__artist__name"
|
|
|
-
|
|
|
+ @admin.display(description=_("Year"), ordering="album__year")
|
|
|
def album_year(self, track):
|
|
|
return get_admin_url(
|
|
|
Track,
|
|
@@ -235,10 +233,6 @@ class TrackAdmin(admin.ModelAdmin):
|
|
|
title=track.album_year,
|
|
|
)
|
|
|
|
|
|
- album_year.short_description = _("Year")
|
|
|
- album_year.admin_order_field = "album__year"
|
|
|
-
|
|
|
+ @admin.display(description=_("API"))
|
|
|
def track_api_link(self, track):
|
|
|
return get_api_url(track, request=self.request)
|
|
|
-
|
|
|
- track_api_link.short_description = _("API")
|