From f35840c19664fed7b6bc4cf561bf0b6fd1a3b463 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Thu, 30 Jul 2020 23:22:34 +0100 Subject: [PATCH] Refs #25513 -- Fixed admin pagination elision bounds. It doesn't make sense to elide a single page number which could be a clickable link to that page. We only want to elide two or more pages. --- .../contrib/admin/templatetags/admin_list.py | 4 +-- tests/admin_changelist/tests.py | 34 +++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py index 088b85991f..5c8d21d30d 100644 --- a/django/contrib/admin/templatetags/admin_list.py +++ b/django/contrib/admin/templatetags/admin_list.py @@ -67,14 +67,14 @@ def pagination(cl): # links at either end of the list of pages, and there are always # ON_EACH_SIDE links at either end of the "current page" link. page_range = [] - if page_num > (1 + ON_EACH_SIDE + ON_ENDS): + if page_num > (1 + ON_EACH_SIDE + ON_ENDS) + 1: page_range += [ *range(1, ON_ENDS + 1), DOT, *range(page_num - ON_EACH_SIDE, page_num + 1), ] else: page_range.extend(range(1, page_num + 1)) - if page_num < (paginator.num_pages - ON_EACH_SIDE - ON_ENDS): + if page_num < (paginator.num_pages - ON_EACH_SIDE - ON_ENDS) - 1: page_range += [ *range(page_num + 1, page_num + ON_EACH_SIDE + 1), DOT, *range(paginator.num_pages - ON_ENDS + 1, paginator.num_pages + 1) diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index 68319d69d4..bd4d406c10 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -1246,22 +1246,28 @@ class ChangeListTests(TestCase): for page_num, objects_count, expected_page_range in [ (1, per_page, []), - (1, per_page * 2, list(range(1, 3))), - (6, per_page * 11, list(range(1, 12))), - (6, per_page * 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, '.', 11, 12]), - (7, per_page * 12, [1, 2, '.', 4, 5, 6, 7, 8, 9, 10, 11, 12]), - (7, per_page * 13, [1, 2, '.', 4, 5, 6, 7, 8, 9, 10, '.', 12, 13]), + (1, per_page * 2, [1, 2]), + (6, per_page * 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), + (6, per_page * 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), + (6, per_page * 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, '.', 12, 13]), + (7, per_page * 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), + (7, per_page * 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]), + (7, per_page * 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '.', 13, 14]), + (8, per_page * 13, [1, 2, '.', 5, 6, 7, 8, 9, 10, 11, 12, 13]), + (8, per_page * 14, [1, 2, '.', 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]), + (8, per_page * 15, [1, 2, '.', 5, 6, 7, 8, 9, 10, 11, '.', 14, 15]), ]: - # assuming we have exactly `objects_count` objects - Group.objects.all().delete() - for i in range(objects_count): - Group.objects.create(name='test band') + with self.subTest(number=number, pages=pages): + # assuming exactly `objects_count` objects + Group.objects.all().delete() + for i in range(objects_count): + Group.objects.create(name='test band') - # setting page number and calculating page range - cl.page_num = page_num - cl.get_results(request) - real_page_range = pagination(cl)['page_range'] - self.assertEqual(expected_page_range, list(real_page_range)) + # setting page number and calculating page range + cl.page_num = page_num + cl.get_results(request) + real_page_range = pagination(cl)['page_range'] + self.assertEqual(expected_page_range, list(real_page_range)) def test_object_tools_displayed_no_add_permission(self): """