Skip to content

Commit

Permalink
Merge pull request #45 from cloudblue/opt/LITE-22313
Browse files Browse the repository at this point in the history
LITE-22313 Optimized RQLLimitOffsetPagination to use only possible limit
  • Loading branch information
maxipavlovic authored Feb 25, 2022
2 parents 74702f4 + 367d6c4 commit 00f5703
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 18 additions & 2 deletions dj_rql/drf/paginations.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,28 @@ def paginate_queryset(self, queryset, request, view=None):
'error': 'Limit and offset are set incorrectly.',
})

if self.get_limit() == 0:
self.limit = self.get_limit(request)
if self.limit == 0:
self.count = self.get_count(queryset)
self.offset = 0
return []

return super(RQLLimitOffsetPagination, self).paginate_queryset(queryset, request, view)
elif self.limit is None:
return None

self.count = self.get_count(queryset)
self.offset = self.get_offset(request)
self.request = request
if self.count > self.limit and self.template is not None:
self.display_page_controls = True

if self.count == 0 or self.offset > self.count:
return []

if self.limit + self.offset > self.count:
self.limit = self.count - self.offset

return list(queryset[self.offset:self.offset + self.limit])

def get_limit(self, *args):
if self._rql_limit is not None:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_drf/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ def test_max_limit(self):
queryset = self.paginate_queryset(request)
assert queryset == list(range(51, 66))

def test_limit_gt_than_count_wout_offset(self):
self.pagination.max_limit = 500

request = Request(factory.get('/', {'limit': 125}))
queryset = self.paginate_queryset(request)
assert queryset == list(range(1, 101))

def test_limit_gt_than_count_with_offset(self):
request = Request(factory.get('/', {'limit': 125, 'offset': 95}))
queryset = self.paginate_queryset(request)
assert queryset == list(range(96, 101))

def test_rql_operators(self):
limit, offset = 1, 2
request = Request(
Expand Down

0 comments on commit 00f5703

Please sign in to comment.