From 0429201ddb9276b5e47a1eb48196f4f9c33eac55 Mon Sep 17 00:00:00 2001 From: Nick Horelik Date: Thu, 24 Sep 2015 22:49:57 +0000 Subject: [PATCH 1/5] Added test for limit=0 when using LimitOffsetPagination --- tests/test_pagination.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 63fdbecae..c0f341cd2 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -481,6 +481,21 @@ class TestLimitOffset: assert content.get('next') == next_url assert content.get('previous') == prev_url + def test_limit_is_zero(self): + """ + A limit of zero should produce an empty queryset without Exceptions and + otherwise favor the defaults. + """ + request = Request(factory.get('/', {'limit': '0', 'offset': 0})) + queryset = self.paginate_queryset(request) + content = self.get_paginated_content(queryset) + context = self.get_html_context() + next_limit = self.pagination.default_limit + next_offset = self.pagination.default_limit + next_url = 'http://testserver/?limit={0}&offset={1}'.format(next_limit, next_offset) + assert queryset == [] + assert content.get('next') == next_url + class TestCursorPagination: """ From 145e2409697835c967cf30c7eeb035100d48939a Mon Sep 17 00:00:00 2001 From: Nick Horelik Date: Thu, 24 Sep 2015 23:09:38 +0000 Subject: [PATCH 2/5] Added page_links check to limit=0 pagination test --- tests/test_pagination.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_pagination.py b/tests/test_pagination.py index c0f341cd2..c262ff0fe 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -495,6 +495,9 @@ class TestLimitOffset: next_url = 'http://testserver/?limit={0}&offset={1}'.format(next_limit, next_offset) assert queryset == [] assert content.get('next') == next_url + assert context.get('page_links')[0] == \ + PageLink('http://testserver/?limit={0}'.format(next_limit), 1, True, False) + class TestCursorPagination: From 08e2071d0213cf376fcea128c9f464cbae107f1b Mon Sep 17 00:00:00 2001 From: Nick Horelik Date: Thu, 24 Sep 2015 23:10:50 +0000 Subject: [PATCH 3/5] Fixed ZeroDivisionError on limit=0 in pagination --- rest_framework/pagination.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index bf72ef4fc..5070dfa22 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -389,39 +389,43 @@ class LimitOffsetPagination(BasePagination): return 0 def get_next_link(self): + limit = self.limit if self.limit > 0 else self.default_limit if self.offset + self.limit >= self.count: return None url = self.request.build_absolute_uri() - url = replace_query_param(url, self.limit_query_param, self.limit) + url = replace_query_param(url, self.limit_query_param, limit) - offset = self.offset + self.limit + offset = self.offset + limit return replace_query_param(url, self.offset_query_param, offset) def get_previous_link(self): + limit = self.limit if self.limit > 0 else self.default_limit if self.offset <= 0: return None url = self.request.build_absolute_uri() - url = replace_query_param(url, self.limit_query_param, self.limit) + url = replace_query_param(url, self.limit_query_param, limit) - if self.offset - self.limit <= 0: + if self.offset - limit <= 0: return remove_query_param(url, self.offset_query_param) - offset = self.offset - self.limit + offset = self.offset - limit return replace_query_param(url, self.offset_query_param, offset) def get_html_context(self): base_url = self.request.build_absolute_uri() - current = _divide_with_ceil(self.offset, self.limit) + 1 + limit = self.limit if self.limit > 0 else self.default_limit + base_url = replace_query_param(base_url, self.limit_query_param, limit) + current = _divide_with_ceil(self.offset, limit) + 1 # The number of pages is a little bit fiddly. # We need to sum both the number of pages from current offset to end # plus the number of pages up to the current offset. # When offset is not strictly divisible by the limit then we may # end up introducing an extra page as an artifact. final = ( - _divide_with_ceil(self.count - self.offset, self.limit) + - _divide_with_ceil(self.offset, self.limit) + _divide_with_ceil(self.count - self.offset, limit) + + _divide_with_ceil(self.offset, limit) ) if final < 1: final = 1 @@ -433,7 +437,7 @@ class LimitOffsetPagination(BasePagination): if page_number == 1: return remove_query_param(base_url, self.offset_query_param) else: - offset = self.offset + ((page_number - current) * self.limit) + offset = self.offset + ((page_number - current) * limit) return replace_query_param(base_url, self.offset_query_param, offset) page_numbers = _get_displayed_page_numbers(current, final) From 6d067bcd6560c2a051b603c90a47780a6178167e Mon Sep 17 00:00:00 2001 From: Nick Horelik Date: Fri, 25 Sep 2015 15:33:11 +0000 Subject: [PATCH 4/5] Changed whitespace to satisfy the linter --- tests/test_pagination.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_pagination.py b/tests/test_pagination.py index c262ff0fe..dbbeee713 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -496,8 +496,7 @@ class TestLimitOffset: assert queryset == [] assert content.get('next') == next_url assert context.get('page_links')[0] == \ - PageLink('http://testserver/?limit={0}'.format(next_limit), 1, True, False) - + PageLink('http://testserver/?limit={0}'.format(next_limit), 1, True, False) class TestCursorPagination: From 1b7351763ca72437acb2d23c8614f1c279c86db3 Mon Sep 17 00:00:00 2001 From: Nick Horelik Date: Fri, 25 Sep 2015 16:01:02 +0000 Subject: [PATCH 5/5] Fixed missing limit swap in get_next_link --- rest_framework/pagination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 5070dfa22..bb7b2471e 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -390,7 +390,7 @@ class LimitOffsetPagination(BasePagination): def get_next_link(self): limit = self.limit if self.limit > 0 else self.default_limit - if self.offset + self.limit >= self.count: + if self.offset + limit >= self.count: return None url = self.request.build_absolute_uri()