summaryrefslogtreecommitdiffstats
path: root/testapi
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-07-19 17:18:44 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2017-07-19 17:25:25 +0800
commit09b1180a782e58f9ed82dff6876363d8c7634359 (patch)
tree224e7bf1ea22ccc734e7d008e5f5e6189122af27 /testapi
parentbba00ca773c4851de91ab19d34859b930b9e1960 (diff)
bugfix: pagination raise exception when no records returned
if no records returned, it will raise 'Request page > total_pages' error. this patch is submitted to fix this JIRA: RELENG-281 Change-Id: I447247c55c64674d44d21d6f4ac8bb3ef725cfb0 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
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