Projet

Général

Profil

0001-manager-easier-journal-filtering-by-event-types-5005.patch

Valentin Deniaud, 24 mars 2021 12:10

Télécharger (4,57 ko)

Voir les différences:

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(+)
src/authentic2/manager/journal_views.py
16 16

  
17 17
import uuid
18 18

  
19
from django import forms
19 20
from django.contrib.auth import get_user_model
20 21
from django.core.exceptions import PermissionDenied, ValidationError
21 22
from django.core.validators import EmailValidator
......
23 24
from django.utils.translation import ugettext_lazy as _
24 25

  
25 26
from authentic2.apps.journal.forms import JournalForm
27
from authentic2.apps.journal.models import EventType
26 28
from authentic2.apps.journal.search_engine import JournalSearchEngine
27 29
from authentic2.apps.journal.views import JournalView
28 30

  
......
69 71
            return self.query_for_users(users)
70 72

  
71 73

  
74
EVENT_TYPE_CHOICES = (
75
    ('', _('All')),
76
    (
77
        _('Users'), (
78
            ('manager.user.activation', _('Activation')),
79
            ('manager.user.deactivation', _('Deactivation')),
80
            ('user.deletion', _('User deletion')),
81
            ('user.login', _('Login')),
82
            ('logout', _('Logout')),
83
            ('password.change', _('Password change')),
84
            ('password.reset', _('Password reset')),
85
            ('profile.edit', _('Profile edition')),
86
            ('user.registration', _('Registration')),
87
        )
88
    ),
89
    (
90
        _('Roles'), (
91
            ('manager.role.administrator', _('Role administrator change')),
92
            ('manager.role.creation', _('Role creation')),
93
            ('manager.role.deletion', _('Role deletion')),
94
            ('manager.role.edit', _('Role edition')),
95
            ('manager.role.inheritance', _('Role inheritance change')),
96
            ('manager.role.membership', _('Role membership change')),
97
        )
98
    )
99
)
100

  
101

  
72 102
class JournalForm(JournalForm):
73 103
    search_engine_class = JournalSearchEngine
74 104

  
105
    event_type = forms.ChoiceField(required=False, choices=EVENT_TYPE_CHOICES)
106

  
107
    def clean_event_type(self):
108
        return EventType.objects.filter(name__contains=self.cleaned_data['event_type'])
109

  
110
    def get_queryset(self, **kwargs):
111
        qs = super().get_queryset(**kwargs)
112

  
113
        event_type = self.cleaned_data.get('event_type')
114
        if event_type:
115
            qs = qs.filter(type__in=event_type)
116

  
117
        return qs
118

  
75 119

  
76 120
class BaseJournalView(views.TitleMixin, views.MediaMixin, views.MultipleOUMixin, JournalView):
77 121
    template_name = 'authentic2/manager/journal.html'
tests/test_manager_journal.py
890 890
        list(map(text_content, p))
891 891
        for p in zip(pq('tbody td.journal-list--user-column'), pq('tbody td.journal-list--message-column'))
892 892
    ] == [['Johnny doe', 'login using password']]
893

  
894
    response.form.set('search', '')
895
    response.form.set('event_type', 'password.change')
896
    response = response.form.submit()
897
    pq = response.pyquery
898

  
899
    table_content = [
900
        list(map(text_content, p))
901
        for p in zip(pq('tbody td.journal-list--user-column'), pq('tbody td.journal-list--message-column'))
902
    ]
903
    assert table_content == [
904
        ['agent', 'mandatory password change at next login unset for user "Johnny doe"'],
905
        ['agent', 'mandatory password change at next login set for user "Johnny doe"'],
906
        ['agent', 'password change of user "Johnny doe" and notification by mail'],
907
        ['agent', 'password change of user "Johnny doe"'],
908
        ['Johnny doe', 'password change']
909
    ]
910

  
911
    response.form.set('search', 'johnny doe')
912
    response = response.form.submit()
913
    pq = response.pyquery
914

  
915
    assert [
916
        list(map(text_content, p))
917
        for p in zip(pq('tbody td.journal-list--user-column'), pq('tbody td.journal-list--message-column'))
918
    ] == table_content
919

  
920
    response.form.set('search', 'agent@example.com')
921
    response = response.form.submit()
922
    pq = response.pyquery
923

  
924
    assert len(response.pyquery('tbody tr')) == 4
893
-