From 6abcfa9732ddf2221e4edb0f276067c4283e8796 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 1 Jun 2021 13:37:25 +0200 Subject: [PATCH] View top stats. --- src/stats/forms.py | 6 ++++++ src/stats/templates/stats/top.html | 32 ++++++++++++++++++++++++++++++ src/stats/urls.py | 7 +++++++ src/stats/views.py | 29 +++++++++++++++++++++++++++ src/wolnelektury/urls.py | 1 + 5 files changed, 75 insertions(+) create mode 100644 src/stats/forms.py create mode 100644 src/stats/templates/stats/top.html create mode 100644 src/stats/urls.py create mode 100644 src/stats/views.py diff --git a/src/stats/forms.py b/src/stats/forms.py new file mode 100644 index 000000000..52ff11108 --- /dev/null +++ b/src/stats/forms.py @@ -0,0 +1,6 @@ +from django import forms + + +class VisitsForm(forms.Form): + date_since = forms.DateField(required=False) + date_until = forms.DateField(required=False) diff --git a/src/stats/templates/stats/top.html b/src/stats/templates/stats/top.html new file mode 100644 index 000000000..2ff1eb21d --- /dev/null +++ b/src/stats/templates/stats/top.html @@ -0,0 +1,32 @@ +{% extends 'base/base.html' %} + + +{% block body %} +
+ {{ form.as_p }} +
+ + + + + + + + {% for v in visits %} + + + + + + {% endfor %} +
KsiążkaUnikalne odsłonyOdsłony
+ + {{ v.book__slug }} + + + {{ v.unique_views }} + + {{ v.views }} +
+
+{% endblock %} diff --git a/src/stats/urls.py b/src/stats/urls.py new file mode 100644 index 000000000..80d261178 --- /dev/null +++ b/src/stats/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from . import views + + +urlpatterns = [ + path('top/', views.TopView.as_view(), name='stats_top'), +] diff --git a/src/stats/views.py b/src/stats/views.py new file mode 100644 index 000000000..4ff85f494 --- /dev/null +++ b/src/stats/views.py @@ -0,0 +1,29 @@ +from django.contrib.auth.mixins import PermissionRequiredMixin +from django.views.generic import TemplateView +from django.db.models import Sum +from . import forms +from . import models + + +class TopView(PermissionRequiredMixin, TemplateView): + permission_required = 'stats.view_visits' + template_name = 'stats/top.html' + + def get_context_data(self, **kwargs): + ctx = super().get_context_data(**kwargs) + form = forms.VisitsForm(self.request.GET) + assert form.is_valid() + visits = models.Visits.objects.all() + if form.cleaned_data['date_since']: + visits = visits.filter(date__gte=form.cleaned_data['date_since'].replace(day=1)) + if form.cleaned_data['date_until']: + visits = visits.filter(date__lte=form.cleaned_data['date_until']) + visits = visits.values('book__slug').annotate( + views=Sum('views'), + unique_views=Sum('unique_views') + ) + visits = visits.order_by('-unique_views') + ctx['form'] = form + ctx['visits'] = visits + return ctx + diff --git a/src/wolnelektury/urls.py b/src/wolnelektury/urls.py index 1fc60fcb0..f4bd6b251 100644 --- a/src/wolnelektury/urls.py +++ b/src/wolnelektury/urls.py @@ -47,6 +47,7 @@ urlpatterns += [ path('isbn/', include('isbn.urls')), path('messaging/', include('messaging.urls')), path('re/', include('redirects.urls')), + path('stats/', include('stats.urls')), path('paypal/app-form/', RedirectView.as_view( url='/towarzystwo/?pk_campaign=aplikacja', permanent=False)), -- 2.20.1