diff options
author | 2017-06-22 02:19:20 +0000 | |
---|---|---|
committer | 2017-06-22 02:19:20 +0000 | |
commit | b7644e8f1f43b762bab51d8bc7a68298558a6bdf (patch) | |
tree | 4cd24ba34647c32cef89e2a0391be3818ed7af0a /utils/test/testapi/opnfv_testapi/resources | |
parent | e9383ded0d640e372d29c3d191ef68868ea0aa02 (diff) | |
parent | d96bf32a81e1c109c87013db5799940c0f9d726b (diff) |
Merge "support pagination in TestAPI"
Diffstat (limited to 'utils/test/testapi/opnfv_testapi/resources')
-rw-r--r-- | utils/test/testapi/opnfv_testapi/resources/handlers.py | 26 | ||||
-rw-r--r-- | utils/test/testapi/opnfv_testapi/resources/result_handlers.py | 21 |
2 files changed, 37 insertions, 10 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 diff --git a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py index 824a89e58..208af6da2 100644 --- a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py @@ -11,12 +11,15 @@ from datetime import timedelta from bson import objectid +from opnfv_testapi.common import config from opnfv_testapi.common import message from opnfv_testapi.common import raises from opnfv_testapi.resources import handlers from opnfv_testapi.resources import result_models from opnfv_testapi.tornado_swagger import swagger +CONF = config.Config() + class GenericResultHandler(handlers.GenericApiHandler): def __init__(self, application, request, **kwargs): @@ -135,22 +138,28 @@ class ResultsCLHandler(GenericResultHandler): @type last: L{string} @in last: query @required last: False + @param page: which page to list + @type page: L{int} + @in page: query + @required page: False @param trust_indicator: must be float @type trust_indicator: L{float} @in trust_indicator: query @required trust_indicator: False """ + limitations = {'sort': [('start_date', -1)]} last = self.get_query_argument('last', 0) if last is not None: last = self.get_int('last', last) + limitations.update({'last': last}) - page = self.get_query_argument('page', 0) - if page: - last = 20 + page = self.get_query_argument('page', 1) + if page is not None: + page = self.get_int('page', page) + limitations.update({'page': page, + 'per_page': CONF.api_results_per_page}) - self._list(query=self.set_query(), - sort=[('start_date', -1)], - last=last) + self._list(query=self.set_query(), **limitations) @swagger.operation(nickname="createTestResult") def post(self): |