From 27718743d74af36d9daca6db5c83749ee0e619f3 Mon Sep 17 00:00:00 2001 From: Chris Shyi Date: Tue, 5 Jun 2018 19:55:36 -0400 Subject: [PATCH] Change artists field in Track artists is now a ManyToManyField in Track, which is cleaner than using a unique_together clause in the Meta class. --- spotifyvis/migrations/0001_initial.py | 10 +++------- ...0605_0209.py => 0002_auto_20180605_2347.py} | 8 ++++---- .../migrations/0003_auto_20180605_0211.py | 18 ------------------ spotifyvis/models.py | 9 ++++----- spotifyvis/views.py | 4 +++- 5 files changed, 14 insertions(+), 35 deletions(-) rename spotifyvis/migrations/{0002_auto_20180605_0209.py => 0002_auto_20180605_2347.py} (59%) delete mode 100644 spotifyvis/migrations/0003_auto_20180605_0211.py diff --git a/spotifyvis/migrations/0001_initial.py b/spotifyvis/migrations/0001_initial.py index e22ffd2..18f13b6 100644 --- a/spotifyvis/migrations/0001_initial.py +++ b/spotifyvis/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.0.5 on 2018-06-03 23:01 +# Generated by Django 2.0.5 on 2018-06-05 20:53 from django.db import migrations, models import django.db.models.deletion @@ -43,7 +43,7 @@ class Migration(migrations.Migration): name='User', fields=[ ('user_id', models.CharField(max_length=30, primary_key=True, serialize=False)), - ('username', models.CharField(max_length=30)), + ('user_name', models.CharField(blank=True, max_length=30)), ], options={ 'verbose_name': 'User', @@ -71,15 +71,11 @@ class Migration(migrations.Migration): migrations.AddField( model_name='track', name='artist', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='spotifyvis.Artist'), + field=models.ManyToManyField(to='spotifyvis.Artist'), ), migrations.AddField( model_name='track', name='users', field=models.ManyToManyField(to='spotifyvis.User'), ), - migrations.AlterUniqueTogether( - name='track', - unique_together={('track_id', 'artist')}, - ), ] diff --git a/spotifyvis/migrations/0002_auto_20180605_0209.py b/spotifyvis/migrations/0002_auto_20180605_2347.py similarity index 59% rename from spotifyvis/migrations/0002_auto_20180605_0209.py rename to spotifyvis/migrations/0002_auto_20180605_2347.py index fe5fb37..af053ab 100644 --- a/spotifyvis/migrations/0002_auto_20180605_0209.py +++ b/spotifyvis/migrations/0002_auto_20180605_2347.py @@ -1,4 +1,4 @@ -# Generated by Django 2.0.5 on 2018-06-05 02:09 +# Generated by Django 2.0.5 on 2018-06-05 23:47 from django.db import migrations @@ -11,8 +11,8 @@ class Migration(migrations.Migration): operations = [ migrations.RenameField( - model_name='user', - old_name='username', - new_name='user_name', + model_name='track', + old_name='artist', + new_name='artists', ), ] diff --git a/spotifyvis/migrations/0003_auto_20180605_0211.py b/spotifyvis/migrations/0003_auto_20180605_0211.py deleted file mode 100644 index 9e59bb3..0000000 --- a/spotifyvis/migrations/0003_auto_20180605_0211.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.0.5 on 2018-06-05 02:11 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('spotifyvis', '0002_auto_20180605_0209'), - ] - - operations = [ - migrations.AlterField( - model_name='user', - name='user_name', - field=models.CharField(blank=True, max_length=30), - ), - ] diff --git a/spotifyvis/models.py b/spotifyvis/models.py index 02a081d..7fc5767 100644 --- a/spotifyvis/models.py +++ b/spotifyvis/models.py @@ -20,11 +20,11 @@ class User(models.Model): verbose_name = "User" verbose_name_plural = "Users" - user_id = models.CharField(primary_key=True, max_length=30) # the user's Spotify ID - user_name = models.CharField(max_length=30, blank=True) # User's Spotify user name, if set + user_id = models.CharField(primary_key=True, max_length=30) # the user's Spotify ID + user_name = models.CharField(max_length=30, blank=True) # User's Spotify user name, if set def __str__(self): - return self.username + return self.user_name class Track(models.Model): @@ -32,10 +32,9 @@ class Track(models.Model): class Meta: verbose_name = "Track" verbose_name_plural = "Tracks" - unique_together = ('track_id', 'artist',) track_id = models.CharField(max_length=30) - artist = models.ForeignKey(Artist, on_delete=models.CASCADE) + artists = models.ManyToManyField(Artist) year = models.PositiveSmallIntegerField() popularity = models.DecimalField(decimal_places=2, max_digits=2) runtime = models.PositiveSmallIntegerField() diff --git a/spotifyvis/views.py b/spotifyvis/views.py index 9a9fdd1..55ee2a3 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -16,6 +16,7 @@ TRACKS_TO_QUERY = 5 # generate_random_string {{{ # + def generate_random_string(length): """Generates a random string of a certain length @@ -145,7 +146,8 @@ def user_data(request): user = User.objects.get(user_id=request.session['user_id']) except User.DoesNotExist: user = User.objects.create(user_id=request.session['user_id'], user_name=request.session['user_name']) - + user.save() + context = { 'user_name': user_data_response['display_name'], 'id': user_data_response['id'],