From 28e0743b3caddbb3d51d68fa5b44fa5e5e38adbc Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sun, 4 Feb 2018 20:28:09 +0100 Subject: [PATCH] Add an inline admin interface to the log module --- logs/admin.py | 3 +- logs/forms.py | 14 ++++ logs/locale/en/LC_MESSAGES/django.po | 32 +++++++++- logs/locale/fr/LC_MESSAGES/django.po | 32 +++++++++- logs/templates/logs/entries.html | 3 + logs/templates/logs/index.html | 18 ++++++ logs/urls.py | 14 +++- logs/views.py | 96 +++++++++++++++++++++++----- neluser/views.py | 3 +- 9 files changed, 187 insertions(+), 28 deletions(-) create mode 100644 logs/forms.py diff --git a/logs/admin.py b/logs/admin.py index fe46e23..50945d8 100644 --- a/logs/admin.py +++ b/logs/admin.py @@ -1,5 +1,4 @@ from django.contrib import admin -from .models import Source, Entry +from .models import Source admin.site.register(Source) -admin.site.register(Entry) diff --git a/logs/forms.py b/logs/forms.py new file mode 100644 index 0000000..a364a41 --- /dev/null +++ b/logs/forms.py @@ -0,0 +1,14 @@ +from django.utils.translation import gettext_lazy as _ +from django.forms.widgets import DateInput +from django import forms +import datetime + + +class SearchForm(forms.Form): + source = forms.CharField(max_length=128) + date = forms.DateField( + label=_('date'), + initial=datetime.date.today(), + widget=DateInput(format='%d-%m-%Y'), + input_formats=['%d-%m-%Y'], + ) diff --git a/logs/locale/en/LC_MESSAGES/django.po b/logs/locale/en/LC_MESSAGES/django.po index c8e6fa8..32dfaa5 100644 --- a/logs/locale/en/LC_MESSAGES/django.po +++ b/logs/locale/en/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: 1.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-01-27 18:32+0100\n" +"POT-Creation-Date: 2018-02-04 19:45+0100\n" "PO-Revision-Date: 2018-01-27 18:35+0100\n" "Last-Translator: Khaganat \n" "Language-Team: Khaganat \n" @@ -12,7 +12,11 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -#: templates/logs/entries.html:10 templates/logs/entries.html:18 +#: forms.py:10 +msgid "date" +msgstr "" + +#: templates/logs/entries.html:10 templates/logs/entries.html:21 msgid "logs_back" msgstr "Back" @@ -20,6 +24,28 @@ msgstr "Back" msgid "logs_title" msgstr "Khaganat's logs" -#: templates/logs/index.html:33 +#: templates/logs/index.html:32 +msgid "go" +msgstr "" + +#: templates/logs/index.html:36 +#, python-format +msgid "%(name)s is hidden." +msgstr "" + +#: templates/logs/index.html:37 +msgid "show" +msgstr "" + +#: templates/logs/index.html:39 +#, python-format +msgid "%(name)s is published." +msgstr "" + +#: templates/logs/index.html:40 +msgid "hide" +msgstr "" + +#: templates/logs/index.html:51 msgid "logs_no_logs_available" msgstr "No logs available." diff --git a/logs/locale/fr/LC_MESSAGES/django.po b/logs/locale/fr/LC_MESSAGES/django.po index e893b0b..b6b97c1 100644 --- a/logs/locale/fr/LC_MESSAGES/django.po +++ b/logs/locale/fr/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: 1.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-01-27 18:32+0100\n" +"POT-Creation-Date: 2018-02-04 19:45+0100\n" "PO-Revision-Date: 2018-01-27 18:35+0100\n" "Last-Translator: Khaganat \n" "Language-Team: Khaganat \n" @@ -12,7 +12,11 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n" -#: templates/logs/entries.html:10 templates/logs/entries.html:18 +#: forms.py:10 +msgid "date" +msgstr "" + +#: templates/logs/entries.html:10 templates/logs/entries.html:21 msgid "logs_back" msgstr "Retour" @@ -20,6 +24,28 @@ msgstr "Retour" msgid "logs_title" msgstr "Journaux de conversation de Khaganat" -#: templates/logs/index.html:33 +#: templates/logs/index.html:32 +msgid "go" +msgstr "" + +#: templates/logs/index.html:36 +#, python-format +msgid "%(name)s is hidden." +msgstr "%(name)s est masqué." + +#: templates/logs/index.html:37 +msgid "show" +msgstr "montrer" + +#: templates/logs/index.html:39 +#, python-format +msgid "%(name)s is published." +msgstr "%(name)s est publié." + +#: templates/logs/index.html:40 +msgid "hide" +msgstr "masquer" + +#: templates/logs/index.html:51 msgid "logs_no_logs_available" msgstr "Aucun journal de conversation disponible." diff --git a/logs/templates/logs/entries.html b/logs/templates/logs/entries.html index 5d138b1..b3d6e5a 100644 --- a/logs/templates/logs/entries.html +++ b/logs/templates/logs/entries.html @@ -11,6 +11,9 @@
{% for entry in entries%} + {% if request.user.is_staff %} + {% if entry.hidden %}{% else %}{% endif %} + {% endif %} {{ entry.created|date:"H:i:s" }} {% if entry.nick %}<{{ entry.nick }}> {{ entry.content }}{% else %}* {{ entry.content }}{% endif %}
{% endfor %}
diff --git a/logs/templates/logs/index.html b/logs/templates/logs/index.html index d272362..6a24cb6 100644 --- a/logs/templates/logs/index.html +++ b/logs/templates/logs/index.html @@ -23,6 +23,24 @@ {% for date in source.stats %} {{ date.date }} {% endfor %} + {% if request.user.is_staff %} +
+
+ {% csrf_token %} + {{ form.date }} + + +
+ + {% if source.source__hidden %} + {% blocktrans with name=source.source__name %}{{ name }} is hidden.{% endblocktrans %} + {% trans 'show'|capfirst %} + {% else %} + {% blocktrans with name=source.source__name %}{{ name }} is published.{% endblocktrans %} + {% trans 'hide'|capfirst %} + {% endif %} +
+ {% endif %} {% endfor %} diff --git a/logs/urls.py b/logs/urls.py index a204c22..6d3bf0d 100644 --- a/logs/urls.py +++ b/logs/urls.py @@ -6,6 +6,18 @@ urlpatterns = [ path('', views.IndexView.as_view(), name='log_index'), path( '////', - views.EntriesView.as_view(), name='log_day' + views.EntriesView.as_view(), + name='log_day' ), + path( + 'switch/source//', + views.switch_source, + name='log_switch_source' + ), + path( + 'switch/entry//', + views.switch_entry, + name='log_switch_entry' + ), + path('search/', views.search_view, name='log_search'), ] diff --git a/logs/views.py b/logs/views.py index ca0b4eb..f8e3304 100644 --- a/logs/views.py +++ b/logs/views.py @@ -1,29 +1,86 @@ +from django.contrib.admin.views.decorators import staff_member_required +from django.shortcuts import redirect, get_object_or_404 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 from .models import Source, Entry +from .forms import SearchForm import datetime +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.') + + 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 + def get_queryset(self): - 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 - ) + now, start_date, end_date = _get_dates() out = [] - qs = Entry.objects.filter( - hidden=False, - source__hidden=False, - created__range=(start_date, end_date) - ).values('source', 'source__name', 'source__slug').annotate( + 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( nb=Count('id') ) @@ -60,6 +117,7 @@ class EntriesView(generic.ListView): return context def get_queryset(self): + is_staff = self.request.user.is_staff dt = datetime.date( self.kwargs['year'], self.kwargs['month'], @@ -70,15 +128,19 @@ class EntriesView(generic.ListView): (now - dt).days > settings.KHAGANAT_LOGS_MAX_DAYS, (now - dt).days < settings.KHAGANAT_LOGS_MIN_DAYS, )) - if out_of_bounds: + if out_of_bounds and not is_staff: return Entry.objects.none() src = Source.objects.get(slug=self.kwargs['source']) - if src is None or src.hidden: + if src is None: return Entry.objects.none() - return Entry.objects.filter( - hidden=False, + if src.hidden and not is_staff: + return Entry.objects.none() + qs = Entry.objects.filter( source=src, created__year=dt.year, created__month=dt.month, created__day=dt.day - ).order_by('created') + ) + if not is_staff: + qs = qs.filter(hidden=False) + return qs.order_by('created') diff --git a/neluser/views.py b/neluser/views.py index df698ae..f28ba46 100644 --- a/neluser/views.py +++ b/neluser/views.py @@ -1,5 +1,5 @@ from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode -from django.shortcuts import render, redirect, get_object_or_404 +from django.shortcuts import render, get_object_or_404 from django.contrib.auth.tokens import default_token_generator from django.contrib.sites.shortcuts import get_current_site from django.template.loader import render_to_string @@ -7,7 +7,6 @@ from django.core.mail import EmailMultiAlternatives from django.utils.encoding import force_bytes from django.http import HttpResponse, Http404 from django.conf import settings -from django.urls import reverse from .forms import RegistrationForm from .models import NelUser