45 lines
2.1 KiB
Python
45 lines
2.1 KiB
Python
|
from django.db.models.functions import TruncDate
|
||
|
from django.db.models import Count
|
||
|
from django.views import generic
|
||
|
from django.conf import settings
|
||
|
from .models import Source,Entry
|
||
|
import datetime
|
||
|
|
||
|
class IndexView(generic.ListView):
|
||
|
template_name = 'logs/index.html'
|
||
|
context_object_name = 'sources'
|
||
|
|
||
|
def get_queryset(self):
|
||
|
now = datetime.date.today()
|
||
|
begin_date = now - datetime.timedelta(days=settings.KHAGANAT_LOGS_MAX_DAYS)
|
||
|
end_date = now - datetime.timedelta(days=settings.KHAGANAT_LOGS_MIN_DAYS - 1)
|
||
|
out = []
|
||
|
for src in Entry.objects.filter(hidden=False, source__hidden=False, created__range=(begin_date, end_date)).values('source', 'source__name', 'source__slug').annotate(nb=Count('id')):
|
||
|
src['stats'] = Entry.objects.filter(hidden=False, source=src['source'], created__range=(begin_date, end_date)).annotate(date=TruncDate('created')).values('date').annotate(nb=Count('date')).order_by('-date')
|
||
|
out.append(src)
|
||
|
return out
|
||
|
|
||
|
|
||
|
class EntriesView(generic.ListView):
|
||
|
context_object_name = 'entries'
|
||
|
template_name = 'logs/entries.html'
|
||
|
allow_empty = False
|
||
|
|
||
|
def get_context_data(self, **kwargs):
|
||
|
context = super().get_context_data(**kwargs)
|
||
|
context['date'] = datetime.date(self.kwargs['year'], self.kwargs['month'], self.kwargs['day'])
|
||
|
context['source_channel'] = Source.objects.get(slug=self.kwargs['source']),
|
||
|
if isinstance(context['source_channel'], tuple):
|
||
|
context['source_channel'] = context['source_channel'][0]
|
||
|
return context
|
||
|
|
||
|
def get_queryset(self):
|
||
|
dt = datetime.date(self.kwargs['year'], self.kwargs['month'], self.kwargs['day'])
|
||
|
now = datetime.date.today()
|
||
|
if (now - dt).days > settings.KHAGANAT_LOGS_MAX_DAYS or (now - dt).days < settings.KHAGANAT_LOGS_MIN_DAYS:
|
||
|
return Entry.objects.none()
|
||
|
src = Source.objects.get(slug=self.kwargs['source'])
|
||
|
if src is None or src.hidden:
|
||
|
return Entry.objects.none()
|
||
|
return Entry.objects.filter(hidden=False, source=src, created__year=dt.year, created__month=dt.month, created__day=dt.day).order_by('created')
|