Setup django_tables2 for user history table

This commit is contained in:
2018-11-06 15:23:43 -05:00
parent b4ffddb24d
commit 2912977992
8 changed files with 53 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
<!DOCTYPE html>
{% load static %}
{% load render_table from django_tables2 %}
<html lang="en">
<head>
<meta charset="UTF-8">
@@ -8,6 +9,7 @@
<link rel="stylesheet" href="{% static 'css/dark_bg.css' %}">
</head>
<body>
<h1>Your Listening History</h1>
<h1>{{ user_id }}'s Listening History</h1>
{% render_table user_history_table %}
</body>
</html>

View File

@@ -1,3 +1,14 @@
import django_tables2 as tables
from pprint import pprint
from login.models import User
from api.models import History
class HistoryTable(tables.Table):
class Meta:
model = History
template_name = 'django_tables2/bootstrap.html'
def get_secret_context(user_secret):
"""Return user_secret in context for graph pages.
@@ -15,6 +26,19 @@ def get_user_history(user_secret):
:returns: list of lists of song history plus information.
"""
# TODO: pass back user id as well?
pass
user_id = get_user_id_from_secret(user_secret)
history_fields = [field.name for field in History._meta.get_fields()]
user_history = History.objects.filter(user__exact=user_id).order_by('-timestamp')
user_history_table = HistoryTable(user_history)
return { 'user_id': user_id,
'history_fields': history_fields,
'user_history_table': user_history_table, }
def get_user_id_from_secret(user_secret):
"""Retrieve corresponding user_id for user_secret.
:user_secret:
:returns: user_id
"""
return User.objects.get(secret=user_secret).id

View File

@@ -6,12 +6,13 @@ import requests
import os
import urllib
import secrets
import pprint
from pprint import pprint
import string
from datetime import datetime
from django.shortcuts import render, redirect
from .utils import *
from django_tables2 import RequestConfig
# }}} imports #
@@ -48,6 +49,8 @@ def display_history_table(request, user_secret):
:param user_secret: user secret used for identification
:return: renders the user history page
"""
return render(request, "graphs/user_history.html",
get_secret_context(user_secret))
context = get_secret_context(user_secret)
context.update(get_user_history(user_secret))
RequestConfig(request).configure(context['user_history_table'])
return render(request, "graphs/user_history.html", context)