From 0403038cd4c12645c08f7270f8a1bae34e7b0154 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 23 Feb 2021 11:34:05 +0100 Subject: [PATCH 2/2] manager: filter journal entries by event type (#50054) --- src/authentic2/apps/journal/forms.py | 20 +++++++++++++ src/authentic2/manager/journal_views.py | 30 ++++++++++++++++++- .../templates/authentic2/manager/journal.html | 1 + src/authentic2/manager/urls.py | 2 ++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/authentic2/apps/journal/forms.py b/src/authentic2/apps/journal/forms.py index a18707b2..1382f484 100644 --- a/src/authentic2/apps/journal/forms.py +++ b/src/authentic2/apps/journal/forms.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import unicodedata from datetime import datetime from django.http import QueryDict @@ -161,6 +162,10 @@ class JournalForm(forms.Form): before_cursor = forms.CharField(widget=forms.HiddenInput(), required=False) + event_types = forms.ModelMultipleChoiceField( + widget=forms.MultipleHiddenInput(), required=False, queryset=models.EventType.objects.all() + ) + search = SearchField(required=False, label='') search_engine_class = search_engine.JournalSearchEngine @@ -253,6 +258,7 @@ class JournalForm(forms.Form): month = self.cleaned_data.get('month') day = self.cleaned_data.get('day') search_query = self.cleaned_data.get('_search_query') + event_types = self.cleaned_data.get('event_types') if year: qs = qs.filter(timestamp__year=year) @@ -262,6 +268,8 @@ class JournalForm(forms.Form): qs = qs.filter(timestamp__day=day) if search_query: qs = qs.filter(search_query) + if event_types: + qs = qs.filter(type__in=event_types) return qs def make_querydict(self, name=None, value=None, exclude=()): @@ -350,3 +358,15 @@ class JournalForm(forms.Form): @property def url(self): return self.make_url() + + +class ChooseEventTypeForm(forms.Form): + event_types = forms.ModelMultipleChoiceField( + widget=forms.CheckboxSelectMultiple(), required=False, queryset=models.EventType.objects.all() + ) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['event_types'].choices = sorted( + self.fields['event_types'].choices, key=lambda x: unicodedata.normalize('NFKD', x[1]) + ) diff --git a/src/authentic2/manager/journal_views.py b/src/authentic2/manager/journal_views.py index e764db2e..afc0bd61 100644 --- a/src/authentic2/manager/journal_views.py +++ b/src/authentic2/manager/journal_views.py @@ -15,14 +15,17 @@ # along with this program. If not, see . import uuid +from urllib.parse import urlparse from django.contrib.auth import get_user_model from django.core.exceptions import PermissionDenied, ValidationError from django.core.validators import EmailValidator from django.db.models import Q +from django.http import QueryDict from django.utils.translation import ugettext_lazy as _ +from django.views.generic.edit import FormView -from authentic2.apps.journal.forms import JournalForm +from authentic2.apps.journal.forms import JournalForm, ChooseEventTypeForm from authentic2.apps.journal.search_engine import JournalSearchEngine from authentic2.apps.journal.views import JournalView @@ -96,3 +99,28 @@ class GlobalJournalView(BaseJournalView): journal = GlobalJournalView.as_view() + + +class ChooseEventTypesView(views.TitleMixin, FormView): + form_class = ChooseEventTypeForm + template_name = 'authentic2/manager/form.html' + title = _('Choose event types') + + def get_initial(self): + next_url = urlparse(self.request.GET['next']) + params = QueryDict(next_url.query) + return {'event_types': params.getlist('event_types')} + + def form_valid(self, form): + self.event_type_pks = form.cleaned_data['event_types'].values_list('pk', flat=True) + return super().form_valid(form) + + def get_success_url(self): + next_url = urlparse(self.request.GET['next']) + params = QueryDict(next_url.query, mutable=True) + params.setlist('event_types', self.event_type_pks) + next_url = next_url._replace(query=params.urlencode()) + return next_url.geturl() + + +choose_event_types = ChooseEventTypesView.as_view() diff --git a/src/authentic2/manager/templates/authentic2/manager/journal.html b/src/authentic2/manager/templates/authentic2/manager/journal.html index 8d49d0eb..294de8a4 100644 --- a/src/authentic2/manager/templates/authentic2/manager/journal.html +++ b/src/authentic2/manager/templates/authentic2/manager/journal.html @@ -22,6 +22,7 @@ +

{% trans "Filter by event types" %}

{% if date_hierarchy.choice_name %}

{{ date_hierarchy.choice_name }}

diff --git a/src/authentic2/manager/urls.py b/src/authentic2/manager/urls.py index 244f242d..f81c4f4a 100644 --- a/src/authentic2/manager/urls.py +++ b/src/authentic2/manager/urls.py @@ -167,6 +167,8 @@ urlpatterns = required( # Journal url(r'^journal/$', journal_views.journal, name='a2-manager-journal'), + url(r'^journal/choose-event-types/$', journal_views.choose_event_types, + name='a2-manager-journal-choose-event-types'), # backoffice menu as json url(r'^menu.json$', views.menu_json), -- 2.20.1