diff --git a/api/models.py b/api/models.py index 0b9b337..08fdcd8 100644 --- a/api/models.py +++ b/api/models.py @@ -102,7 +102,7 @@ class History(models.Model): track = models.ForeignKey(Track, on_delete=models.CASCADE) def __str__(self): - return (self.user, self.time, self.track) + return " - ".join((str(self.user), str(self.timestamp), str(self.track))) # }}} # diff --git a/api/views.py b/api/views.py index 94d1c13..3e16ab7 100644 --- a/api/views.py +++ b/api/views.py @@ -19,6 +19,8 @@ from dateutil.parser import parse # }}} imports # +# constants {{{ # + USER_TRACKS_LIMIT = 50 HISTORY_LIMIT = 50 ARTIST_LIMIT = 50 @@ -31,6 +33,8 @@ HISTORY_ENDPOINT = 'https://api.spotify.com/v1/me/player/recently-played' console_logging = True # console_logging = False +# }}} constants # + # parse_library {{{ # def parse_library(request, user_secret): diff --git a/graphs/templates/graphs/user_history.html b/graphs/templates/graphs/user_history.html index f07e954..6086518 100644 --- a/graphs/templates/graphs/user_history.html +++ b/graphs/templates/graphs/user_history.html @@ -1,5 +1,6 @@ {% load static %} +{% load render_table from django_tables2 %} @@ -8,6 +9,7 @@ -

Your Listening History

+

{{ user_id }}'s Listening History

+ {% render_table user_history_table %} diff --git a/graphs/utils.py b/graphs/utils.py index e866585..98c90fd 100644 --- a/graphs/utils.py +++ b/graphs/utils.py @@ -1,3 +1,14 @@ +import django_tables2 as tables + +from pprint import pprint +from login.models import User +from api.models import History + +class HistoryTable(tables.Table): + class Meta: + model = History + template_name = 'django_tables2/bootstrap.html' + def get_secret_context(user_secret): """Return user_secret in context for graph pages. @@ -15,6 +26,19 @@ def get_user_history(user_secret): :returns: list of lists of song history plus information. """ - # TODO: pass back user id as well? - pass + user_id = get_user_id_from_secret(user_secret) + history_fields = [field.name for field in History._meta.get_fields()] + user_history = History.objects.filter(user__exact=user_id).order_by('-timestamp') + user_history_table = HistoryTable(user_history) + return { 'user_id': user_id, + 'history_fields': history_fields, + 'user_history_table': user_history_table, } +def get_user_id_from_secret(user_secret): + """Retrieve corresponding user_id for user_secret. + + :user_secret: + :returns: user_id + + """ + return User.objects.get(secret=user_secret).id diff --git a/graphs/views.py b/graphs/views.py index f4d6b44..0415363 100644 --- a/graphs/views.py +++ b/graphs/views.py @@ -6,12 +6,13 @@ import requests import os import urllib import secrets -import pprint +from pprint import pprint import string from datetime import datetime from django.shortcuts import render, redirect from .utils import * +from django_tables2 import RequestConfig # }}} imports # @@ -48,6 +49,8 @@ def display_history_table(request, user_secret): :param user_secret: user secret used for identification :return: renders the user history page """ - return render(request, "graphs/user_history.html", - get_secret_context(user_secret)) + context = get_secret_context(user_secret) + context.update(get_user_history(user_secret)) + RequestConfig(request).configure(context['user_history_table']) + return render(request, "graphs/user_history.html", context) diff --git a/requirements.txt b/requirements.txt index 04ea69f..6bde00c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,15 +3,16 @@ certifi==2018.4.16 chardet==3.0.4 Django==2.0.5 djangorestframework==3.8.2 +django-tables2==2.0.2 idna==2.6 isort==4.3.4 lazy-object-proxy==1.3.1 mccabe==0.6.1 psycopg2-binary==2.7.4 pylint==1.8.4 +python-dateutil==2.7.5 pytz==2018.4 requests==2.18.4 six==1.11.0 urllib3==1.22 wrapt==1.10.11 -python-dateutil==2.7.5 diff --git a/spotifyvis/settings.py b/spotifyvis/settings.py index ffbffaa..d91d2ea 100644 --- a/spotifyvis/settings.py +++ b/spotifyvis/settings.py @@ -40,6 +40,7 @@ INSTALLED_APPS = [ 'login.apps.LoginConfig', 'api.apps.ApiConfig', 'graphs.apps.GraphsConfig', + 'django_tables2', ] MIDDLEWARE = [ diff --git a/static/css/dark_bg.css b/static/css/dark_bg.css index a472959..52af531 100644 --- a/static/css/dark_bg.css +++ b/static/css/dark_bg.css @@ -1,8 +1,15 @@ body { -background-color: #1e1e1e; + /* dark grey */ + background-color: #1e1e1e; } -h1,p { -color: grey; +h1 { + /* light grey */ + color: #e5e5e5; +} + +p,td { + /* light-dark grey */ + color: #b2b2b2; }