From f505dab1f5f26154619ec5c30cabbd8efaad173e Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Thu, 7 Dec 2017 04:04:09 +0800 Subject: leverage query_by_name decorator to simplify name querying name query is case insensitive and partial match Change-Id: I65a6f5d3aa9411559cb9cf27d0fbd7ec46d4b26f Signed-off-by: SerenaFeng --- testapi/opnfv_testapi/handlers/base_handlers.py | 8 +++----- testapi/opnfv_testapi/handlers/pod_handlers.py | 11 +---------- testapi/opnfv_testapi/handlers/project_handlers.py | 11 +---------- 3 files changed, 5 insertions(+), 25 deletions(-) (limited to 'testapi/opnfv_testapi/handlers') diff --git a/testapi/opnfv_testapi/handlers/base_handlers.py b/testapi/opnfv_testapi/handlers/base_handlers.py index a2fdb19..bc91f64 100644 --- a/testapi/opnfv_testapi/handlers/base_handlers.py +++ b/testapi/opnfv_testapi/handlers/base_handlers.py @@ -103,7 +103,8 @@ class GenericApiHandler(web.RequestHandler): @web.asynchronous @gen.coroutine - def _list(self, query=None, res_op=None, *args, **kwargs): + @check.query_by_name + def _list(self, query=None, **kwargs): sort = kwargs.get('sort') page = kwargs.get('page', 0) last = kwargs.get('last', 0) @@ -131,10 +132,7 @@ class GenericApiHandler(web.RequestHandler): cursor = dbapi.db_aggregate(self.table, pipelines) 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) + res = {self.table: data} if page > 0: res.update({ 'pagination': { diff --git a/testapi/opnfv_testapi/handlers/pod_handlers.py b/testapi/opnfv_testapi/handlers/pod_handlers.py index 06a8b17..03c2033 100644 --- a/testapi/opnfv_testapi/handlers/pod_handlers.py +++ b/testapi/opnfv_testapi/handlers/pod_handlers.py @@ -6,7 +6,6 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import re from opnfv_testapi.handlers import base_handlers from opnfv_testapi.models import pod_models @@ -19,14 +18,6 @@ class GenericPodHandler(base_handlers.GenericApiHandler): self.table = 'pods' self.table_cls = pod_models.Pod - def set_query(self): - query = dict() - for k in self.request.query_arguments.keys(): - v = self.get_query_argument(k) - if k == 'name': - query['name'] = re.compile(v, re.IGNORECASE) - return query - class PodCLHandler(GenericPodHandler): @swagger.operation(nickname='listAllPods') @@ -40,7 +31,7 @@ class PodCLHandler(GenericPodHandler): @in name: query @required name: False """ - self._list(query=self.set_query()) + self._list() @swagger.operation(nickname='createPod') def post(self): diff --git a/testapi/opnfv_testapi/handlers/project_handlers.py b/testapi/opnfv_testapi/handlers/project_handlers.py index 3ba1a80..0800ee8 100644 --- a/testapi/opnfv_testapi/handlers/project_handlers.py +++ b/testapi/opnfv_testapi/handlers/project_handlers.py @@ -6,7 +6,6 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import re from opnfv_testapi.handlers import base_handlers from opnfv_testapi.models import project_models @@ -21,14 +20,6 @@ class GenericProjectHandler(base_handlers.GenericApiHandler): self.table = 'projects' self.table_cls = project_models.Project - def set_query(self): - query = dict() - for k in self.request.query_arguments.keys(): - v = self.get_query_argument(k) - if k == 'name': - query['name'] = re.compile(v, re.IGNORECASE) - return query - class ProjectCLHandler(GenericProjectHandler): @swagger.operation(nickname="listProjects") @@ -47,7 +38,7 @@ class ProjectCLHandler(GenericProjectHandler): @in name: query @required name: False """ - self._list(query=self.set_query()) + self._list() @swagger.operation(nickname="createProject") def post(self): -- cgit 1.2.3-korg