Skip to content

Commit ed13653

Browse files
committed
Implement pagination in REST API via limit/offset
1 parent 26e9399 commit ed13653

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

roundup/rest.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,14 @@ def get_collection(self, class_name, input):
727727
else:
728728
filter_props[key] = value
729729
l = [filter_props]
730+
kw = {}
730731
if sort:
731732
l.append(sort)
732-
obj_list = class_obj.filter(None, *l)
733+
if page ['size'] is not None and page ['size'] > 0:
734+
kw ['limit'] = page ['size']
735+
if page ['index'] is not None and page ['index'] > 1:
736+
kw ['offset'] = (page ['index'] - 1) * page ['size']
737+
obj_list = class_obj.filter(None, *l, **kw)
733738

734739
# Note: We don't sort explicitly in python. The filter implementation
735740
# of the DB already sorts by ID if no sort option was given.
@@ -761,15 +766,12 @@ def get_collection(self, class_name, input):
761766
result_len = len(result['collection'])
762767

763768
# pagination - page_index from 1...N
764-
if page['size'] is not None:
765-
page_start = max((page['index']-1) * page['size'], 0)
766-
page_end = min(page_start + page['size'], result_len)
767-
result['collection'] = result['collection'][page_start:page_end]
769+
if page['size'] is not None and page['size'] > 0:
768770
result['@links'] = {}
769771
for rel in ('next', 'prev', 'self'):
770772
if rel == 'next':
771773
# if current index includes all data, continue
772-
if page['index']*page['size'] > result_len: continue
774+
if page['size'] > result_len: continue
773775
index=page['index']+1
774776
if rel == 'prev':
775777
if page['index'] <= 1: continue

0 commit comments

Comments
 (0)