# HG changeset patch # User Peter Sanchez # Date 1601686651 25200 # Fri Oct 02 17:57:31 2020 -0700 # Node ID d8c9df67fcbd0ae751392e88ff9f8684730e7196 # Parent dc72819a06e814a01c5ccb5cdbb1458466797bb8 Changed the behavior of the MAX_FILTER_SIZE filter list. Closes #38 diff --git a/README.md b/README.md --- a/README.md +++ b/README.md @@ -361,8 +361,8 @@ MAX_FILTER_SIZE The max number of items acceptable in the admin list filters. If the -number of items exceeds this, then the filter is removed (just shows -all). This is used by the "Filter by impersonator" filter. +number of items exceeds this, then the filter list is the size of the settings +value. This is used by the "Filter by impersonator" filter. It is optional, and defaults to 100. diff --git a/impersonate/admin.py b/impersonate/admin.py --- a/impersonate/admin.py +++ b/impersonate/admin.py @@ -80,22 +80,23 @@ .order_by() .values_list('impersonator_id', flat=True,) .distinct('impersonator_id') - ) + )[:MAX_FILTER_SIZE] except (NotSupportedError, NotImplementedError): # Unit tests use sqlite db backend which doesn't support distinct. - qs = model_admin.get_queryset(request).only('impersonator_id') + qs = model_admin.get_queryset(request).only('impersonator_id')[ + :MAX_FILTER_SIZE + ] ids = set([x.impersonator_id for x in qs]) if len(ids) > MAX_FILTER_SIZE: - logger.debug( + logger.info( ( - 'Hiding admin list filter as number of impersonators [{0}] ' - 'exceeds MAX_FILTER_SIZE [{1}]' + 'Limiting admin list filter as number of impersonators ' + '[{0}] exceeds MAX_FILTER_SIZE [{1}]' ).format( len(ids), MAX_FILTER_SIZE, ) ) - return impersonators = User.objects.filter(id__in=ids).order_by( User.USERNAME_FIELD diff --git a/impersonate/tests.py b/impersonate/tests.py --- a/impersonate/tests.py +++ b/impersonate/tests.py @@ -720,7 +720,7 @@ opts = [ (_id, name) for _id, name in _filter.lookups(None, model_admin) ] - self.assertEqual(len(opts), 0) + self.assertEqual(len(opts), 1) def test_impersonatelog_admin_add_delete_permissions(self): model_admin = ImpersonationLogAdmin(ImpersonationLog, AdminSite())