Files
vrobbler/vrobbler/apps/music/views.py
Colin Powell bf7e5677e6
All checks were successful
build & deploy / test (push) Successful in 1m51s
build & deploy / build-and-deploy (push) Successful in 30s
[charts] Clean up how they're displayed
2026-05-15 16:03:53 -04:00

97 lines
2.7 KiB
Python

from django.db.models import Count
from django.views import generic
from music.models import Album, Artist, Track
from charts.models import ChartRecord
from scrobbles.stats import get_scrobble_count_qs
from scrobbles.views import ScrobbleableListView, ScrobbleableDetailView
class TrackListView(ScrobbleableListView):
model = Track
class TrackDetailView(ScrobbleableDetailView):
model = Track
class ArtistListView(generic.ListView):
model = Artist
paginate_by = 100
def get_queryset(self):
return (
super()
.get_queryset()
.annotate(scrobble_count=Count("track__scrobble"))
.order_by("-scrobble_count")
)
def get_context_data(self, *, object_list=None, **kwargs):
context_data = super().get_context_data(object_list=object_list, **kwargs)
context_data["view"] = self.request.GET.get("view")
return context_data
class ArtistDetailView(generic.DetailView):
model = Artist
slug_field = "uuid"
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
artist = context_data["object"]
ranked_tracks = sorted(
artist.tracks.all(), key=lambda t: t.scrobble_count, reverse=True
)[:10]
tracks_ranked = [
(rank, track) for rank, track in enumerate(ranked_tracks, start=1)
]
context_data["tracks_ranked"] = tracks_ranked
charts_qs = ChartRecord.objects.filter(
artist=self.object, rank__in=[1, 2, 3]
).exclude(day__isnull=False)
from collections import OrderedDict
grouped = OrderedDict()
for chart in charts_qs:
grouped.setdefault(chart.period_type, []).append(chart)
context_data["charts"] = grouped
from scrobbles.models import Scrobble
context_data["recent_scrobbles"] = (
Scrobble.objects.filter(track__artist=artist)
.select_related("track", "track__album")
.order_by("-timestamp")[:100]
)
return context_data
class AlbumListView(generic.ListView):
model = Album
def get_queryset(self):
return (
super()
.get_queryset()
.annotate(scrobble_count=Count("track__scrobble"))
.order_by("-scrobble_count")
)
class AlbumDetailView(generic.DetailView):
model = Album
slug_field = "uuid"
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
# context_data['charts'] = ChartRecord.objects.filter(
# track__album=self.object, rank__in=[1, 2, 3]
# )
return context_data