Project

General

Profile

0001-manager-search-journal-by-uuid-of-deleted-accounts-6.patch

Benjamin Dauvergne, 27 September 2022 04:03 PM

Download (3.59 KB)

View differences:

Subject: [PATCH 1/3] manager: search journal by uuid of deleted accounts
 (#69591)

Search by a queryset of DeletedUser is extracted
from search_by_email() to be shared with search_by_uuid().
 src/authentic2/manager/journal_views.py | 18 ++++++++++++++----
 tests/test_manager_journal.py           | 23 +++++++++++++++++++++++
 2 files changed, 37 insertions(+), 4 deletions(-)
src/authentic2/manager/journal_views.py
47 47
            user_uuid = uuid.UUID(lexem)
48 48
        except ValueError:
49 49
            yield self.q_false
50
        else:
50
            return
51
        # check if uuid exists to go on the fast path
52
        # searching in DeletedUser is expensive
53
        if User.objects.filter(uuid=user_uuid.hex).exists():
51 54
            yield Q(user__uuid=user_uuid.hex)
55
        else:
56
            deleted_user_qs = DeletedUser.objects.filter(old_uuid=user_uuid.hex)
57
            yield from self._search_by_deleted_user(deleted_user_qs)
52 58

  
53 59
    @classmethod
54 60
    def search_by_uuid_documentation(cls):
......
78 84
            users = User.objects.find_duplicates(fullname=fullname, threshold=app_settings.A2_FTS_THRESHOLD)
79 85
            return self.query_for_users(users)
80 86

  
81
    def search_by_email(self, email):
82
        yield from super().search_by_email(email)
83
        pks = list(DeletedUser.objects.filter(old_email=email).values_list('old_user_id', flat=True))
87
    def _search_by_deleted_user(self, deleted_user_qs):
88
        pks = list(deleted_user_qs.values_list('old_user_id', flat=True))
84 89
        if pks:
85 90
            yield Q(user_id__in=pks)
86 91
            user_ct = ContentType.objects.get_for_model(User)
87 92
            yield Q(reference_ids__contains=[n_2_pairing(user_ct.id, pk) for pk in pks])
88 93

  
94
    def search_by_email(self, email):
95
        yield from super().search_by_email(email)
96
        deleted_user_qs = DeletedUser.objects.filter(old_email=email)
97
        yield from self._search_by_deleted_user(deleted_user_qs)
98

  
89 99
    @classmethod
90 100
    def search_by_event_documentation(cls):
91 101
        return '%s %s' % (
tests/test_manager_journal.py
1340 1340
    response.form['event_type'].select(text='User deletions')
1341 1341
    response = response.form.submit()
1342 1342
    assert len(response.pyquery('tbody tr[data-event-type]')) == 0
1343

  
1344

  
1345
def test_search_by_uuid(app, superuser, events):
1346
    from authentic2.custom_user.models import DeletedUser
1347

  
1348
    login(app, user=superuser)
1349
    response = app.get('/manage/journal/')
1350

  
1351
    response.form.set('search', 'uuid:%s' % events['user'].uuid)
1352
    response = response.form.submit()
1353
    assert len(response.pyquery('tbody tr')) > 0
1354

  
1355
    events['user'].delete()
1356

  
1357
    response.form.set('search', 'uuid:%s' % events['user'].uuid)
1358
    response = response.form.submit()
1359
    assert len(response.pyquery('tbody tr')) > 0
1360

  
1361
    DeletedUser.objects.all().delete()
1362

  
1363
    response.form.set('search', 'uuid:%s' % events['user'].uuid)
1364
    response = response.form.submit()
1365
    assert len(response.pyquery('tbody tr')) == 0
1343
-