diff --git a/api/models.py b/api/models.py
index f660b3b..be099d0 100644
--- a/api/models.py
+++ b/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)
diff --git a/graphs/templates/graphs/user_history.html b/graphs/templates/graphs/user_history.html
index 7d2cd25..8d41a06 100644
--- a/graphs/templates/graphs/user_history.html
+++ b/graphs/templates/graphs/user_history.html
@@ -1,6 +1,6 @@
{% load static %}
-{% load render_table from django_tables2 %}
+{% load render_table export_url from django_tables2 %}
@@ -10,7 +10,7 @@
{{ user_id }}'s Listening History
- Export CSV
+ Export CSV
{% render_table user_history_table %}
diff --git a/graphs/urls.py b/graphs/urls.py
index 7f985e1..6ff4da5 100644
--- a/graphs/urls.py
+++ b/graphs/urls.py
@@ -12,5 +12,6 @@ urlpatterns = [
name='display_audio_features'),
# path('history/', 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'),
]
diff --git a/graphs/utils.py b/graphs/utils.py
index 62756bb..69e262d 100644
--- a/graphs/utils.py
+++ b/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):
diff --git a/graphs/views.py b/graphs/views.py
index 85fa94c..37b1c5c 100644
--- a/graphs/views.py
+++ b/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))
diff --git a/requirements.txt b/requirements.txt
index 936771a..23befcc 100644
--- a/requirements.txt
+++ b/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