[music] Fix slow loading artist page

This commit is contained in:
2026-06-01 10:50:22 -04:00
parent ce302e4d45
commit 410c033f12
3 changed files with 39 additions and 18 deletions

View File

@ -19,11 +19,7 @@ class ArtistListView(generic.ListView):
paginate_by = 100
def get_queryset(self):
qs = (
super()
.get_queryset()
.annotate(scrobble_count=Count("track__scrobble"))
)
qs = super().get_queryset().annotate(scrobble_count=Count("track__scrobble"))
genre = self.request.GET.get("genre")
if genre:
qs = qs.filter(theaudiodb_genre=genre)
@ -82,26 +78,30 @@ class ArtistDetailView(generic.DetailView):
similar = artist.similar_artists or []
if similar:
mbids = [sa["artist_mbid"] for sa in similar if sa.get("artist_mbid")]
top = similar[:10]
mbids = [sa["artist_mbid"] for sa in top if sa.get("artist_mbid")]
local_artists = {
a.musicbrainz_id: a
for a in Artist.objects.filter(musicbrainz_id__in=mbids)
}
for sa in similar:
for sa in top:
local = local_artists.get(sa.get("artist_mbid"))
sa["local_url"] = local.get_absolute_url() if local else None
context_data["similar_artists"] = similar
sa["musicbrainz_url"] = (
f"https://musicbrainz.org/artist/{sa['artist_mbid']}"
if sa.get("artist_mbid")
else None
)
context_data["similar_artists"] = top
if artist.theaudiodb_genre:
context_data["genre_count"] = (
Artist.objects.filter(theaudiodb_genre=artist.theaudiodb_genre)
.count()
)
context_data["genre_count"] = Artist.objects.filter(
theaudiodb_genre=artist.theaudiodb_genre
).count()
if artist.theaudiodb_mood:
context_data["mood_count"] = (
Artist.objects.filter(theaudiodb_mood=artist.theaudiodb_mood)
.count()
)
context_data["mood_count"] = Artist.objects.filter(
theaudiodb_mood=artist.theaudiodb_mood
).count()
return context_data

View File

@ -53,13 +53,15 @@
<tr>
<th scope="col">Artist</th>
<th scope="col">Score</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for sa in similar_artists|slice:":10" %}
{% for sa in similar_artists %}
<tr>
<td>{% if sa.local_url %}<a href="{{sa.local_url}}">{{sa.name}}</a>{% else %}{{sa.name}}{% endif %}</td>
<td>{{sa.score}}</td>
<td>{% if sa.musicbrainz_url %}<a href="{{sa.musicbrainz_url}}">musicbrainz</a>{% endif %}</td>
</tr>
{% endfor %}
</tbody>