Fixed bug in get_artists_in_genre

Artist would show up in "undefined" genre even when they don't have that
genre. Likely due to changing genre to be a model so updated code to
adjust to that.
This commit is contained in:
2018-06-27 06:12:55 -04:00
parent 3aa49cc4e1
commit 2b98398b6c
2 changed files with 9 additions and 5 deletions

2
.gitignore vendored
View File

@@ -8,4 +8,4 @@ db.sqlite3
api-keys.sh api-keys.sh
Pipfile Pipfile
*.txt *.txt
graph.js scrap.py

View File

@@ -122,11 +122,14 @@ def update_track_genres(user):
# set genres to first artist's genres then find intersection with others # set genres to first artist's genres then find intersection with others
shared_genres = track_artists.first().genres.all() shared_genres = track_artists.first().genres.all()
for artist in track_artists: for artist in track_artists:
shared_genres.intersection(artist.genres.all()) shared_genres = shared_genres.intersection(artist.genres.all())
shared_genres = shared_genres.order_by('-num_songs')
most_common_genre = shared_genres.order_by('-num_songs').first() undefined_genre_obj = Genre.objects.get(name="undefined")
most_common_genre = shared_genres.first() if shared_genres.first() is \
not undefined_genre_obj else shared_genres[1]
track.genre = most_common_genre if most_common_genre is not None \ track.genre = most_common_genre if most_common_genre is not None \
else "undefined" else undefined_genre_obj
track.save() track.save()
# print(track.name, track.genre) # print(track.name, track.genre)
@@ -256,8 +259,9 @@ def get_artists_in_genre(user, genre, max_songs):
:returns: dict of artists in the genre along with the number of songs they :returns: dict of artists in the genre along with the number of songs they
have. have.
""" """
genre_obj = Genre.objects.get(name=genre)
artist_counts = (Artist.objects.filter(track__users=user) artist_counts = (Artist.objects.filter(track__users=user)
.filter(track__genre=genre) .filter(genres=genre_obj)
.annotate(num_songs=Count('track', distinct=True)) .annotate(num_songs=Count('track', distinct=True))
.order_by('-num_songs') .order_by('-num_songs')
) )