summaryrefslogtreecommitdiffstats
path: root/testapi
diff options
context:
space:
mode:
authorSerena Feng <feng.xiaowei@zte.com.cn>2017-07-19 09:48:06 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-07-19 09:48:06 +0000
commit0c684cf169699a48570cb96c565d40007d4f006f (patch)
tree3378dbcdd1839a59650be732890aabcf62d87aa0 /testapi
parent73fce467d6816c3631b9bd8f2543d0f09ff5076a (diff)
parent09b1180a782e58f9ed82dff6876363d8c7634359 (diff)
Merge "bugfix: pagination raise exception when no records returned"
Diffstat (limited to 'testapi')
-rw-r--r--testapi/opnfv_testapi/resources/handlers.py27
1 files changed, 14 insertions, 13 deletions
diff --git a/testapi/opnfv_testapi/resources/handlers.py b/testapi/opnfv_testapi/resources/handlers.py
index c7fed8f..f23cc57 100644
--- a/testapi/opnfv_testapi/resources/handlers.py
+++ b/testapi/opnfv_testapi/resources/handlers.py
@@ -107,12 +107,15 @@ class GenericApiHandler(web.RequestHandler):
per_page = kwargs.get('per_page', 0)
if query is None:
query = {}
- cursor = self._eval_db(self.table, 'find', query)
- records_count = yield cursor.count()
- total_pages = self._calc_total_pages(records_count,
- last,
- page,
- per_page)
+
+ total_pages = 0
+ if page > 0:
+ cursor = self._eval_db(self.table, 'find', query)
+ records_count = yield cursor.count()
+ total_pages = self._calc_total_pages(records_count,
+ last,
+ page,
+ per_page)
pipelines = self._set_pipelines(query, sort, last, page, per_page)
cursor = self._eval_db(self.table,
'aggregate',
@@ -125,7 +128,7 @@ class GenericApiHandler(web.RequestHandler):
res = {self.table: data}
else:
res = res_op(data, *args)
- if total_pages > 0:
+ if page > 0:
res.update({
'pagination': {
'current_page': kwargs.get('page'),
@@ -140,12 +143,10 @@ class GenericApiHandler(web.RequestHandler):
if (records_count > last) and (last > 0):
records_nr = last
- total_pages = 0
- if page > 0:
- total_pages, remainder = divmod(records_nr, per_page)
- if remainder > 0:
- total_pages += 1
- if page > total_pages:
+ total_pages, remainder = divmod(records_nr, per_page)
+ if remainder > 0:
+ total_pages += 1
+ if page > 1 and page > total_pages:
raises.BadRequest(
'Request page > total_pages [{}]'.format(total_pages))
return total_pages