summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-06-28 14:10:45 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2017-06-28 14:14:29 +0800
commit70632d045345ae9f2de0f259f9280e95909741f0 (patch)
tree96714c04b79d66f29de9fd1e3f52fe1320895f03
parenta42e6289c90531df6a45293411ae353cd1e251f6 (diff)
refactor list operation
all operations mix together make it very difficult to read Change-Id: I7e4a12fd13a7aa0dc0c8fc09b69c7e43a2ab18ac Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/handlers.py68
1 files changed, 42 insertions, 26 deletions
diff --git a/utils/test/testapi/opnfv_testapi/resources/handlers.py b/utils/test/testapi/opnfv_testapi/resources/handlers.py
index 5c98c48ef..c7fed8f17 100644
--- a/utils/test/testapi/opnfv_testapi/resources/handlers.py
+++ b/utils/test/testapi/opnfv_testapi/resources/handlers.py
@@ -101,55 +101,71 @@ class GenericApiHandler(web.RequestHandler):
@web.asynchronous
@gen.coroutine
def _list(self, query=None, res_op=None, *args, **kwargs):
- if query is None:
- query = {}
- data = []
sort = kwargs.get('sort')
page = kwargs.get('page', 0)
last = kwargs.get('last', 0)
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()
- records_nr = records_count
- if (records_count > last) and (last > 0):
- records_nr = last
-
- pipelines = list()
- if query:
- pipelines.append({'$match': query})
- if sort:
- pipelines.append({'$sort': sort})
-
- if page > 0:
- total_pages, remainder = divmod(records_nr, per_page)
- if remainder > 0:
- total_pages += 1
- pipelines.append({'$skip': (page - 1) * per_page})
- pipelines.append({'$limit': per_page})
- elif last > 0:
- pipelines.append({'$limit': last})
-
+ 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',
pipelines,
allowDiskUse=True)
-
+ data = list()
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:
+ if total_pages > 0:
res.update({
'pagination': {
- 'current_page': page,
+ 'current_page': kwargs.get('page'),
'total_pages': total_pages
}
})
self.finish_request(res)
+ @staticmethod
+ def _calc_total_pages(records_count, last, page, per_page):
+ records_nr = records_count
+ 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:
+ raises.BadRequest(
+ 'Request page > total_pages [{}]'.format(total_pages))
+ return total_pages
+
+ @staticmethod
+ def _set_pipelines(query, sort, last, page, per_page):
+ pipelines = list()
+ if query:
+ pipelines.append({'$match': query})
+ if sort:
+ pipelines.append({'$sort': sort})
+
+ if page > 0:
+ pipelines.append({'$skip': (page - 1) * per_page})
+ pipelines.append({'$limit': per_page})
+ elif last > 0:
+ pipelines.append({'$limit': last})
+
+ return pipelines
+
@web.asynchronous
@gen.coroutine
@check.not_exist