Browse Source

Export listening history to csv (#57)

Still need to convert timestamps to more CPU-readable format and figure
out what to do about reading headers for importing.
master
Kevin Mok 6 years ago
parent
commit
fa2c5f7008
  1. 3
      api/models.py
  2. 4
      graphs/templates/graphs/user_history.html
  3. 3
      graphs/urls.py
  4. 1
      graphs/utils.py
  5. 46
      graphs/views.py
  6. 1
      requirements.txt

3
api/models.py

@ -106,6 +106,9 @@ class History(models.Model):
def get_track_name(self):
return self.track.name
def get_track_id(self):
return self.track.id
def get_artists(self):
artist_names = [artist.name for artist in self.track.artists.all()]
return ', '.join(artist_names)

4
graphs/templates/graphs/user_history.html

@ -1,6 +1,6 @@
<!DOCTYPE html>
{% load static %}
{% load render_table from django_tables2 %}
{% load render_table export_url from django_tables2 %}
<html lang="en">
<head>
<meta charset="UTF-8">
@ -10,7 +10,7 @@
</head>
<body>
<h1>{{ user_id }}'s Listening History</h1>
<a class="btn btn-primary disabled" href="" role="button">Export CSV</a>
<a class="btn btn-primary " href="{% export_url 'csv' %}" role="button">Export CSV</a>
{% render_table user_history_table %}
</body>
</html>

3
graphs/urls.py

@ -12,5 +12,6 @@ urlpatterns = [
name='display_audio_features'),
# path('history/<str:user_secret>', display_history_table,
# name='display_history_table'),
path('history/', display_history_table, name='display_history_table'),
# path('history/', display_history_table, name='display_history_table'),
path('history/', HistoryList.as_view(), name='display_history_table'),
]

1
graphs/utils.py

@ -10,6 +10,7 @@ class HistoryTable(tables.Table):
template_name = 'django_tables2/bootstrap.html'
track_name = tables.Column(accessor='get_track_name', orderable=False)
track_id = tables.Column(accessor='get_track_id', orderable=False)
artists = tables.Column(accessor='get_artists', orderable=False)
def get_secret_context(user_secret):

46
graphs/views.py

@ -13,6 +13,8 @@ from datetime import datetime
from django.shortcuts import render, redirect
from .utils import *
from django_tables2 import RequestConfig, SingleTableView
from django_tables2.export.views import ExportMixin
from django_tables2.export import TableExport
from api.models import History
# }}} imports #
@ -43,27 +45,33 @@ def display_features_graphs(request, user_secret):
return render(request, "graphs/features_graphs.html",
get_secret_context(user_secret))
def display_history_table(request):
"""Renders the user history page
class HistoryList(ExportMixin, SingleTableView):
"""Create table with list of song history."""
model = History
table_class = HistoryTable
context_table_name = 'user_history_table'
template_name = 'graphs/user_history.html'
:param request: the HTTP request
:param user_secret: user secret used for identification
:return: renders the user history page
"""
user_id = request.session['user_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)
def get_table_kwargs(self):
return { 'exclude': ('id', 'user', 'track', 'track_id', ) }
context = { 'user_history_table': history_table,
'user_id': user_id, }
def get_table_data(self):
return History.objects.filter(user__exact=self.request.session['user_id']).order_by('-timestamp')
return render(request, "graphs/user_history.html", context)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['user_id'] = self.request.session['user_id']
return context
class HistoryList(SingleTableView):
"""Create table with list of song history."""
model = History
table_class = HistoryTable
# table_data =
def get_export_filename(self, export_format):
return "{}.{}".format(self.request.session['user_id'], export_format)
def create_export(self, export_format):
export_exclude = ('id', 'user', 'track', 'track_name', 'artists', )
exporter = TableExport(
export_format=export_format,
table=self.get_table(exclude=export_exclude),
exclude_columns=self.exclude_columns,
)
return exporter.response(filename=self.get_export_filename(export_format))

1
requirements.txt

@ -15,5 +15,6 @@ python-dateutil==2.7.5
pytz==2018.4
requests==2.18.4
six==1.11.0
tablib==0.12.1
urllib3==1.22
wrapt==1.10.11
Loading…
Cancel
Save