From 54bc0f28fd9b5ec6675401d6c3322b30e72e7d9c Mon Sep 17 00:00:00 2001 From: grakiss Date: Wed, 18 Oct 2017 22:15:32 -0400 Subject: [web-cvp]share result by email JIRA: DOVETAIL-529 It may be easier to get other people's email than his user id Change-Id: Ia5f9aa72d3c79bd3f5e69dad98b3f188cdaafc3d Signed-off-by: grakiss --- .../components/results/modal/sharedModal.html | 2 +- .../testapi-ui/components/results/results.html | 2 +- .../resources/application_handlers.py | 5 ++- cvp/opnfv_testapi/resources/handlers.py | 14 +++++--- cvp/opnfv_testapi/resources/test_handlers.py | 37 ++++++++++++++++------ 5 files changed, 44 insertions(+), 16 deletions(-) (limited to 'cvp') diff --git a/cvp/3rd_party/static/testapi-ui/components/results/modal/sharedModal.html b/cvp/3rd_party/static/testapi-ui/components/results/modal/sharedModal.html index de3b125d..2f50b0ce 100644 --- a/cvp/3rd_party/static/testapi-ui/components/results/modal/sharedModal.html +++ b/cvp/3rd_party/static/testapi-ui/components/results/modal/sharedModal.html @@ -1,6 +1,6 @@
-

Enter user name

+

Enter user name or email

diff --git a/cvp/3rd_party/static/testapi-ui/components/results/results.html b/cvp/3rd_party/static/testapi-ui/components/results/results.html index 18de5ea0..5d6979da 100644 --- a/cvp/3rd_party/static/testapi-ui/components/results/results.html +++ b/cvp/3rd_party/static/testapi-ui/components/results/results.html @@ -54,7 +54,7 @@ Share List -
diff --git a/cvp/opnfv_testapi/resources/application_handlers.py b/cvp/opnfv_testapi/resources/application_handlers.py index 16dae4f3..6986b9ef 100644 --- a/cvp/opnfv_testapi/resources/application_handlers.py +++ b/cvp/opnfv_testapi/resources/application_handlers.py @@ -31,6 +31,8 @@ class GenericApplicationHandler(handlers.GenericApiHandler): class ApplicationsCLHandler(GenericApplicationHandler): @swagger.operation(nickname="queryApplications") + @web.asynchronous + @gen.coroutine def get(self): """ @description: Retrieve result(s) for a application project @@ -64,7 +66,8 @@ class ApplicationsCLHandler(GenericApplicationHandler): 'per_page': CONF.api_results_per_page } - self._list(query=self.set_query(), **limitations) + query = yield self.set_query() + yield self._list(query=query, **limitations) logging.debug('list end') @swagger.operation(nickname="createApplication") diff --git a/cvp/opnfv_testapi/resources/handlers.py b/cvp/opnfv_testapi/resources/handlers.py index 17a7e359..51092b94 100644 --- a/cvp/opnfv_testapi/resources/handlers.py +++ b/cvp/opnfv_testapi/resources/handlers.py @@ -59,6 +59,7 @@ class GenericApiHandler(web.RequestHandler): raises.BadRequest(message.must_int(key)) return value + @gen.coroutine def set_query(self): query = dict() date_range = dict() @@ -76,13 +77,17 @@ class GenericApiHandler(web.RequestHandler): date_range.update({'$lt': str(v)}) elif k == 'signed': openid = self.get_secure_cookie(auth_const.OPENID) + user = yield dbapi.db_find_one("users", {'openid': openid}) role = self.get_secure_cookie(auth_const.ROLE) logging.info('role:%s', role) if role: if role.find("reviewer") != -1: query['$or'] = [{"shared": - {"$elemMatch": {"$eq": openid}} - }, {"owner": openid}, + {"$elemMatch": {"$eq": openid}}}, + {"owner": openid}, + {"shared": {"$elemMatch": + {"$eq": + user.get("email")}}}, {"status": {"$ne": "private"}}] else: query['$or'] = [{"shared": @@ -99,7 +104,7 @@ class GenericApiHandler(web.RequestHandler): query['start_date'].update({'$lt': str(datetime.now())}) logging.debug("query:%s", query) - return query + raise gen.Return((query)) def prepare(self): if self.request.method != "GET" and self.request.method != "DELETE": @@ -178,9 +183,10 @@ class GenericApiHandler(web.RequestHandler): raise gen.Return((True, 'Data alreay exists. %s' % (query))) raise gen.Return((False, 'Data does not exist. %s' % (query))) - @web.asynchronous + # @web.asynchronous @gen.coroutine def _list(self, query=None, res_op=None, *args, **kwargs): + logging.debug("_list query:%s", query) sort = kwargs.get('sort') page = kwargs.get('page', 0) last = kwargs.get('last', 0) diff --git a/cvp/opnfv_testapi/resources/test_handlers.py b/cvp/opnfv_testapi/resources/test_handlers.py index 9ad1bbc2..2cbfbf10 100644 --- a/cvp/opnfv_testapi/resources/test_handlers.py +++ b/cvp/opnfv_testapi/resources/test_handlers.py @@ -33,6 +33,8 @@ class GenericTestHandler(handlers.GenericApiHandler): class TestsCLHandler(GenericTestHandler): @swagger.operation(nickname="queryTests") + @web.asynchronous + @gen.coroutine def get(self): """ @description: Retrieve result(s) for a test project @@ -68,7 +70,8 @@ class TestsCLHandler(GenericTestHandler): 'per_page': CONF.api_results_per_page } - self._list(query=self.set_query(), **limitations) + query = yield self.set_query() + yield self._list(query=query, **limitations) logging.debug('list end') @swagger.operation(nickname="createTest") @@ -140,17 +143,28 @@ class TestsGURHandler(GenericTestHandler): logging.error('except:%s', e) return + @gen.coroutine + def _convert_to_id(self, email): + query = {"email": email} + table = "users" + if query and table: + data = yield dbapi.db_find_one(table, query) + if data: + raise gen.Return((True, 'Data alreay exists. %s' % (query), + data.get("openid"))) + raise gen.Return((False, 'Data does not exist. %s' % (query), None)) + @gen.coroutine def update(self, test_id, item, value): logging.debug("update") if item == "shared": - if len(value) != len(set(value)): - msg = "Already shared with this user" - self.finish_request({'code': '403', 'msg': msg}) - return - + new_list = [] for user in value: - query = {"openid": user} + ret, msg, user_id = yield self._convert_to_id(user) + if ret: + user = user_id + new_list.append(user) + query = {"$or": [{"openid": user}, {"email": user}]} table = "users" ret, msg = yield self._check_if_exists(table=table, query=query) @@ -159,6 +173,11 @@ class TestsGURHandler(GenericTestHandler): self.finish_request({'code': '403', 'msg': msg}) return + if len(new_list) != len(set(new_list)): + msg = "Already shared with this user" + self.finish_request({'code': '403', 'msg': msg}) + return + logging.debug("before _update") self.json_args = {} self.json_args[item] = value @@ -169,9 +188,9 @@ class TestsGURHandler(GenericTestHandler): query = {'id': test_id} db_keys = ['id', ] - user = self.get_secure_cookie(auth_const.OPENID) + curr_user = self.get_secure_cookie(auth_const.OPENID) if item == "shared": - query['owner'] = user + query['owner'] = curr_user db_keys.append('owner') logging.debug("before _update 2") self._update(query=query, db_keys=db_keys) -- cgit 1.2.3-korg