From 4e8ac7d35af1ade2781406c678dbf8f5908737c6 Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Sat, 2 Jan 2016 08:10:58 +0300 Subject: [PATCH] Fix window exceeding page range Resolve issue #33 --- .../templatetags/pagination_tags.py | 2 +- linaro_django_pagination/tests/test_main.py | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/linaro_django_pagination/templatetags/pagination_tags.py b/linaro_django_pagination/templatetags/pagination_tags.py index 167f13a..f106942 100644 --- a/linaro_django_pagination/templatetags/pagination_tags.py +++ b/linaro_django_pagination/templatetags/pagination_tags.py @@ -284,7 +284,7 @@ def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN): window_end = window_end - window_start window_start = 0 if window_end > paginator.num_pages: - window_start = window_start - (window_end - paginator.num_pages) + window_start = max(0, window_start - (window_end - paginator.num_pages)) window_end = paginator.num_pages pages = page_range[window_start:window_end] diff --git a/linaro_django_pagination/tests/test_main.py b/linaro_django_pagination/tests/test_main.py index 1dc6d2c..2409b0f 100644 --- a/linaro_django_pagination/tests/test_main.py +++ b/linaro_django_pagination/tests/test_main.py @@ -209,6 +209,38 @@ class ZeroWindowZeroMarginTestCase(SimpleTestCase): ) +class NoEllipsisTestCase(SimpleTestCase): + """ + Tests a case where should be no any ellipsis pages. + """ + def setUp(self): + self.p = Paginator(range(100), 25) + + def test_on_start(self): + self.assertListEqual( + paginate({'paginator': self.p, 'page_obj': self.p.page(1)}, 2, 0)['pages'], + [1, 2, 3, 4], + ) + + def test_in_middle_page_2(self): + self.assertListEqual( + paginate({'paginator': self.p, 'page_obj': self.p.page(2)}, 2, 0)['pages'], + [1, 2, 3, 4], + ) + + def test_in_middle_page_3(self): + self.assertListEqual( + paginate({'paginator': self.p, 'page_obj': self.p.page(3)}, 2, 0)['pages'], + [1, 2, 3, 4], + ) + + def test_on_end(self): + self.assertListEqual( + paginate({'paginator': self.p, 'page_obj': self.p.page(4)}, 2, 0)['pages'], + [1, 2, 3, 4], + ) + + class SpecialTestCase(SimpleTestCase): def test_middle_with_no_window_and_margin_1(self): p = Paginator(range(31), 2) -- 2.20.1