diff --git a/graphs/templates/graphs/user_history.html b/graphs/templates/graphs/user_history.html index 6086518..7d2cd25 100644 --- a/graphs/templates/graphs/user_history.html +++ b/graphs/templates/graphs/user_history.html @@ -10,6 +10,7 @@

{{ user_id }}'s Listening History

+ Export CSV {% render_table user_history_table %} diff --git a/graphs/views.py b/graphs/views.py index 4bfbcdb..6a9811e 100644 --- a/graphs/views.py +++ b/graphs/views.py @@ -12,7 +12,7 @@ from datetime import datetime from django.shortcuts import render, redirect from .utils import * -from django_tables2 import RequestConfig +from django_tables2 import RequestConfig, SingleTableView from api.models import History # }}} imports # @@ -61,3 +61,9 @@ def display_history_table(request, user_secret): return render(request, "graphs/user_history.html", context) +class HistoryList(SingleTableView): + """Create table with list of song history.""" + model = History + table_class = HistoryTable + # table_data = + diff --git a/login/templates/login/index.html b/login/templates/login/index.html index 3a41ec5..eb428de 100644 --- a/login/templates/login/index.html +++ b/login/templates/login/index.html @@ -20,7 +20,7 @@

spotify-lib-vis

- Login + Login Admin Graphs
diff --git a/login/utils.py b/login/utils.py index 695bfae..1f2bdef 100644 --- a/login/utils.py +++ b/login/utils.py @@ -1,3 +1,7 @@ +import string +import random +import requests + from .models import User def get_user_context(user_obj): @@ -8,3 +12,58 @@ def get_user_context(user_obj): """ return { 'user_id': user_obj.id, 'user_secret': user_obj.secret, } + + +# generate_random_string {{{ # + +def generate_random_string(length): + """Generates a random string of a certain length + + Args: + length: the desired length of the randomized string + + Returns: + A random string + """ + all_chars = string.ascii_letters + string.digits + rand_str = "".join(random.choice(all_chars) for _ in range(length)) + + return rand_str + +# }}} generate_random_string # + +# create_user {{{ # + + +def create_user(refresh_token, access_token, access_expires_in): + """Create a User object based on information returned from Step 2 (callback + function) of auth flow. + + :refresh_token: Used to renew access tokens. + :access_token: Used in Spotify API calls. + :access_expires_in: How long the access token last in seconds. + + :returns: The newly created User object. + + """ + profile_response = requests.get('https://api.spotify.com/v1/me', + headers={'Authorization': "Bearer " + access_token}).json() + user_id = profile_response['id'] + + try: + user_obj = User.objects.get(id=user_id) + except User.DoesNotExist: + # Python docs recommends 32 bytes of randomness against brute + # force attacks + user_obj = User.objects.create( + id=user_id, + secret=secrets.token_urlsafe(32), + refresh_token=refresh_token, + access_token=access_token, + access_expires_in=access_expires_in, + ) + + return user_obj + +# }}} create_user # + diff --git a/login/views.py b/login/views.py index 1e0bcbe..17db2a5 100644 --- a/login/views.py +++ b/login/views.py @@ -1,13 +1,10 @@ # imports {{{ # import math -import random -import requests import os import urllib import secrets import pprint -import string from datetime import datetime from django.shortcuts import render, redirect @@ -21,28 +18,8 @@ TIME_FORMAT = '%Y-%m-%d-%H-%M-%S' TRACKS_TO_QUERY = 200 AUTH_SCOPE = ['user-library-read', 'user-read-recently-played',] -# generate_random_string {{{ # - - -def generate_random_string(length): - """Generates a random string of a certain length - - Args: - length: the desired length of the randomized string - - Returns: - A random string - """ - all_chars = string.ascii_letters + string.digits - rand_str = "".join(random.choice(all_chars) for _ in range(length)) - - return rand_str - -# }}} generate_random_string # - # index {{{ # -# Create your views here. def index(request): return render(request, 'login/index.html') @@ -73,6 +50,8 @@ def spotify_login(request): # }}} spotify_login # +# callback {{{ # + def callback(request): """ Step 2 in authorization flow: Have your application request refresh and access tokens; Spotify returns access and refresh tokens. @@ -97,38 +76,8 @@ def callback(request): token_response['expires_in']) return render(request, 'login/scan.html', get_user_context(user_obj)) - # return redirect('user/' + user_obj.secret) - -def create_user(refresh_token, access_token, access_expires_in): - """Create a User object based on information returned from Step 2 (callback - function) of auth flow. - - :refresh_token: Used to renew access tokens. - :access_token: Used in Spotify API calls. - :access_expires_in: How long the access token last in seconds. - - :returns: The newly created User object. - - """ - profile_response = requests.get('https://api.spotify.com/v1/me', - headers={'Authorization': "Bearer " + access_token}).json() - user_id = profile_response['id'] - - try: - user_obj = User.objects.get(id=user_id) - except User.DoesNotExist: - # Python docs recommends 32 bytes of randomness against brute - # force attacks - user_obj = User.objects.create( - id=user_id, - secret=secrets.token_urlsafe(32), - refresh_token=refresh_token, - access_token=access_token, - access_expires_in=access_expires_in, - ) - - return user_obj +# }}} callback # # admin_graphs {{{ #