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.

87 lines
2.6 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. # HistoryList {{{ #
  38. class HistoryList(ExportMixin, SingleTableView):
  39. """Create table with list of song history."""
  40. model = History
  41. table_class = HistoryTable
  42. context_table_name = 'user_history_table'
  43. template_name = 'graphs/user_history.html'
  44. # overridden methods {{{ #
  45. def get_table_kwargs(self):
  46. return { 'exclude': ('id', 'user', 'track', 'track_id', 'iso_timestamp', ) }
  47. def get_table_data(self):
  48. return History.objects.filter(user__exact=self.request.session['user_id']).order_by('-timestamp')
  49. def get_context_data(self, **kwargs):
  50. context = super().get_context_data(**kwargs)
  51. context['user_id'] = self.request.session['user_id']
  52. return context
  53. def get_export_filename(self, export_format):
  54. return "{}.{}".format(self.request.session['user_id'], export_format)
  55. def create_export(self, export_format):
  56. export_exclude = ('id', 'user', 'track', 'track_name', 'artists',
  57. 'timestamp', )
  58. exporter = TableExport(
  59. export_format=export_format,
  60. table=self.get_table(exclude=export_exclude),
  61. exclude_columns=self.exclude_columns,
  62. )
  63. return exporter.response(filename=self.get_export_filename(export_format))
  64. # }}} overridden methods #
  65. # }}} HistoryList #