From 016d5299419213d2759872832ed1073cc887912f Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 28 Apr 2019 15:05:02 +0200 Subject: [PATCH] Added general stats --- counter/templates/index.html | 17 ++++++++++++++++- counter/templates/menu.html | 2 ++ counter/views.py | 31 ++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/counter/templates/index.html b/counter/templates/index.html index e6dbb5e..da41d17 100644 --- a/counter/templates/index.html +++ b/counter/templates/index.html @@ -1,7 +1,22 @@ {% extends 'base.html' %} +{# vim: set ts=2 sw=2 et tw=80: #} {% block title %}Home{% endblock %} {% block content %}

Arrow counter

-

A simple online arrow counter

+
A simple online arrow counter
+{% if user.is_authenticated %} +
+
Arrows shot this year:
+

{{ yearArrows }}

+
+
+
Arrows shot this month:
+

{{ monthArrows }}

+
+
+
Arrows shot this week:
+

{{ weekArrows }}

+
+{% endif %} {% endblock %} diff --git a/counter/templates/menu.html b/counter/templates/menu.html index 2501658..a619a45 100644 --- a/counter/templates/menu.html +++ b/counter/templates/menu.html @@ -10,6 +10,8 @@ {% endif %} +
  • Home
  • +
  • {% if user.is_authenticated %}
  • Counts
  • Weekly stats
  • diff --git a/counter/views.py b/counter/views.py index 04c5cce..0019a36 100644 --- a/counter/views.py +++ b/counter/views.py @@ -1,3 +1,5 @@ +# vim: set ts=4 sw=4 et tw=80: + from django.shortcuts import render from django.http import HttpResponse, JsonResponse from django.views import generic @@ -24,7 +26,30 @@ def tofirstdayinisoweek(year, week): def index(request): 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): template = loader.get_template('stats.html') @@ -37,7 +62,7 @@ def count_stats(request): .values('isoyear', 'week') \ .annotate(sum_count=Sum('count')) \ .order_by('-isoyear', '-week') - + for w in weeklyArrows: w['weekStarts'] = tofirstdayinisoweek(w['isoyear'], w['week']) w['weekEnds'] = w['weekStarts'] + timedelta(days=6) @@ -94,7 +119,7 @@ class EditArrowCount(generic.UpdateView): super().get(self, request, *args, **kwargs) context_data = self.get_context_data() 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): model = ArrowCount