From 0ca18aa6187bef39a1e98451f69930ff881c04c0 Mon Sep 17 00:00:00 2001 From: Harsha Kethineni Date: Mon, 31 Jul 2017 15:32:56 -0500 Subject: [PATCH 1/3] groups join=projects parameter --- api/dao/base.py | 9 +++++++-- api/handlers/grouphandler.py | 11 +++++++++++ test/integration_tests/python/test_groups.py | 6 ++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/api/dao/base.py b/api/dao/base.py index b0ac55177..8752b822e 100644 --- a/api/dao/base.py +++ b/api/dao/base.py @@ -80,12 +80,17 @@ def get_container(self, _id, projection=None, get_children=False): cont[CHILD_MAP[self.cont_name]] = children return cont - def get_children(self, _id, projection=None): + def get_children(self, _id, projection=None, uid=None): try: child_name = CHILD_MAP[self.cont_name] except KeyError: raise APINotFoundException('Children cannot be listed from the {0} level'.format(self.cont_name)) - query = {self.cont_name[:-1]: bson.ObjectId(_id)} + if self.cont_name == 'groups': + query = {self.cont_name[:-1]: _id} + if uid: + query['permissions'] = {'$elemMatch': {'_id': uid}} + else: + query = {self.cont_name[:-1]: bson.ObjectId(_id)} if not projection: projection = {'info': 0, 'files.info': 0, 'subject': 0, 'tags': 0} return ContainerStorage.factory(child_name).get_all_el(query, None, projection) diff --git a/api/handlers/grouphandler.py b/api/handlers/grouphandler.py index b908b7204..f20f5637e 100644 --- a/api/handlers/grouphandler.py +++ b/api/handlers/grouphandler.py @@ -36,6 +36,14 @@ def delete(self, _id): self.abort(404, 'Group {} not removed'.format(_id)) return result + def handle_projects(self, result): + """ + If parameter join=projects, return project id, label, permissions, and description + """ + result['projects'] = self.storage.get_children(result.get('_id'), projection={'permissions': 1, 'label': 1, 'description': 1}, uid=self.uid) + self._filter_permissions(result['projects'], self.uid) + return result + def get_all(self, uid=None): projection = {'label': 1, 'created': 1, 'modified': 1, 'permissions': 1, 'tags': 1} permchecker = groupauth.list_permission_checker(self, uid) @@ -44,6 +52,9 @@ def get_all(self, uid=None): self._filter_permissions(results, self.uid) if self.is_true('join_avatars'): results = ContainerHandler.join_user_info(results) + if 'projects' in self.request.params.getall('join'): + for result in results: + result = self.handle_projects(result) return results @validators.verify_payload_exists diff --git a/test/integration_tests/python/test_groups.py b/test/integration_tests/python/test_groups.py index 30433bab4..42c457fb3 100644 --- a/test/integration_tests/python/test_groups.py +++ b/test/integration_tests/python/test_groups.py @@ -118,3 +118,9 @@ def test_groups(as_user, as_admin, data_builder): r = as_admin.get('/groups/' + group) assert r.ok assert r.json()['label'] == group_label + + # Test join=projects + project = data_builder.create_project() + r = as_admin.get('/groups', params={'join': 'projects'}) + assert r.ok + assert r.json()[1].get('projects')[0].get('_id') == project From 1aaaad55f65b58c0bdfc8cfb1f5a9e01fe5a199a Mon Sep 17 00:00:00 2001 From: Harsha Kethineni Date: Mon, 31 Jul 2017 16:19:17 -0500 Subject: [PATCH 2/3] group returned for each project as well --- api/handlers/grouphandler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/handlers/grouphandler.py b/api/handlers/grouphandler.py index f20f5637e..2b55f9fd8 100644 --- a/api/handlers/grouphandler.py +++ b/api/handlers/grouphandler.py @@ -40,7 +40,7 @@ def handle_projects(self, result): """ If parameter join=projects, return project id, label, permissions, and description """ - result['projects'] = self.storage.get_children(result.get('_id'), projection={'permissions': 1, 'label': 1, 'description': 1}, uid=self.uid) + result['projects'] = self.storage.get_children(result.get('_id'), projection={'permissions': 1, 'label': 1, 'description': 1, 'group': 1}, uid=self.uid) self._filter_permissions(result['projects'], self.uid) return result From 2b32ef0a9d6aa11edfce8e4d99d2880cf9bb2c0a Mon Sep 17 00:00:00 2001 From: Harsha Kethineni Date: Thu, 3 Aug 2017 10:57:08 -0500 Subject: [PATCH 3/3] remove singlurize hack --- api/dao/base.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/api/dao/base.py b/api/dao/base.py index 8752b822e..5d01b2d2a 100644 --- a/api/dao/base.py +++ b/api/dao/base.py @@ -85,12 +85,13 @@ def get_children(self, _id, projection=None, uid=None): child_name = CHILD_MAP[self.cont_name] except KeyError: raise APINotFoundException('Children cannot be listed from the {0} level'.format(self.cont_name)) - if self.cont_name == 'groups': - query = {self.cont_name[:-1]: _id} - if uid: - query['permissions'] = {'$elemMatch': {'_id': uid}} + if not self.use_object_id: + query = {containerutil.singularize(self.cont_name): _id} else: - query = {self.cont_name[:-1]: bson.ObjectId(_id)} + query = {containerutil.singularize(self.cont_name): bson.ObjectId(_id)} + + if uid: + query['permissions'] = {'$elemMatch': {'_id': uid}} if not projection: projection = {'info': 0, 'files.info': 0, 'subject': 0, 'tags': 0} return ContainerStorage.factory(child_name).get_all_el(query, None, projection)