diff --git a/spotifyvis/static/spotifyvis/scripts/test_db.js b/spotifyvis/static/spotifyvis/scripts/test_db.js new file mode 100644 index 0000000..6830820 --- /dev/null +++ b/spotifyvis/static/spotifyvis/scripts/test_db.js @@ -0,0 +1,11 @@ +console.log("{{ user_id }}"); +artist_data = JSON.parse('{{ artist_data }}'); +artist_data.forEach(function(d) { + console.log(d.name, d.num_songs); +}); + +d3.json("{% url "get_artist_data" user_id %}", function(error, data) { + data.forEach(function(d) { + console.log(d.name, d.num_songs); + }); +}); diff --git a/spotifyvis/templates/spotifyvis/index.html b/spotifyvis/templates/spotifyvis/index.html index 32d6db9..5964b34 100644 --- a/spotifyvis/templates/spotifyvis/index.html +++ b/spotifyvis/templates/spotifyvis/index.html @@ -20,6 +20,7 @@

This is an example of the Authorization Code flow

Log In (Original) + Test DB
diff --git a/spotifyvis/templates/spotifyvis/test_db.html b/spotifyvis/templates/spotifyvis/test_db.html new file mode 100644 index 0000000..3f7a0d0 --- /dev/null +++ b/spotifyvis/templates/spotifyvis/test_db.html @@ -0,0 +1,27 @@ + + + + + + + + + Test DB Page + + + + + + +
 {% filter force_escape %} {% debug %} {% endfilter %} 
+ + {% load static %} + + + diff --git a/spotifyvis/templates/spotifyvis/user_data.html b/spotifyvis/templates/spotifyvis/user_data.html index fce7fef..99823f6 100644 --- a/spotifyvis/templates/spotifyvis/user_data.html +++ b/spotifyvis/templates/spotifyvis/user_data.html @@ -21,6 +21,6 @@ {% for genre_name, genre_count in genre_dict.items %}
  • {{ genre_name }} - {{ genre_count }}
  • {% endfor %} - + diff --git a/spotifyvis/urls.py b/spotifyvis/urls.py index 0d5e894..55380bd 100644 --- a/spotifyvis/urls.py +++ b/spotifyvis/urls.py @@ -1,9 +1,13 @@ from django.urls import path, include -from . import views +from django.conf.urls import url + +from .views import * urlpatterns = [ - path('', views.index, name='index'), - path('login', views.login, name='login'), - path('callback', views.callback, name='callback'), - path('user_data', views.user_data, name='user_data'), -] \ No newline at end of file + path('', index, name='index'), + path('login', login, name='login'), + path('callback', callback, name='callback'), + path('user_data', user_data, name='user_data'), + path('test_db', test_db, name='test_db'), + path('user_artists/', get_artist_data, name='get_artist_data'), +] diff --git a/spotifyvis/utils.py b/spotifyvis/utils.py index 8ed1cc6..dc684e3 100644 --- a/spotifyvis/utils.py +++ b/spotifyvis/utils.py @@ -3,7 +3,11 @@ import requests import math import pprint + from .models import Artist, User, Track, AudioFeatures +from django.db.models import Count +from django.http import JsonResponse +from django.core import serializers # }}} imports # @@ -372,3 +376,35 @@ def process_library_stats(library_stats): return processed_library_stats # }}} process_library_stats # + +def get_genre_data(user): + """Return genre data needed to create the graph user. + + :user: User object for which to return the data for. + :returns: List of dicts containing counts for each genre. + + """ + pass + # user_tracks = Track.objects.filter(users__exact=user) + # for track in user_tracks: + # print(track.name) + + +def get_artist_data(user_id): + """Return artist data needed to create the graph for user. + + :user_id: user ID for which to return the data for. + :returns: List of dicts containing counts for each artist. + + """ + # TODO: not actual artists for user + # PICK UP: figure out how to pass data to D3/frontend + print(user_id) + # user = User.objects.get(user_id=user_id) + artist_counts = Artist.objects.annotate(num_songs=Count('track')) + processed_artist_data = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts] + # for artist in artist_counts: + # print(artist.name, artist.num_songs) + return JsonResponse(processed_artist_data, safe=False) + # return serializers.serialize('json', processed_artist_data) + # return processed_artist_data diff --git a/spotifyvis/views.py b/spotifyvis/views.py index 795ade3..3ab4f61 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -1,7 +1,5 @@ # imports {{{ # -from django.shortcuts import render, redirect -from django.http import HttpResponse, HttpResponseBadRequest import math import random import requests @@ -10,7 +8,11 @@ import urllib import json import pprint from datetime import datetime -from .utils import parse_library, process_library_stats + +from django.shortcuts import render, redirect +from django.http import HttpResponse, HttpResponseBadRequest +from django.db.models import Count +from .utils import parse_library, process_library_stats, get_artist_data from .models import User, Track, AudioFeatures, Artist # }}} imports # @@ -168,3 +170,12 @@ def user_data(request): return render(request, 'spotifyvis/user_data.html', context) # }}} user_data # + +def test_db(request): + user_id = "polarbier" + context = { + 'artist_data': get_artist_data(user_id), + 'user_id': user_id, + } + # get_artist_data(user) + return render(request, 'spotifyvis/test_db.html', context)