1 from django.contrib.auth.mixins import PermissionRequiredMixin
2 from django.views.generic import TemplateView
3 from django.db.models import Sum
8 class TopView(PermissionRequiredMixin, TemplateView):
10 permission_required = 'stats.view_visits'
11 template_name = 'stats/top.html'
13 def get_context_data(self, **kwargs):
14 ctx = super().get_context_data(**kwargs)
15 form = forms.VisitsForm(self.request.GET)
16 assert form.is_valid()
17 visits = self.model.objects.all()
18 if form.cleaned_data['date_since']:
19 visits = visits.filter(date__gte=form.cleaned_data['date_since'].replace(day=1))
20 if form.cleaned_data['date_until']:
21 visits = visits.filter(date__lte=form.cleaned_data['date_until'])
22 visits = visits.values('book__slug').annotate(
24 unique_views=Sum('unique_views')
26 visits = visits.order_by('-unique_views')
28 ctx['visits'] = visits
32 class DailyTopView(TopView):
33 model = models.DayVisits