2018-02-04 19:28:09 +00:00
|
|
|
from django.contrib.admin.views.decorators import staff_member_required
|
2019-07-23 13:16:03 +00:00
|
|
|
from django.shortcuts import redirect, render, 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
|
2018-02-04 19:28:09 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
from django.http import Http404
|
2018-06-03 21:48:21 +00:00
|
|
|
from nsfw import views as nsfw
|
2019-07-23 13:16:03 +00:00
|
|
|
from .models import LogSource, LogEntry
|
2018-02-04 19:28:09 +00:00
|
|
|
from .forms import SearchForm
|
2018-06-03 20:16:40 +00:00
|
|
|
from utils import is_link_legit
|
2018-01-27 17:58:36 +00:00
|
|
|
import datetime
|
|
|
|
|
2018-01-27 22:12:09 +00:00
|
|
|
|
2019-07-23 13:16:03 +00:00
|
|
|
def chat_view(request):
|
|
|
|
ctx = {
|
|
|
|
'debug': settings.DEBUG,
|
|
|
|
'bosh_url': settings.KHAGANAT_XMPP_BOSH_URL,
|
|
|
|
'jid': settings.KHAGANAT_XMPP_JID,
|
|
|
|
'rooms': settings.KHAGANAT_XMPP_ROOMS,
|
|
|
|
'websocket_url': settings.KHAGANAT_XMPP_WEBSOCKET_URL,
|
|
|
|
}
|
|
|
|
return render(request, 'chat/chat_conversejs.html', ctx)
|
|
|
|
|
2018-02-04 19:28: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()
|
|
|
|
|
2018-06-03 20:16:40 +00:00
|
|
|
next_page = request.GET.get('next', '/')
|
|
|
|
if not is_link_legit(next_page):
|
2018-02-04 19:28:09 +00:00
|
|
|
next_page = reverse('log_index')
|
|
|
|
return redirect(next_page)
|
|
|
|
|
|
|
|
|
|
|
|
@staff_member_required
|
2019-07-23 13:16:03 +00:00
|
|
|
def switch_log_source(request, pk):
|
|
|
|
return _switch_hidden(request, LogSource, pk)
|
2018-02-04 19:28:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@staff_member_required
|
2019-07-23 13:16:03 +00:00
|
|
|
def switch_log_entry(request, pk):
|
|
|
|
return _switch_hidden(request, LogEntry, pk)
|
2018-02-04 19:28:09 +00:00
|
|
|
|
|
|
|
|
2019-07-23 13:16:03 +00:00
|
|
|
def log_search_view(request):
|
2018-02-04 19:28:09 +00:00
|
|
|
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.')
|
|
|
|
|
|
|
|
|
2019-07-23 13:16:03 +00:00
|
|
|
class LogEntriesView(generic.ListView):
|
|
|
|
template_name = 'chat/entries.html'
|
2018-06-03 20:16:40 +00:00
|
|
|
context_object_name = 'entries'
|
2018-06-03 21:48:21 +00:00
|
|
|
filter_nsfw = False
|
2018-01-27 17:58:36 +00:00
|
|
|
|
2018-06-03 20:16:40 +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
|
2018-02-04 19:28:09 +00:00
|
|
|
|
2018-06-03 20:16:40 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
if self.is_nsfw():
|
|
|
|
if not nsfw.is_nsfw_allowed(request):
|
2018-06-03 21:48:21 +00:00
|
|
|
self.filter_nsfw = True
|
2018-06-03 20:16:40 +00:00
|
|
|
else:
|
2018-06-03 21:48:21 +00:00
|
|
|
nsfw.alert(request, request.get_full_path())
|
2018-06-03 20:16:40 +00:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_date(self):
|
|
|
|
"""Return the date object corresponding to the URL parameters
|
|
|
|
or None if missing.
|
|
|
|
"""
|
|
|
|
has_date = all([
|
|
|
|
self.kwargs.get('year') is not None,
|
|
|
|
self.kwargs.get('month') is not None,
|
|
|
|
self.kwargs.get('day') is not None,
|
|
|
|
])
|
|
|
|
if not has_date:
|
|
|
|
return None
|
|
|
|
return datetime.date(
|
|
|
|
self.kwargs['year'],
|
|
|
|
self.kwargs['month'],
|
|
|
|
self.kwargs['day']
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_dates(self, source):
|
|
|
|
"""Return a list if available dates for the current source."""
|
|
|
|
_, start_date, end_date = _get_dates()
|
|
|
|
if source is None:
|
|
|
|
return []
|
2018-06-09 17:02:47 +00:00
|
|
|
|
2018-11-08 19:11:14 +00:00
|
|
|
nb_max = settings.KHAGANAT_LOGS_MAX_DAYS
|
|
|
|
nb_max -= settings.KHAGANAT_LOGS_MIN_DAYS
|
2019-07-23 13:16:03 +00:00
|
|
|
lst = LogEntry.objects.filter(
|
2018-06-03 20:16:40 +00:00
|
|
|
source=source,
|
2018-06-09 17:02:47 +00:00
|
|
|
)
|
|
|
|
if not self.request.user.is_staff:
|
|
|
|
lst = lst.filter(
|
|
|
|
hidden=False,
|
|
|
|
created__range=(start_date, end_date)
|
|
|
|
)
|
|
|
|
lst = lst.annotate(
|
2018-06-03 20:16:40 +00:00
|
|
|
date=TruncDate('created')
|
|
|
|
).values('date').annotate(
|
|
|
|
nb=Count('date')
|
2018-06-09 17:02:47 +00:00
|
|
|
).order_by('-date')[:nb_max]
|
2018-06-03 20:16:40 +00:00
|
|
|
return [o['date'] for o in lst]
|
|
|
|
|
|
|
|
def get_source(self):
|
|
|
|
"""Return the current source."""
|
|
|
|
if self.kwargs.get('source') is None:
|
|
|
|
return None
|
2019-07-23 13:16:03 +00:00
|
|
|
return LogSource.objects.get(slug=self.kwargs['source'])
|
2018-06-03 20:16:40 +00:00
|
|
|
|
|
|
|
def get_sources(self):
|
|
|
|
"""Return available sources."""
|
2018-02-04 19:28:09 +00:00
|
|
|
now, start_date, end_date = _get_dates()
|
2019-07-23 13:16:03 +00:00
|
|
|
qs = LogEntry.objects.all()
|
2018-02-04 19:28:09 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
)
|
|
|
|
|
2018-06-03 20:16:40 +00:00
|
|
|
out = []
|
2018-01-27 22:12:09 +00:00
|
|
|
for src in qs:
|
2018-06-03 20:16:40 +00:00
|
|
|
dts = self.get_dates(src['source'])
|
|
|
|
src['first_date'] = dts[0] if dts else None
|
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
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2018-06-03 20:16:40 +00:00
|
|
|
context['form'] = SearchForm()
|
|
|
|
context['sources'] = self.get_sources()
|
|
|
|
context['current_source'] = self.get_source()
|
|
|
|
context['dates'] = self.get_dates(context['current_source'])
|
|
|
|
context['current_date'] = self.get_date()
|
2018-06-03 21:48:21 +00:00
|
|
|
context['filter_nsfw'] = self.filter_nsfw
|
|
|
|
context['current_url'] = self.request.get_full_path()
|
2018-01-27 17:58:36 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2018-06-03 20:16:40 +00:00
|
|
|
"""Return the entries to be displayed."""
|
|
|
|
dt = self.get_date()
|
|
|
|
if dt is None:
|
|
|
|
return []
|
|
|
|
|
2018-02-04 19:28:09 +00:00
|
|
|
is_staff = self.request.user.is_staff
|
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,
|
|
|
|
))
|
2018-02-04 19:28:09 +00:00
|
|
|
if out_of_bounds and not is_staff:
|
2019-07-23 13:16:03 +00:00
|
|
|
return LogEntry.objects.none()
|
2018-06-03 20:16:40 +00:00
|
|
|
src = self.get_source()
|
2018-02-04 19:28:09 +00:00
|
|
|
if src is None:
|
2019-07-23 13:16:03 +00:00
|
|
|
return LogEntry.objects.none()
|
2018-02-04 19:28:09 +00:00
|
|
|
if src.hidden and not is_staff:
|
2019-07-23 13:16:03 +00:00
|
|
|
return LogEntry.objects.none()
|
|
|
|
qs = LogEntry.objects.filter(
|
2018-01-27 22:12:09 +00:00
|
|
|
source=src,
|
2018-06-02 12:29:46 +00:00
|
|
|
created__year=dt.year
|
|
|
|
).filter(
|
2018-01-27 22:12:09 +00:00
|
|
|
created__month=dt.month,
|
2018-06-02 12:29:46 +00:00
|
|
|
).filter(
|
2018-01-27 22:12:09 +00:00
|
|
|
created__day=dt.day
|
2018-02-04 19:28:09 +00:00
|
|
|
)
|
|
|
|
if not is_staff:
|
|
|
|
qs = qs.filter(hidden=False)
|
|
|
|
return qs.order_by('created')
|