khaganat-web/logs/views.py

163 lines
4.8 KiB
Python
Raw Normal View History

from django.contrib.admin.views.decorators import staff_member_required
from django.shortcuts import redirect, get_object_or_404
2018-01-27 17:58:36 +00:00
from django.db.models.functions import TruncDate
from django.db.models import Count
from django.views import generic
from django.conf import settings
from django.urls import reverse
from django.http import Http404
2018-05-27 16:45:52 +00:00
from neluser import nsfw
2018-01-27 22:12:09 +00:00
from .models import Source, Entry
from .forms import SearchForm
2018-01-27 17:58:36 +00:00
import datetime
2018-01-27 22:12:09 +00:00
def _get_dates():
now = datetime.date.today()
start_date = now - datetime.timedelta(
days=settings.KHAGANAT_LOGS_MAX_DAYS
)
end_date = now - datetime.timedelta(
days=settings.KHAGANAT_LOGS_MIN_DAYS - 1
)
return (now, start_date, end_date)
def _switch_hidden(request, obj, pk):
e = get_object_or_404(obj, pk=pk)
e.hidden = not e.hidden
e.save()
next_page = request.GET.get('next', '')
if next_page == '':
next_page = reverse('log_index')
return redirect(next_page)
@staff_member_required
def switch_source(request, pk):
return _switch_hidden(request, Source, pk)
@staff_member_required
def switch_entry(request, pk):
return _switch_hidden(request, Entry, pk)
def search_view(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
date = form.cleaned_data['date']
return redirect(reverse('log_day', kwargs={
'source': form.cleaned_data['source'],
'year': date.year,
'month': date.month,
'day': date.day,
}))
raise Http404('No search parameters.')
2018-01-27 17:58:36 +00:00
class IndexView(generic.ListView):
template_name = 'logs/index.html'
context_object_name = 'sources'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = SearchForm()
return context
2018-01-27 17:58:36 +00:00
def get_queryset(self):
now, start_date, end_date = _get_dates()
2018-01-27 17:58:36 +00:00
out = []
qs = Entry.objects.all()
if not self.request.user.is_staff:
qs = qs.filter(
hidden=False,
source__hidden=False,
created__range=(start_date, end_date)
)
qs = qs.values(
'source', 'source__name', 'source__slug',
'source__id', 'source__hidden'
).annotate(
2018-01-27 22:12:09 +00:00
nb=Count('id')
)
for src in qs:
src['stats'] = Entry.objects.filter(
hidden=False,
source=src['source'],
created__range=(start_date, end_date)
).annotate(
date=TruncDate('created')
).values('date').annotate(nb=Count('date')).order_by('-date')
2018-01-27 17:58:36 +00:00
out.append(src)
2018-01-27 22:12:09 +00:00
2018-01-27 17:58:36 +00:00
return out
class EntriesView(generic.ListView):
context_object_name = 'entries'
template_name = 'logs/entries.html'
allow_empty = False
2018-05-27 16:45:52 +00:00
def is_nsfw(self):
for e in self.get_queryset():
for tag in settings.KHAGANAT_NSFW_TAGS:
if tag.lower() in e.content.lower():
return True
return False
def dispatch(self, request, *args, **kwargs):
if not nsfw.is_nsfw_allowed(request):
if self.is_nsfw():
return nsfw.redirect(request)
return super().dispatch(request, *args, **kwargs)
2018-01-27 17:58:36 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2018-01-27 22:12:09 +00:00
context['date'] = datetime.date(
self.kwargs['year'],
self.kwargs['month'],
self.kwargs['day']
)
context['source_channel'] = Source.objects.get(
slug=self.kwargs['source']
)
2018-01-27 17:58:36 +00:00
if isinstance(context['source_channel'], tuple):
context['source_channel'] = context['source_channel'][0]
return context
def get_queryset(self):
is_staff = self.request.user.is_staff
2018-01-27 22:12:09 +00:00
dt = datetime.date(
self.kwargs['year'],
self.kwargs['month'],
self.kwargs['day']
)
2018-01-27 17:58:36 +00:00
now = datetime.date.today()
2018-01-27 22:12:09 +00:00
out_of_bounds = any((
(now - dt).days > settings.KHAGANAT_LOGS_MAX_DAYS,
(now - dt).days < settings.KHAGANAT_LOGS_MIN_DAYS,
))
if out_of_bounds and not is_staff:
2018-01-27 17:58:36 +00:00
return Entry.objects.none()
src = Source.objects.get(slug=self.kwargs['source'])
if src is None:
2018-01-27 17:58:36 +00:00
return Entry.objects.none()
if src.hidden and not is_staff:
return Entry.objects.none()
qs = Entry.objects.filter(
2018-01-27 22:12:09 +00:00
source=src,
created__year=dt.year
).filter(
2018-01-27 22:12:09 +00:00
created__month=dt.month,
).filter(
2018-01-27 22:12:09 +00:00
created__day=dt.day
)
if not is_staff:
qs = qs.filter(hidden=False)
return qs.order_by('created')