diff --git a/viewdownloads/__pycache__/urls.cpython-38.pyc b/viewdownloads/__pycache__/urls.cpython-38.pyc
index bb2075c..3d3d3c7 100644
Binary files a/viewdownloads/__pycache__/urls.cpython-38.pyc and b/viewdownloads/__pycache__/urls.cpython-38.pyc differ
diff --git a/viewdownloads/__pycache__/views.cpython-38.pyc b/viewdownloads/__pycache__/views.cpython-38.pyc
index 6d0de35..e419d1f 100644
Binary files a/viewdownloads/__pycache__/views.cpython-38.pyc and b/viewdownloads/__pycache__/views.cpython-38.pyc differ
diff --git a/viewdownloads/templates/viewrequests/requests.html b/viewdownloads/templates/viewrequests/requests.html
index fffb2ff..d4ee552 100644
--- a/viewdownloads/templates/viewrequests/requests.html
+++ b/viewdownloads/templates/viewrequests/requests.html
@@ -1,6 +1,8 @@
{% extends 'common/master.html' %}
{% block body %}
View Download Attempts
+Download as CSV
+Download all as CSV
Email |
diff --git a/viewdownloads/urls.py b/viewdownloads/urls.py
index bd6956d..1e41e78 100644
--- a/viewdownloads/urls.py
+++ b/viewdownloads/urls.py
@@ -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'),
]
\ No newline at end of file
diff --git a/viewdownloads/views.py b/viewdownloads/views.py
index 51dfb09..e72c881 100644
--- a/viewdownloads/views.py
+++ b/viewdownloads/views.py
@@ -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()
\ No newline at end of file