From 5939864616d56cf949f0bca348a8e05230b62fe0 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 29 May 2013 09:23:08 -0400 Subject: [PATCH] Fixed #15653 - Error in admin pagination tag. Thanks jcumbo@ for the report and adamzap and nott for the patch. --- .../contrib/admin/templatetags/admin_list.py | 2 +- tests/admin_changelist/tests.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py index 18a45a006f..965352e0f5 100644 --- a/django/contrib/admin/templatetags/admin_list.py +++ b/django/contrib/admin/templatetags/admin_list.py @@ -62,7 +62,7 @@ def pagination(cl): # ON_EACH_SIDE links at either end of the "current page" link. page_range = [] if page_num > (ON_EACH_SIDE + ON_ENDS): - page_range.extend(range(0, ON_EACH_SIDE - 1)) + page_range.extend(range(0, ON_ENDS)) page_range.append(DOT) page_range.extend(range(page_num - ON_EACH_SIDE, page_num + 1)) else: diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index fd433967d3..a69cffd751 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -4,6 +4,7 @@ import datetime from django.contrib import admin from django.contrib.admin.options import IncorrectLookupParameters +from django.contrib.admin.templatetags.admin_list import pagination from django.contrib.admin.views.main import ChangeList, SEARCH_VAR, ALL_VAR from django.contrib.auth.models import User from django.core.urlresolvers import reverse @@ -564,6 +565,44 @@ class ChangeListTests(TestCase): response = m.changelist_view(request) self.assertEqual(response.context_data['cl'].list_filter, ('parent', 'name', 'age')) + def test_pagination_page_range(self): + """ + Regression tests for ticket #15653: ensure the number of pages + generated for changelist views are correct. + """ + # instantiating and setting up ChangeList object + m = GroupAdmin(Group, admin.site) + request = self.factory.get('/group/') + cl = ChangeList(request, Group, m.list_display, + m.list_display_links, m.list_filter, m.date_hierarchy, + m.search_fields, m.list_select_related, m.list_per_page, + m.list_max_show_all, m.list_editable, m) + per_page = cl.list_per_page = 10 + + for page_num, objects_count, expected_page_range in [ + (0, per_page, []), + (0, per_page * 2, range(2)), + (5, per_page * 11, list(range(11))), + (5, per_page * 12, [0, 1, 2, 3, 4, 5, 6, 7, 8, '.', 10, 11]), + (6, per_page * 12, [0, 1, '.', 3, 4, 5, 6, 7, 8, 9, 10, 11]), + (6, per_page * 13, [0, 1, '.', 3, 4, 5, 6, 7, 8, 9, '.', 11, 12]), + ]: + # assuming we have 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, + real_page_range, + ) + + class AdminLogNodeTestCase(TestCase): def test_get_admin_log_templatetag_custom_user(self):