Browse Source

Filter out unnecessary columns in history table

master
Kevin Mok 6 years ago
parent
commit
b2990b45ee
  1. 7
      api/views.py
  2. 36
      graphs/utils.py
  3. 13
      graphs/views.py
  4. 1
      requirements.txt
  5. 1
      spotifyvis/settings.py

7
api/views.py

@ -5,7 +5,6 @@ import random
import requests import requests
import urllib import urllib
import secrets import secrets
import pprint
import string import string
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
@ -16,6 +15,7 @@ from .models import *
from login.models import User from login.models import User
from login.utils import get_user_context from login.utils import get_user_context
from dateutil.parser import parse from dateutil.parser import parse
from pprint import pprint
# }}} imports # # }}} imports #
@ -138,13 +138,16 @@ def parse_history(request, user_secret):
""" """
user_obj = User.objects.get(secret=user_secret) user_obj = User.objects.get(secret=user_secret)
payload = {'limit': str(USER_TRACKS_LIMIT)}
last_time_played = History.objects.filter(user=user_obj).aggregate(Max('timestamp'))['timestamp__max'] last_time_played = History.objects.filter(user=user_obj).aggregate(Max('timestamp'))['timestamp__max']
payload = {'limit': str(USER_TRACKS_LIMIT), 'after': last_time_played.isoformat()}
if last_time_played is not None:
payload['after'] = last_time_played.isoformat()
artist_genre_queue = [] artist_genre_queue = []
user_headers = get_user_header(user_obj) user_headers = get_user_header(user_obj)
history_response = requests.get(HISTORY_ENDPOINT, history_response = requests.get(HISTORY_ENDPOINT,
headers=user_headers, headers=user_headers,
params=payload).json()['items'] params=payload).json()['items']
# pprint(history_response)
if console_logging: if console_logging:
tracks_processed = 0 tracks_processed = 0

36
graphs/utils.py

@ -2,7 +2,7 @@ import django_tables2 as tables
from pprint import pprint from pprint import pprint
from login.models import User from login.models import User
from api.models import History, Track
from api.models import History
class HistoryTable(tables.Table): class HistoryTable(tables.Table):
class Meta: class Meta:
@ -12,13 +12,6 @@ class HistoryTable(tables.Table):
track_name = tables.Column(accessor='get_track_name', orderable=False) track_name = tables.Column(accessor='get_track_name', orderable=False)
artists = tables.Column(accessor='get_artists', orderable=False) artists = tables.Column(accessor='get_artists', orderable=False)
# def render_track_name(self, record):
# return record.track.name
# return record.user
# def render_user(self, value):
# return ''
def get_secret_context(user_secret): def get_secret_context(user_secret):
"""Return user_secret in context for graph pages. """Return user_secret in context for graph pages.
@ -27,30 +20,3 @@ def get_secret_context(user_secret):
""" """
return { 'user_secret': user_secret, } return { 'user_secret': user_secret, }
def get_user_history(user_secret):
"""Return all stored history for corresponding User to user_secret.
:user_secret: User secret to get history for.
:returns: list of lists of song history plus information.
"""
user_id = get_user_id_from_secret(user_secret)
history_fields = [field.name for field in History._meta.get_fields()]
# don't need ordering bc. django-tables2?
user_history = History.objects.filter(user__exact=user_id).order_by('-timestamp')
# 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

13
graphs/views.py

@ -13,6 +13,7 @@ from datetime import datetime
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from .utils import * from .utils import *
from django_tables2 import RequestConfig from django_tables2 import RequestConfig
from api.models import History
# }}} imports # # }}} imports #
@ -49,8 +50,14 @@ def display_history_table(request, user_secret):
:param user_secret: user secret used for identification :param user_secret: user secret used for identification
:return: renders the user history page :return: renders the user history page
""" """
context = get_secret_context(user_secret)
context.update(get_user_history(user_secret))
RequestConfig(request).configure(context['user_history_table'])
user_id = User.objects.get(secret=user_secret).id
user_history = History.objects.filter(user__exact=user_id).order_by('-timestamp')
history_table = HistoryTable(user_history)
history_table.exclude = ('id', 'user', 'track', )
RequestConfig(request).configure(history_table)
context = { 'user_history_table': history_table,
'user_id': user_id, }
return render(request, "graphs/user_history.html", context) return render(request, "graphs/user_history.html", context)

1
requirements.txt

@ -2,6 +2,7 @@ astroid==1.6.3
certifi==2018.4.16 certifi==2018.4.16
chardet==3.0.4 chardet==3.0.4
Django==2.0.5 Django==2.0.5
django-filter==2.0
djangorestframework==3.8.2 djangorestframework==3.8.2
django-tables2==2.0.2 django-tables2==2.0.2
idna==2.6 idna==2.6

1
spotifyvis/settings.py

@ -41,6 +41,7 @@ INSTALLED_APPS = [
'api.apps.ApiConfig', 'api.apps.ApiConfig',
'graphs.apps.GraphsConfig', 'graphs.apps.GraphsConfig',
'django_tables2', 'django_tables2',
'django_filters',
] ]
MIDDLEWARE = [ MIDDLEWARE = [

Loading…
Cancel
Save