|
|
@ -394,10 +394,13 @@ def process_library_stats(library_stats): |
|
|
|
|
|
|
|
# get_artists_in_genre {{{ # |
|
|
|
|
|
|
|
def get_artists_in_genre(user, genre): |
|
|
|
def get_artists_in_genre(user, genre, max_songs): |
|
|
|
"""Return count of artists in genre. |
|
|
|
|
|
|
|
:user: User object to return data for. |
|
|
|
:genre: genre to count artists for. |
|
|
|
:max_songs: max total songs to include to prevent overflow due to having |
|
|
|
multiple artists on each track. |
|
|
|
|
|
|
|
:returns: dict of artists in the genre along with the number of songs they |
|
|
|
have. |
|
|
@ -405,9 +408,16 @@ def get_artists_in_genre(user, genre): |
|
|
|
artist_counts = (Artist.objects.filter(track__users=user) |
|
|
|
.filter(track__genre=genre) |
|
|
|
.annotate(num_songs=Count('track', distinct=True)) |
|
|
|
.order_by('-num_songs') |
|
|
|
) |
|
|
|
processed_artist_counts = {} |
|
|
|
songs_added = 0 |
|
|
|
for artist in artist_counts: |
|
|
|
if songs_added + artist.num_songs <= max_songs: |
|
|
|
processed_artist_counts[artist.name] = artist.num_songs |
|
|
|
songs_added += artist.num_songs |
|
|
|
# processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts] |
|
|
|
processed_artist_counts = {artist.name: artist.num_songs for artist in artist_counts} |
|
|
|
# processed_artist_counts = {artist.name: artist.num_songs for artist in artist_counts} |
|
|
|
# pprint.pprint(processed_artist_counts) |
|
|
|
return processed_artist_counts |
|
|
|
|
|
|
|