Browse Source

Able to upload history onto server (#57)

master
Kevin Mok 6 years ago
parent
commit
a4a00458af
  1. 1
      .gitignore
  2. 2
      graphs/templates/graphs/user_history.html
  3. 9
      graphs/views.py
  4. 8
      login/forms.py
  5. 5
      login/models.py
  6. 5
      login/templates/login/scan.html
  7. 1
      login/urls.py
  8. 21
      login/views.py
  9. 3
      spotifyvis/settings.py

1
.gitignore

@ -9,3 +9,4 @@ api-keys.sh
Pipfile
*.txt
scrap.py
media/history/*

2
graphs/templates/graphs/user_history.html

@ -10,7 +10,7 @@
</head>
<body>
<h1>{{ user_id }}'s Listening History</h1>
<a class="btn btn-primary " href="{% export_url 'csv' %}" role="button">Export CSV</a>
<a class="btn btn-primary " href="{% export_url 'csv' %}" role="button">Export</a>
{% render_table user_history_table %}
</body>
</html>

9
graphs/views.py

@ -45,6 +45,8 @@ def display_features_graphs(request, user_secret):
return render(request, "graphs/features_graphs.html",
get_secret_context(user_secret))
# HistoryList {{{ #
class HistoryList(ExportMixin, SingleTableView):
"""Create table with list of song history."""
model = History
@ -52,6 +54,8 @@ class HistoryList(ExportMixin, SingleTableView):
context_table_name = 'user_history_table'
template_name = 'graphs/user_history.html'
# overridden methods {{{ #
def get_table_kwargs(self):
return { 'exclude': ('id', 'user', 'track', 'track_id', 'iso_timestamp', ) }
@ -76,3 +80,8 @@ class HistoryList(ExportMixin, SingleTableView):
)
return exporter.response(filename=self.get_export_filename(export_format))
# }}} overridden methods #
# }}} HistoryList #

8
login/forms.py

@ -0,0 +1,8 @@
from django import forms
from .models import HistoryUpload
class HistoryUploadForm(forms.ModelForm):
class Meta:
model = HistoryUpload
fields = ('user_id', 'document', )
# widgets = { 'user_id': forms.HiddenInput() }

5
login/models.py

@ -20,3 +20,8 @@ class User(models.Model):
def __str__(self):
return self.id
class HistoryUpload(models.Model):
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
document = models.FileField(upload_to='history/')
uploaded_at = models.DateTimeField(auto_now_add=True)

5
login/templates/login/scan.html

@ -24,5 +24,10 @@
<a href="{% url "api:scan_history" user_secret %}" class="btn btn-primary">
Scan History
</a>
<form action="{% url 'login:upload_history' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input class="btn btn-primary" type="submit" value="Import History">
</form>
</body>
</html>

1
login/urls.py

@ -9,4 +9,5 @@ urlpatterns = [
path('callback', callback, name='callback'),
# path('user/<str:user_secret>', user_home, name='user_home'),
path('admin_graphs', admin_graphs, name='admin_graphs'),
path('upload_history', upload_history, name='upload_history'),
]

21
login/views.py

@ -11,6 +11,7 @@ from django.shortcuts import render, redirect
from django.http import HttpResponseBadRequest
from .models import *
from .utils import *
from .forms import HistoryUploadForm
# }}} imports #
@ -78,7 +79,10 @@ def callback(request):
request.session['user_id'] = user_obj.id
request.session['user_secret'] = user_obj.secret
return render(request, 'login/scan.html', get_user_context(user_obj))
context = get_user_context(user_obj)
context['form'] = HistoryUploadForm()
return render(request, 'login/scan.html', context)
# }}} callback #
@ -97,3 +101,18 @@ def admin_graphs(request):
return render(request, 'graphs/logged_in.html', get_user_context(user_obj))
# }}} admin_graphs #
def upload_history(request):
if request.method == 'POST':
form = HistoryUploadForm(request.POST, request.FILES)
form.fields['user_id'].initial = User.objects.get(id=request.session['user_id'])
if form.is_valid():
form.save()
# Redirect to the document list after POST
return redirect('graphs:display_history_table')
else:
form = HistoryUploadForm()
# return redirect('graphs:display_history_table')
return render(request, 'login/scan.html', context)

3
spotifyvis/settings.py

@ -130,3 +130,6 @@ STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Loading…
Cancel
Save