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.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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'),
|
||||
]
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
: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)
|
||||
|
||||
context = { 'user_history_table': history_table,
|
||||
'user_id': user_id, }
|
||||
|
||||
return render(request, "graphs/user_history.html", context)
|
||||
|
||||
class HistoryList(SingleTableView):
|
||||
class HistoryList(ExportMixin, SingleTableView):
|
||||
"""Create table with list of song history."""
|
||||
model = History
|
||||
table_class = HistoryTable
|
||||
# table_data =
|
||||
context_table_name = 'user_history_table'
|
||||
template_name = 'graphs/user_history.html'
|
||||
|
||||
def get_table_kwargs(self):
|
||||
return { 'exclude': ('id', 'user', 'track', 'track_id', ) }
|
||||
|
||||
def get_table_data(self):
|
||||
return History.objects.filter(user__exact=self.request.session['user_id']).order_by('-timestamp')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['user_id'] = self.request.session['user_id']
|
||||
return context
|
||||
|
||||
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))
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user