Browse Source

Trying to pass artist data from db to frontend

master
Kevin Mok 7 years ago
parent
commit
79aedc655d
  1. 11
      spotifyvis/static/spotifyvis/scripts/test_db.js
  2. 1
      spotifyvis/templates/spotifyvis/index.html
  3. 27
      spotifyvis/templates/spotifyvis/test_db.html
  4. 2
      spotifyvis/templates/spotifyvis/user_data.html
  5. 16
      spotifyvis/urls.py
  6. 36
      spotifyvis/utils.py
  7. 17
      spotifyvis/views.py

11
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);
});
});

1
spotifyvis/templates/spotifyvis/index.html

@ -20,6 +20,7 @@
<div id="login"> <div id="login">
<h1>This is an example of the Authorization Code flow</h1> <h1>This is an example of the Authorization Code flow</h1>
<a href="/login" class="btn btn-primary">Log In (Original)</a> <a href="/login" class="btn btn-primary">Log In (Original)</a>
<a href="/test_db" class="btn btn-primary">Test DB</a>
<button id="login-btn">Log In</button> <button id="login-btn">Log In</button>
</div> </div>

27
spotifyvis/templates/spotifyvis/test_db.html

@ -0,0 +1,27 @@
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Test DB Page</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<ul>
{% for artist in artist_data %}
<li>{{ artist.name }} - {{ artist.num_songs }}</li>
{% endfor %}
</ul>
<pre> {% filter force_escape %} {% debug %} {% endfilter %} </pre>
<script src="http://d3js.org/d3.v3.js"></script>
{% load static %}
<script src="{% static "spotifyvis/scripts/test_db.js" %}"></script>
</body>
</html>

2
spotifyvis/templates/spotifyvis/user_data.html

@ -20,6 +20,6 @@
{% for genre_name, genre_count in genre_dict.items %} {% for genre_name, genre_count in genre_dict.items %}
<li>{{ genre_name }} - {{ genre_count }}</li> <li>{{ genre_name }} - {{ genre_count }}</li>
{% endfor %} {% endfor %}
</ul>
</ul>
</body> </body>
</html> </html>

16
spotifyvis/urls.py

@ -1,9 +1,13 @@
from django.urls import path, include from django.urls import path, include
from . import views
from django.conf.urls import url
from .views import *
urlpatterns = [ 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'),
]
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/<str:user_id>', get_artist_data, name='get_artist_data'),
]

36
spotifyvis/utils.py

@ -3,7 +3,11 @@
import requests import requests
import math import math
import pprint import pprint
from .models import Artist, User, Track, AudioFeatures from .models import Artist, User, Track, AudioFeatures
from django.db.models import Count
from django.http import JsonResponse
from django.core import serializers
# }}} imports # # }}} imports #
@ -372,3 +376,35 @@ def process_library_stats(library_stats):
return processed_library_stats return processed_library_stats
# }}} process_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

17
spotifyvis/views.py

@ -1,7 +1,5 @@
# imports {{{ # # imports {{{ #
from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseBadRequest
import math import math
import random import random
import requests import requests
@ -10,7 +8,11 @@ import urllib
import json import json
import pprint import pprint
from datetime import datetime 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 from .models import User, Track, AudioFeatures, Artist
# }}} imports # # }}} imports #
@ -167,3 +169,12 @@ def user_data(request):
return render(request, 'spotifyvis/user_data.html', context) return render(request, 'spotifyvis/user_data.html', context)
# }}} user_data # # }}} 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)
Loading…
Cancel
Save