From d2ae988c6ee79ce2e5f2d1e4988b634ba369dcd1 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 9 Mar 2021 23:02:23 +0100 Subject: [PATCH 2/3] manager: search deleted users by email (#51808) --- src/authentic2/manager/journal_views.py | 11 ++++++++++- tests/test_manager_journal.py | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/authentic2/manager/journal_views.py b/src/authentic2/manager/journal_views.py index 70e8fce2..66fe9dc3 100644 --- a/src/authentic2/manager/journal_views.py +++ b/src/authentic2/manager/journal_views.py @@ -19,15 +19,17 @@ import uuid from django import forms from django.contrib.auth import get_user_model +from django.contrib.contenttypes.models import ContentType from django.core.exceptions import PermissionDenied, ValidationError from django.core.validators import EmailValidator 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.models import EventType, n_2_pairing from authentic2.apps.journal.search_engine import JournalSearchEngine from authentic2.apps.journal.views import JournalView +from authentic2.custom_user.models import DeletedUser from . import views @@ -71,6 +73,13 @@ to user whose UUID is 1234.''' users = User.objects.find_duplicates(fullname=fullname) return self.query_for_users(users) + def search_by_email(self, email): + yield from super().search_by_email(email) + pks = list(DeletedUser.objects.filter(old_email=email).values_list('old_user_id', flat=True)) + yield Q(user_id__in=pks) + user_ct = ContentType.objects.get_for_model(User) + yield Q(reference_ids__contains=[n_2_pairing(user_ct.id, pk) for pk in pks]) + EVENT_TYPE_CHOICES = ( ('', _('All')), diff --git a/tests/test_manager_journal.py b/tests/test_manager_journal.py index 2211ff87..08317922 100644 --- a/tests/test_manager_journal.py +++ b/tests/test_manager_journal.py @@ -987,14 +987,14 @@ def test_search(app, superuser, events): response.form.set('search', 'email:jane@example.com') response = response.form.submit() assert ( - text_content(response.pyquery('tbody tr td.journal-list--message-column')[0]).strip() + text_content(response.pyquery('tbody tr td.journal-list--message-column')[12]).strip() == 'email change of user "Johnny doe" for email address "jane@example.com"' ) response.form.set('search', 'jane@example.com') response = response.form.submit() assert ( - text_content(response.pyquery('tbody tr td.journal-list--message-column')[0]).strip() + text_content(response.pyquery('tbody tr td.journal-list--message-column')[12]).strip() == 'email change of user "Johnny doe" for email address "jane@example.com"' ) -- 2.32.0.rc0