From a36ce3be88f84ef212aa563d4d046517f8ebba1f Mon Sep 17 00:00:00 2001 From: Kevin Mok Date: Fri, 29 Jun 2018 11:08:40 -0400 Subject: [PATCH] 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. --- api/views.py | 23 +++++++-------- graphs/templates/graphs/artist_graph.html | 7 ++--- ...atures_graph.html => features_graphs.html} | 1 - graphs/templates/graphs/genre_graph.html | 6 ++-- .../templates/graphs}/logged_in.html | 6 ++-- graphs/urls.py | 4 +-- graphs/utils.py | 8 +++++ graphs/views.py | 29 +++++++------------ login/utils.py | 10 +++++++ login/views.py | 16 +++------- 10 files changed, 53 insertions(+), 57 deletions(-) rename graphs/templates/graphs/{features_graph.html => features_graphs.html} (99%) rename {api/templates/api => graphs/templates/graphs}/logged_in.html (65%) create mode 100644 graphs/utils.py create mode 100644 login/utils.py diff --git a/api/views.py b/api/views.py index 0fb13df..5a482d2 100644 --- a/api/views.py +++ b/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') diff --git a/graphs/templates/graphs/artist_graph.html b/graphs/templates/graphs/artist_graph.html index 433e39b..4d0f183 100644 --- a/graphs/templates/graphs/artist_graph.html +++ b/graphs/templates/graphs/artist_graph.html @@ -6,11 +6,10 @@ Artist Graphs -

Logged in as {{ user_id }}

- + - \ No newline at end of file + diff --git a/graphs/templates/graphs/features_graph.html b/graphs/templates/graphs/features_graphs.html similarity index 99% rename from graphs/templates/graphs/features_graph.html rename to graphs/templates/graphs/features_graphs.html index 78e2e50..cc9fe52 100644 --- a/graphs/templates/graphs/features_graph.html +++ b/graphs/templates/graphs/features_graphs.html @@ -20,7 +20,6 @@ -

Logged in as {{ user_id }}

{% load static %} - + diff --git a/api/templates/api/logged_in.html b/graphs/templates/graphs/logged_in.html similarity index 65% rename from api/templates/api/logged_in.html rename to graphs/templates/graphs/logged_in.html index b6e4a69..d553f92 100644 --- a/api/templates/api/logged_in.html +++ b/graphs/templates/graphs/logged_in.html @@ -9,11 +9,11 @@

{{ user_id }}'s Graphs

- Audio Features - Genres - + Artists diff --git a/graphs/urls.py b/graphs/urls.py index 0453083..e1bf0b2 100644 --- a/graphs/urls.py +++ b/graphs/urls.py @@ -4,10 +4,10 @@ from .views import * app_name = 'graphs' urlpatterns = [ - path('artists/', artist_data, + path('artists/', display_artist_graph, name='display_artist_graph'), path('genre/', display_genre_graph, name='display_genre_graph'), - path('audio_features/', audio_features, + path('audio_features/', display_features_graphs, name='display_audio_features'), ] diff --git a/graphs/utils.py b/graphs/utils.py new file mode 100644 index 0000000..d2cf67c --- /dev/null +++ b/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, } diff --git a/graphs/views.py b/graphs/views.py index e65f73b..25c8177 100644 --- a/graphs/views.py +++ b/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)) diff --git a/login/utils.py b/login/utils.py new file mode 100644 index 0000000..695bfae --- /dev/null +++ b/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, } diff --git a/login/views.py b/login/views.py index 74e0188..9406d19 100644 --- a/login/views.py +++ b/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 #