Add CSV download option

master
Tait Hoyem 3 years ago
parent e94311b345
commit ce1faae46a

@ -1,6 +1,8 @@
{% extends 'common/master.html' %}
{% block body %}
<h1>View Download Attempts</h1>
<a href="{% url 'view_downloads_csv' %}">Download as CSV</a>
<a href="{% url 'view_all_downloads_csv' %}">Download all as CSV</a>
<table>
<tr>
<th>Email</th>

@ -3,4 +3,6 @@ from . import views
urlpatterns = [
path('', views.index, name='requests_view'),
path('csv/', views.csv, name='view_downloads_csv'),
path('all/csv/', views.csv_all, name='view_all_downloads_csv'),
]

@ -1,9 +1,38 @@
from django.shortcuts import render, HttpResponse
from django.shortcuts import render, HttpResponse, redirect
from django.urls import reverse_lazy
from download.models import DownloadAttempt
from datetime import datetime
# Create your views here.
def index(request):
if not request.user.is_authenticated:
return redirect(reverse_lazy('login'))
# TODO: Allow search
return render(request, 'viewrequests/requests.html', {
'requests': DownloadAttempt.objects.all().order_by('-timestamp')[:100]
'requests': DownloadAttempt.objects.all().order_by('-timestamp')[:10]
})
def csv_resp(max_items=-1):
header = "Email,Address,IP,Geolocation,Timestamp,Valid Code,Email Sent\n"
body = ""
if max_items == -1:
das = DownloadAttempt.objects.all().order_by('-timestamp')
else:
das = DownloadAttempt.objects.all().order_by('-timestamp')[:max_items]
for dla in das:
body += dla.user.email + ',' + dla.pdf.address.address + ',' + dla.ip + ',"' + dla.geolocation + '",' + dla.timestamp.strftime('%Y/%m/%d %H:%M:%S') + ',' + str(dla.code_correct) + ',' + str(dla.email_sent) + "\n"
resp = HttpResponse(header + body, content_type='text/csv')
resp['Content-Disposition'] = "attachment; filename=download_attempts.csv"
return resp
def csv(request):
if not request.user.is_authenticated:
return redirect(reverse_lazy('login'))
return csv_resp(100)
def csv_all(request):
if not request.user.is_authenticated:
return redirect(reverse_lazy('login'))
return csv_resp()
Loading…
Cancel
Save