Added excel export

This commit is contained in:
Claudio Maggioni (maggicl) 2019-04-28 15:51:25 +02:00
parent 016d529941
commit 8e9d185293
4 changed files with 35 additions and 1 deletions

View file

@ -4,6 +4,11 @@
{% block content %}
<h1 class="center">Counts</h1>
<p class="center">
<a class="btn waves-effect waves-light" href="{% url "count_export" %}" style="margin-bottom: 3em">
Excel Data (CSV) <i class="material-icons right">file_download</i>
</a>
</p>
{% include 'pagination.html' %}
<table class="centered">
<thead>

View file

@ -4,7 +4,13 @@
{% block title %}Home{% endblock %}
{% block content %}
<h1 class="center">Arrow counter</h1>
<h5 class="center" style="margin-bottom: 3rem">A simple online arrow counter</h5>
<h5 class="center">A simple online arrow counter</h5>
<p class="center" style="margin-bottom: 3rem">
<a class="btn waves-effect waves-light" href="{% url "count_export" %}">
Excel Data (CSV) <i class="material-icons right">file_download</i>
</a>
</p>
{% if user.is_authenticated %}
<div class="card-panel grey lighten-2">
<h5 class="center">Arrows shot this year:</h5>

View file

@ -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'),

View file

@ -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')