Graphs and tables for your Spotify account.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.5 KiB

  1. # imports {{{ #
  2. import math
  3. import random
  4. import requests
  5. import os
  6. import urllib
  7. import secrets
  8. from pprint import pprint
  9. import string
  10. from datetime import datetime
  11. from django.shortcuts import render, redirect
  12. from .utils import *
  13. from django_tables2 import RequestConfig, SingleTableView
  14. from django_tables2.export.views import ExportMixin
  15. from django_tables2.export import TableExport
  16. from api.models import History
  17. # }}} imports #
  18. def display_artist_graph(request, user_secret):
  19. """Renders the artist data graph display page
  20. :param request: the HTTP request
  21. :param user_secret: the user secret used for identification
  22. :return: render the artist data graph display page
  23. """
  24. return render(request, "graphs/artist_graph.html",
  25. get_secret_context(user_secret))
  26. def display_genre_graph(request, user_secret):
  27. return render(request, "graphs/genre_graph.html",
  28. get_secret_context(user_secret))
  29. def display_features_graphs(request, user_secret):
  30. """Renders the audio features page
  31. :param request: the HTTP request
  32. :param user_secret: user secret used for identification
  33. :return: renders the audio features page
  34. """
  35. return render(request, "graphs/features_graphs.html",
  36. get_secret_context(user_secret))
  37. class HistoryList(ExportMixin, SingleTableView):
  38. """Create table with list of song history."""
  39. model = History
  40. table_class = HistoryTable
  41. context_table_name = 'user_history_table'
  42. template_name = 'graphs/user_history.html'
  43. def get_table_kwargs(self):
  44. return { 'exclude': ('id', 'user', 'track', 'track_id', 'iso_timestamp', ) }
  45. def get_table_data(self):
  46. return History.objects.filter(user__exact=self.request.session['user_id']).order_by('-timestamp')
  47. def get_context_data(self, **kwargs):
  48. context = super().get_context_data(**kwargs)
  49. context['user_id'] = self.request.session['user_id']
  50. return context
  51. def get_export_filename(self, export_format):
  52. return "{}.{}".format(self.request.session['user_id'], export_format)
  53. def create_export(self, export_format):
  54. export_exclude = ('id', 'user', 'track', 'track_name', 'artists',
  55. 'timestamp', )
  56. exporter = TableExport(
  57. export_format=export_format,
  58. table=self.get_table(exclude=export_exclude),
  59. exclude_columns=self.exclude_columns,
  60. )
  61. return exporter.response(filename=self.get_export_filename(export_format))