Browse Source

Site is as functional as before (resolves #47)

Finished setting up graphs app and getting data from API app. Only issue
now is this branch is behind a few commits from other branches.
master
Kevin Mok 7 years ago
parent
commit
a36ce3be88
  1. 23
      api/views.py
  2. 7
      graphs/templates/graphs/artist_graph.html
  3. 1
      graphs/templates/graphs/features_graphs.html
  4. 6
      graphs/templates/graphs/genre_graph.html
  5. 6
      graphs/templates/graphs/logged_in.html
  6. 4
      graphs/urls.py
  7. 8
      graphs/utils.py
  8. 29
      graphs/views.py
  9. 10
      login/utils.py
  10. 16
      login/views.py

23
api/views.py

@ -14,6 +14,7 @@ from django.db.models import Count, Q
from .utils import *
from .models import *
from login.models import User
from login.utils import get_user_context
# }}} imports #
@ -22,7 +23,7 @@ ARTIST_LIMIT = 50
FEATURES_LIMIT = 100
# ARTIST_LIMIT = 25
# FEATURES_LIMIT = 25
TRACKS_TO_QUERY = 200
TRACKS_TO_QUERY = 100
console_logging = True
@ -114,17 +115,12 @@ def parse_library(request, user_secret):
update_track_genres(user_obj)
context = {
'user_id': user_obj.id,
'user_secret': user_obj.secret,
}
return render(request, 'api/logged_in.html', context)
return render(request, 'graphs/logged_in.html', get_user_context(user_obj))
# }}} parse_library #
# get_artist_data {{{ #
def get_artist_data(request, user_secret):
"""Returns artist data as a JSON serialized list of dictionaries
The (key, value) pairs are (artist name, song count for said artist)
@ -133,11 +129,12 @@ def get_artist_data(request, user_secret):
:param user_secret: the user secret used for identification
:return: a JsonResponse
"""
user = User.objects.get(user_secret=user_secret)
user = User.objects.get(secret=user_secret)
artist_counts = Artist.objects.annotate(num_songs=Count('track',
filter=Q(track__users=user)))
processed_artist_counts = [{'name': artist.name,
'num_songs': artist.num_songs} for artist in artist_counts]
filter=Q(track__users=user)))
processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs}
for artist in artist_counts]
pprint.pprint(processed_artist_counts)
return JsonResponse(data=processed_artist_counts, safe=False)
# }}} get_artist_data #
@ -152,7 +149,7 @@ def get_audio_feature_data(request, audio_feature, user_secret):
audio_feature: The audio feature to be queried
user_secret: client secret, used to identify the user
"""
user = User.objects.get(user_secret=user_secret)
user = User.objects.get(secret=user_secret)
user_tracks = Track.objects.filter(users=user)
response_payload = {
'data_points': [],
@ -173,7 +170,7 @@ def get_genre_data(request, user_secret):
"""Return genre data needed to create the graph user.
TODO
"""
user = User.objects.get(user_secret=user_secret)
user = User.objects.get(secret=user_secret)
genre_counts = (Track.objects.filter(users__exact=user)
.values('genre')
.order_by('genre')

7
graphs/templates/graphs/artist_graph.html

@ -6,11 +6,10 @@
<title>Artist Graphs</title>
</head>
<body>
<p>Logged in as {{ user_id }}</p>
<script src="https://d3js.org/d3.v5.js"></script>
<script src="{% static "spotifyvis/scripts/artist_graph.js" %}"></script>
<script src="{% static "graphs/scripts/artist_graph.js" %}"></script>
<script>
d3.json("{% url "get_artist_data" user_secret %}").then(function(data) {
d3.json("{% url "api:get_artist_data" user_secret %}").then(function(data) {
for (let index = 0; index < data.length; index++) {
console.log(data[index].name);
console.log(data[index].num_songs);
@ -24,4 +23,4 @@
</script>
</body>
</html>
</html>

1
graphs/templates/graphs/features_graph.html → graphs/templates/graphs/features_graphs.html

@ -20,7 +20,6 @@
<!--[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]-->
<p>Logged in as {{ user_id }}</p>
<script src="https://d3js.org/d3.v5.js"></script>
<script type="text/javascript">

6
graphs/templates/graphs/genre_graph.html

@ -13,7 +13,7 @@
<title>Test DB Page</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{% static 'spotifyvis/css/dark_bg.css' %}">
<link rel="stylesheet" href="{% static 'css/dark_bg.css' %}">
</head>
<!-- }}} header -->
@ -22,7 +22,7 @@
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/randomcolor/0.5.2/randomColor.min.js"></script>
{% load static %}
<script src="{% static "spotifyvis/scripts/genre_graph.js" %}"></script>
<script src="{% static "graphs/scripts/genre_graph.js" %}"></script>
<svg width="1920" height="740"></svg>
<script>
@ -38,7 +38,7 @@
var y = d3.scaleLinear()
.rangeRound([height, 0]);
d3.json("{% url "get_genre_data" user_secret %}").then(create_genre_graph);
d3.json("{% url "api:get_genre_data" user_secret %}").then(create_genre_graph);
</script>
</body>
</html>

6
api/templates/api/logged_in.html → graphs/templates/graphs/logged_in.html

@ -9,11 +9,11 @@
</head>
<body>
<h1>{{ user_id }}'s Graphs</h1>
<a class="btn btn-primary" href=""
<a class="btn btn-primary" href="{% url "graphs:display_audio_features" user_secret %}"
role="button">Audio Features</a>
<a class="btn btn-primary" href=""
<a class="btn btn-primary" href="{% url "graphs:display_genre_graph" user_secret %}"
role="button">Genres</a>
<a class="btn btn-primary" href="" role="button">
<a class="btn btn-primary" href="{% url "graphs:display_artist_graph" user_secret %}" role="button">
Artists
</a>
</body>

4
graphs/urls.py

@ -4,10 +4,10 @@ from .views import *
app_name = 'graphs'
urlpatterns = [
path('artists/<str:user_secret>', artist_data,
path('artists/<str:user_secret>', display_artist_graph,
name='display_artist_graph'),
path('genre/<str:user_secret>', display_genre_graph,
name='display_genre_graph'),
path('audio_features/<str:user_secret>', audio_features,
path('audio_features/<str:user_secret>', display_features_graphs,
name='display_audio_features'),
]

8
graphs/utils.py

@ -0,0 +1,8 @@
def get_secret_context(user_secret):
"""Return user_secret in context for graph pages.
:user_secret: User secret to put in context.
:returns: context with user secret.
"""
return { 'user_secret': user_secret, }

29
graphs/views.py

@ -11,41 +11,32 @@ import string
from datetime import datetime
from django.shortcuts import render, redirect
from .utils import *
# }}} imports #
def artist_data(request, user_secret):
def display_artist_graph(request, user_secret):
"""Renders the artist data graph display page
:param request: the HTTP request
:param user_secret: the user secret used for identification
:return: render the artist data graph display page
"""
user = User.objects.get(user_secret=user_secret)
context = {
'user_id': user.user_id,
'user_secret': user_secret,
}
return render(request, "spotifyvis/artist_graph.html", context)
return render(request, "graphs/artist_graph.html",
get_secret_context(user_secret))
def display_genre_graph(request, user_secret):
user = User.objects.get(user_secret=user_secret)
context = {
'user_secret': user_secret,
}
return render(request, "spotifyvis/genre_graph.html", context)
return render(request, "graphs/genre_graph.html",
get_secret_context(user_secret))
def audio_features(request, user_secret):
def display_features_graphs(request, user_secret):
"""Renders the audio features page
:param request: the HTTP request
:param user_secret: user secret used for identification
:return: renders the audio features page
"""
user = User.objects.get(user_secret=user_secret)
context = {
'user_id': user.user_id,
'user_secret': user_secret,
}
return render(request, "spotifyvis/audio_features.html", context)
return render(request, "graphs/features_graphs.html",
get_secret_context(user_secret))

10
login/utils.py

@ -0,0 +1,10 @@
from .models import User
def get_user_context(user_obj):
"""Get context for rendering with User's ID and secret.
:user_obj: User object to make context for.
:returns: context to pass back to HTML file.
"""
return { 'user_id': user_obj.id, 'user_secret': user_obj.secret, }

16
login/views.py

@ -13,6 +13,7 @@ from datetime import datetime
from django.shortcuts import render, redirect
from django.http import HttpResponseBadRequest
from .models import *
from .utils import *
# }}} imports #
@ -94,11 +95,7 @@ def callback(request):
token_response['access_token'],
token_response['expires_in'])
context = {
'user_id': user_obj.id,
'user_secret': user_obj.secret,
}
return render(request, 'login/scan.html', context)
return render(request, 'login/scan.html', get_user_context(user_obj))
# return redirect('user/' + user_obj.secret)
@ -139,12 +136,7 @@ def admin_graphs(request):
"""
user_id = "polarbier"
# user_id = "chrisshyi13"
user_obj = User.objects.get(user_id=user_id)
context = {
'user_id': user_id,
'user_secret': user_obj.user_secret,
}
update_track_genres(user_obj)
return render(request, 'login/logged_in.html', context)
user_obj = User.objects.get(id=user_id)
return render(request, 'graphs/logged_in.html', get_user_context(user_obj))
# }}} admin_graphs #
Loading…
Cancel
Save