summaryrefslogtreecommitdiffstats
path: root/utils/test/testapi/opnfv_testapi/resources/handlers.py
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-06-20 17:40:38 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2017-06-20 17:45:42 +0800
commitd96bf32a81e1c109c87013db5799940c0f9d726b (patch)
treebd496f878173b51d1cd298d38dbf1ee8920748c9 /utils/test/testapi/opnfv_testapi/resources/handlers.py
parentb9492a0eaac9838fdfb6b070bcec328387881129 (diff)
support pagination in TestAPI
In this patch, pagination is supported, so you can go through results leveraging: http://testresults.opnfv.org/test/api/v1/results?page=2 Change-Id: Ibe31c787643f27dbb06c4899e713b3c8e716e784 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils/test/testapi/opnfv_testapi/resources/handlers.py')
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/handlers.py26
1 files changed, 22 insertions, 4 deletions
diff --git a/utils/test/testapi/opnfv_testapi/resources/handlers.py b/utils/test/testapi/opnfv_testapi/resources/handlers.py
index 2fc31ca45..42372e837 100644
--- a/utils/test/testapi/opnfv_testapi/resources/handlers.py
+++ b/utils/test/testapi/opnfv_testapi/resources/handlers.py
@@ -104,17 +104,35 @@ class GenericApiHandler(web.RequestHandler):
if query is None:
query = {}
data = []
+ sort = kwargs.get('sort')
+ page = kwargs.get('page')
+ last = kwargs.get('last')
+ per_page = kwargs.get('per_page')
+
cursor = self._eval_db(self.table, 'find', query)
- if 'sort' in kwargs:
- cursor = cursor.sort(kwargs.get('sort'))
- if 'last' in kwargs:
- cursor = cursor.limit(kwargs.get('last'))
+ if sort:
+ cursor = cursor.sort(sort)
+ if last and last != 0:
+ cursor = cursor.limit(last)
+ if page:
+ records_count = yield cursor.count()
+ total_pages, remainder = divmod(records_count, per_page)
+ if remainder > 0:
+ total_pages += 1
+ cursor = cursor.skip((page - 1) * per_page).limit(per_page)
while (yield cursor.fetch_next):
data.append(self.format_data(cursor.next_object()))
if res_op is None:
res = {self.table: data}
else:
res = res_op(data, *args)
+ if page:
+ res.update({
+ 'pagination': {
+ 'current_page': page,
+ 'total_pages': total_pages
+ }
+ })
self.finish_request(res)
@web.asynchronous