diff --git a/spotifyvis/models.py b/spotifyvis/models.py index 398647e..b6563ed 100644 --- a/spotifyvis/models.py +++ b/spotifyvis/models.py @@ -3,6 +3,22 @@ from django.db import models # id's are 22 in length in examples but set to 30 for buffer MAX_ID = 30 +# Genre {{{ # + +class Genre(models.Model): + + class Meta: + verbose_name = "Genre" + verbose_name_plural = "Genres" + + name = models.CharField(primary_key=True, max_length=50) + num_songs = models.PositiveIntegerField() + + def __str__(self): + return self.name + +# }}} Genre # + # Artist {{{ # @@ -14,6 +30,7 @@ class Artist(models.Model): artist_id = models.CharField(primary_key=True, max_length=MAX_ID) # unique since only storing one genre per artist right now name = models.CharField(unique=True, max_length=50) + genres = models.ManyToManyField(Genre, blank=True) def __str__(self): return self.name @@ -35,18 +52,6 @@ class User(models.Model): # }}} User # -class Genre(models.Model): - - class Meta: - verbose_name = "Genre" - verbose_name_plural = "Genres" - - name = models.CharField(primary_key=True, max_length=50) - num_songs = models.PositiveIntegerField() - - def __str__(self): - return self.name - # Track {{{ # class Track(models.Model): diff --git a/spotifyvis/utils.py b/spotifyvis/utils.py index e4dc4ec..7c4c088 100644 --- a/spotifyvis/utils.py +++ b/spotifyvis/utils.py @@ -48,7 +48,7 @@ def parse_library(headers, tracks, user): name=artist_dict['name'], ) if artist_created: - tally_artist_genres(headers, artist_dict['id']) + add_artist_genres(headers, artist_obj) # update_artist_genre(headers, artist_obj) # get_or_create() returns a tuple (obj, created) @@ -341,24 +341,26 @@ def get_top_genre(headers, top_artist_id): # }}} # -def tally_artist_genres(headers, artist_id): - """Tallies up genres for artist for the respective Genre models. Should be - called when new Artist object is created. +def add_artist_genres(headers, artist_obj): + """Adds genres to artist_obj and increases the count the respective Genre + object. Should be called when a new Artist object is created. :headers: For making the API call. - :artist_id: Artist ID for which to tally up genres for. + :artist_obj: Artist object for which to add/tally up genres for. :returns: None """ artist_response = requests.get('https://api.spotify.com/v1/artists/' + - artist_id, headers=headers).json() + artist_obj.artist_id, headers=headers).json() for genre in artist_response['genres']: genre_obj, created = Genre.objects.get_or_create(name=genre, defaults={'num_songs':1}) if not created: genre_obj.num_songs = F('num_songs') +1 genre_obj.save() + artist_obj.genres.add(genre_obj) + artist_obj.save() # process_library_stats {{{ #