summaryrefslogtreecommitdiffstats
path: root/utils/test/testapi/opnfv_testapi/resources
diff options
context:
space:
mode:
Diffstat (limited to 'utils/test/testapi/opnfv_testapi/resources')
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/handlers.py36
-rw-r--r--utils/test/testapi/opnfv_testapi/resources/result_handlers.py35
2 files changed, 28 insertions, 43 deletions
diff --git a/utils/test/testapi/opnfv_testapi/resources/handlers.py b/utils/test/testapi/opnfv_testapi/resources/handlers.py
index f23cc57ee..8a3a2db38 100644
--- a/utils/test/testapi/opnfv_testapi/resources/handlers.py
+++ b/utils/test/testapi/opnfv_testapi/resources/handlers.py
@@ -20,8 +20,8 @@
# feng.xiaowei@zte.com.cn remove DashboardHandler 5-30-2016
##############################################################################
-from datetime import datetime
import json
+from datetime import datetime
from tornado import gen
from tornado import web
@@ -29,6 +29,7 @@ from tornado import web
from opnfv_testapi.common import check
from opnfv_testapi.common import message
from opnfv_testapi.common import raises
+from opnfv_testapi.db import api as dbapi
from opnfv_testapi.resources import models
from opnfv_testapi.tornado_swagger import swagger
@@ -38,7 +39,6 @@ DEFAULT_REPRESENTATION = "application/json"
class GenericApiHandler(web.RequestHandler):
def __init__(self, application, request, **kwargs):
super(GenericApiHandler, self).__init__(application, request, **kwargs)
- self.db = self.settings["db"]
self.json_args = None
self.table = None
self.table_cls = None
@@ -90,8 +90,7 @@ class GenericApiHandler(web.RequestHandler):
if self.table != 'results':
data.creation_date = datetime.now()
- _id = yield self._eval_db(self.table, 'insert', data.format(),
- check_keys=False)
+ _id = yield dbapi.db_save(self.table, data.format())
if 'name' in self.json_args:
resource = data.name
else:
@@ -110,17 +109,14 @@ class GenericApiHandler(web.RequestHandler):
total_pages = 0
if page > 0:
- cursor = self._eval_db(self.table, 'find', query)
+ cursor = dbapi.db_list(self.table, 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',
- pipelines,
- allowDiskUse=True)
+ cursor = dbapi.db_aggregate(self.table, pipelines)
data = list()
while (yield cursor.fetch_next):
data.append(self.format_data(cursor.next_object()))
@@ -176,7 +172,7 @@ class GenericApiHandler(web.RequestHandler):
@check.authenticate
@check.not_exist
def _delete(self, data, query=None):
- yield self._eval_db(self.table, 'remove', query)
+ yield dbapi.db_delete(self.table, query)
self.finish_request()
@check.authenticate
@@ -186,8 +182,7 @@ class GenericApiHandler(web.RequestHandler):
def _update(self, data, query=None, **kwargs):
data = self.table_cls.from_dict(data)
update_req = self._update_requests(data)
- yield self._eval_db(self.table, 'update', query, update_req,
- check_keys=False)
+ yield dbapi.db_update(self.table, query, update_req)
update_req['_id'] = str(data._id)
self.finish_request(update_req)
@@ -230,23 +225,6 @@ class GenericApiHandler(web.RequestHandler):
query[key] = new
return query if not equal else dict()
- def _eval_db(self, table, method, *args, **kwargs):
- exec_collection = self.db.__getattr__(table)
- return exec_collection.__getattribute__(method)(*args, **kwargs)
-
- def _eval_db_find_one(self, query, table=None):
- if table is None:
- table = self.table
- return self._eval_db(table, 'find_one', query)
-
- def db_save(self, collection, data):
- self._eval_db(collection, 'insert', data, check_keys=False)
-
- def db_find_one(self, query, collection=None):
- if not collection:
- collection = self.table
- return self._eval_db(collection, 'find_one', query)
-
class VersionHandler(GenericApiHandler):
@swagger.operation(nickname='listAllVersions')
diff --git a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py
index 5eb1b925c..2bf1792f2 100644
--- a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py
+++ b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py
@@ -13,7 +13,7 @@ import json
from bson import objectid
-from opnfv_testapi.common import config
+from opnfv_testapi.common.config import CONF
from opnfv_testapi.common import message
from opnfv_testapi.common import raises
from opnfv_testapi.resources import handlers
@@ -21,8 +21,6 @@ from opnfv_testapi.resources import result_models
from opnfv_testapi.tornado_swagger import swagger
from opnfv_testapi.ui.auth import constants as auth_const
-CONF = config.Config()
-
class GenericResultHandler(handlers.GenericApiHandler):
def __init__(self, application, request, **kwargs):
@@ -68,7 +66,7 @@ class GenericResultHandler(handlers.GenericApiHandler):
del query['public']
if role != "reviewer":
query['user'] = openid
- elif k != 'last' and k != 'page':
+ elif k not in ['last', 'page', 'descend']:
query[k] = v
if date_range:
query['start_date'] = date_range
@@ -169,18 +167,27 @@ class ResultsCLHandler(GenericResultHandler):
@type signed: L{string}
@in signed: query
@required signed: False
+ @param descend: true, newest2oldest; false, oldest2newest
+ @type descend: L{string}
+ @in descend: query
+ @required descend: False
"""
- limitations = {'sort': {'_id': -1}}
- last = self.get_query_argument('last', 0)
- if last is not None:
- last = self.get_int('last', last)
- limitations.update({'last': last})
+ def descend_limit():
+ descend = self.get_query_argument('descend', 'true')
+ return -1 if descend.lower() == 'true' else 1
+
+ def last_limit():
+ return self.get_int('last', self.get_query_argument('last', 0))
+
+ def page_limit():
+ return self.get_int('page', self.get_query_argument('page', 0))
- page = self.get_query_argument('page', None)
- if page is not None:
- page = self.get_int('page', page)
- limitations.update({'page': page,
- 'per_page': CONF.api_results_per_page})
+ limitations = {
+ 'sort': {'_id': descend_limit()},
+ 'last': last_limit(),
+ 'page': page_limit(),
+ 'per_page': CONF.api_results_per_page
+ }
self._list(query=self.set_query(), **limitations)