From 8e9d185293f40683ebc164734bb0df1161fe98c5 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 28 Apr 2019 15:51:25 +0200 Subject: [PATCH] Added excel export --- counter/templates/counter/list.html | 5 +++++ counter/templates/index.html | 8 +++++++- counter/urls.py | 1 + counter/views.py | 22 ++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/counter/templates/counter/list.html b/counter/templates/counter/list.html index 93ebe33..587552c 100644 --- a/counter/templates/counter/list.html +++ b/counter/templates/counter/list.html @@ -4,6 +4,11 @@ {% block content %}

Counts

+

+ + Excel Data (CSV) file_download + +

{% include 'pagination.html' %} diff --git a/counter/templates/index.html b/counter/templates/index.html index da41d17..39ae609 100644 --- a/counter/templates/index.html +++ b/counter/templates/index.html @@ -4,7 +4,13 @@ {% block title %}Home{% endblock %} {% block content %}

Arrow counter

-
A simple online arrow counter
+
A simple online arrow counter
+

+ + Excel Data (CSV) file_download + +

+ {% if user.is_authenticated %}
Arrows shot this year:
diff --git a/counter/urls.py b/counter/urls.py index dc284f8..2f1b372 100644 --- a/counter/urls.py +++ b/counter/urls.py @@ -5,6 +5,7 @@ from . import views urlpatterns = [ path('', views.index, name='index'), path('stats', views.count_stats, name='stats'), + path('count/export', views.arrow_count_export, name='count_export'), path('count/list', views.arrow_count_list, name='count_list'), path('count/new', login_required(views.NewArrowCount.as_view()), name='count_new'), diff --git a/counter/views.py b/counter/views.py index 0019a36..080d623 100644 --- a/counter/views.py +++ b/counter/views.py @@ -16,6 +16,8 @@ from django.db.models import Sum from datetime import datetime, timedelta, date import json from django.core.serializers.json import DjangoJSONEncoder +import csv +from django.utils.encoding import smart_str # https://stackoverflow.com/questions/5882405 def tofirstdayinisoweek(year, week): @@ -51,6 +53,7 @@ def index(request): else: return HttpResponse(template.render({}), request) +@login_required def count_stats(request): template = loader.get_template('stats.html') @@ -71,6 +74,25 @@ def count_stats(request): 'weeklyArrows': json.dumps(list(weeklyArrows), cls=DjangoJSONEncoder) }, request)) +@login_required +def arrow_count_export(request): + counts = ArrowCount.objects.filter(user=request.user).order_by('-date').all() + response = HttpResponse(content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename=counts-' \ + + datetime.today().strftime('%Y-%m-%d') + '.csv' + writer = csv.writer(response, csv.excel, delimiter=';') + response.write(u'\ufeff'.encode('utf8')) + writer.writerow([ + smart_str(u"Date"), + smart_str(u"Count") + ]) + for row in counts: + writer.writerow([ + smart_str(row.date.strftime('%Y-%m-%d')), + smart_str(row.count) + ]) + return response + @login_required def arrow_count_list(request): page = request.GET.get('page')