From 2ac67afb03691e26351d6269c8127f12bb8fe021 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 24 Mar 2021 12:04:36 +0100 Subject: [PATCH] manager: easier journal filtering by event types (#50054) --- src/authentic2/manager/journal_views.py | 44 +++++++++++++++++++++++++ tests/test_manager_journal.py | 32 ++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/authentic2/manager/journal_views.py b/src/authentic2/manager/journal_views.py index e764db2e..09957efa 100644 --- a/src/authentic2/manager/journal_views.py +++ b/src/authentic2/manager/journal_views.py @@ -16,6 +16,7 @@ import uuid +from django import forms from django.contrib.auth import get_user_model from django.core.exceptions import PermissionDenied, ValidationError from django.core.validators import EmailValidator @@ -23,6 +24,7 @@ from django.db.models import Q from django.utils.translation import ugettext_lazy as _ from authentic2.apps.journal.forms import JournalForm +from authentic2.apps.journal.models import EventType from authentic2.apps.journal.search_engine import JournalSearchEngine from authentic2.apps.journal.views import JournalView @@ -69,9 +71,51 @@ to user whose UUID is 1234.''' return self.query_for_users(users) +EVENT_TYPE_CHOICES = ( + ('', _('All')), + ( + _('Users'), ( + ('manager.user.activation', _('Activation')), + ('manager.user.deactivation', _('Deactivation')), + ('user.deletion', _('User deletion')), + ('user.login', _('Login')), + ('logout', _('Logout')), + ('password.change', _('Password change')), + ('password.reset', _('Password reset')), + ('profile.edit', _('Profile edition')), + ('user.registration', _('Registration')), + ) + ), + ( + _('Roles'), ( + ('manager.role.administrator', _('Role administrator change')), + ('manager.role.creation', _('Role creation')), + ('manager.role.deletion', _('Role deletion')), + ('manager.role.edit', _('Role edition')), + ('manager.role.inheritance', _('Role inheritance change')), + ('manager.role.membership', _('Role membership change')), + ) + ) +) + + class JournalForm(JournalForm): search_engine_class = JournalSearchEngine + event_type = forms.ChoiceField(required=False, choices=EVENT_TYPE_CHOICES) + + def clean_event_type(self): + return EventType.objects.filter(name__contains=self.cleaned_data['event_type']) + + def get_queryset(self, **kwargs): + qs = super().get_queryset(**kwargs) + + event_type = self.cleaned_data.get('event_type') + if event_type: + qs = qs.filter(type__in=event_type) + + return qs + class BaseJournalView(views.TitleMixin, views.MediaMixin, views.MultipleOUMixin, JournalView): template_name = 'authentic2/manager/journal.html' diff --git a/tests/test_manager_journal.py b/tests/test_manager_journal.py index b2020d76..63725ff1 100644 --- a/tests/test_manager_journal.py +++ b/tests/test_manager_journal.py @@ -890,3 +890,35 @@ def test_search(app, superuser, events): list(map(text_content, p)) for p in zip(pq('tbody td.journal-list--user-column'), pq('tbody td.journal-list--message-column')) ] == [['Johnny doe', 'login using password']] + + response.form.set('search', '') + response.form.set('event_type', 'password.change') + response = response.form.submit() + pq = response.pyquery + + table_content = [ + list(map(text_content, p)) + for p in zip(pq('tbody td.journal-list--user-column'), pq('tbody td.journal-list--message-column')) + ] + assert table_content == [ + ['agent', 'mandatory password change at next login unset for user "Johnny doe"'], + ['agent', 'mandatory password change at next login set for user "Johnny doe"'], + ['agent', 'password change of user "Johnny doe" and notification by mail'], + ['agent', 'password change of user "Johnny doe"'], + ['Johnny doe', 'password change'] + ] + + response.form.set('search', 'johnny doe') + response = response.form.submit() + pq = response.pyquery + + assert [ + list(map(text_content, p)) + for p in zip(pq('tbody td.journal-list--user-column'), pq('tbody td.journal-list--message-column')) + ] == table_content + + response.form.set('search', 'agent@example.com') + response = response.form.submit() + pq = response.pyquery + + assert len(response.pyquery('tbody tr')) == 4 -- 2.20.1