Added general stats

This commit is contained in:
Claudio Maggioni (maggicl) 2019-04-28 15:05:02 +02:00
parent 093c491cde
commit 016d529941
3 changed files with 46 additions and 4 deletions

View file

@ -1,7 +1,22 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{# vim: set ts=2 sw=2 et tw=80: #}
{% block title %}Home{% endblock %} {% block title %}Home{% endblock %}
{% block content %} {% block content %}
<h1 class="center">Arrow counter</h1> <h1 class="center">Arrow counter</h1>
<h4 class="center">A simple online arrow counter</h4> <h5 class="center" style="margin-bottom: 3rem">A simple online arrow counter</h5>
{% if user.is_authenticated %}
<div class="card-panel grey lighten-2">
<h5 class="center">Arrows shot this year:</h5>
<p class="center"><strong style="font-size: 2.7rem">{{ yearArrows }}</strong></p>
</div>
<div class="card-panel grey lighten-2">
<h5 class="center">Arrows shot this month:</h5>
<p class="center"><strong style="font-size: 2.7rem">{{ monthArrows }}</strong></p>
</div>
<div class="card-panel grey lighten-2">
<h5 class="center">Arrows shot this week:</h5>
<p class="center"><strong style="font-size: 2.7rem">{{ weekArrows }}</strong></p>
</div>
{% endif %}
{% endblock %} {% endblock %}

View file

@ -10,6 +10,8 @@
</div> </div>
{% endif %} {% endif %}
<li><a href="{% url "index" %}">Home</a></li>
<li class="divider" tabindex="-1"></li>
{% if user.is_authenticated %} {% if user.is_authenticated %}
<li><a href="{% url "count_list" %}">Counts</a></li> <li><a href="{% url "count_list" %}">Counts</a></li>
<li><a href="{% url "stats" %}">Weekly stats</a></li> <li><a href="{% url "stats" %}">Weekly stats</a></li>

View file

@ -1,3 +1,5 @@
# vim: set ts=4 sw=4 et tw=80:
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse, JsonResponse from django.http import HttpResponse, JsonResponse
from django.views import generic from django.views import generic
@ -24,7 +26,30 @@ def tofirstdayinisoweek(year, week):
def index(request): def index(request):
template = loader.get_template('index.html') template = loader.get_template('index.html')
return HttpResponse(template.render({}, request))
if request.user.is_authenticated:
now = datetime.today()
yearArrows = ArrowCount.objects.filter(user=request.user) \
.filter(date__range=(date(now.year, 1, 1), date(now.year, 12, 31))) \
.aggregate(s=Sum('count'))
monthArrows = ArrowCount.objects.filter(user=request.user) \
.filter(date__year=now.year, date__month=now.month) \
.aggregate(s=Sum('count'))
weekday = now.isoweekday()
weekArrows = ArrowCount.objects.filter(user=request.user) \
.filter(date__range=(now - timedelta(days=weekday), now + timedelta(days=6-weekday))) \
.aggregate(s=Sum('count'))
return HttpResponse(template.render({
'yearArrows': 0 if yearArrows['s'] is None else yearArrows['s'],
'monthArrows': 0 if monthArrows['s'] is None else monthArrows['s'],
'weekArrows': 0 if weekArrows['s'] is None else weekArrows['s']
}, request))
else:
return HttpResponse(template.render({}), request)
def count_stats(request): def count_stats(request):
template = loader.get_template('stats.html') template = loader.get_template('stats.html')
@ -37,7 +62,7 @@ def count_stats(request):
.values('isoyear', 'week') \ .values('isoyear', 'week') \
.annotate(sum_count=Sum('count')) \ .annotate(sum_count=Sum('count')) \
.order_by('-isoyear', '-week') .order_by('-isoyear', '-week')
for w in weeklyArrows: for w in weeklyArrows:
w['weekStarts'] = tofirstdayinisoweek(w['isoyear'], w['week']) w['weekStarts'] = tofirstdayinisoweek(w['isoyear'], w['week'])
w['weekEnds'] = w['weekStarts'] + timedelta(days=6) w['weekEnds'] = w['weekStarts'] + timedelta(days=6)
@ -94,7 +119,7 @@ class EditArrowCount(generic.UpdateView):
super().get(self, request, *args, **kwargs) super().get(self, request, *args, **kwargs)
context_data = self.get_context_data() context_data = self.get_context_data()
context_data.update(ac_id=self.kwargs['id']) context_data.update(ac_id=self.kwargs['id'])
return self.render_to_response(context_data) return self.render_to_response(context_data)
class DeleteArrowCount(generic.DeleteView): class DeleteArrowCount(generic.DeleteView):
model = ArrowCount model = ArrowCount