Mercurial > p > roundup > code
diff roundup/rest.py @ 5870:5ae426616576
Implement pagination in REST API via limit/offset
| author | Ralf Schlatterbeck <rsc@runtux.com> |
|---|---|
| date | Mon, 26 Aug 2019 21:44:48 +0200 |
| parents | 04deafac71ab |
| children | 1b91e3df3fd0 |
line wrap: on
line diff
--- a/roundup/rest.py Mon Aug 26 20:23:53 2019 +0200 +++ b/roundup/rest.py Mon Aug 26 21:44:48 2019 +0200 @@ -727,9 +727,14 @@ else: filter_props[key] = value l = [filter_props] + kw = {} if sort: l.append(sort) - obj_list = class_obj.filter(None, *l) + if page ['size'] is not None and page ['size'] > 0: + kw ['limit'] = page ['size'] + if page ['index'] is not None and page ['index'] > 1: + kw ['offset'] = (page ['index'] - 1) * page ['size'] + obj_list = class_obj.filter(None, *l, **kw) # Note: We don't sort explicitly in python. The filter implementation # of the DB already sorts by ID if no sort option was given. @@ -761,15 +766,12 @@ result_len = len(result['collection']) # pagination - page_index from 1...N - if page['size'] is not None: - page_start = max((page['index']-1) * page['size'], 0) - page_end = min(page_start + page['size'], result_len) - result['collection'] = result['collection'][page_start:page_end] + if page['size'] is not None and page['size'] > 0: result['@links'] = {} for rel in ('next', 'prev', 'self'): if rel == 'next': # if current index includes all data, continue - if page['index']*page['size'] > result_len: continue + if page['size'] > result_len: continue index=page['index']+1 if rel == 'prev': if page['index'] <= 1: continue
