def get(self, resource_id): if not current_token.user_id: return error_response(message='request should be on behalf of a user', code=400) args = parse_json_args(self.get_parser.parse_args(), self.get_payload_json_parse_directives) actions = args['actions'] resource_json = g.objstore_colln.find_one( objstore_helper.resource_selector_doc(resource_id), projection=None ) if not resource_json: return error_response(message='resource not found', code=404) referred_objects_graph = PermissionResolver.get_referred_objects_graph( g.objstore_colln, resource_json, {resource_json['_id']: resource_json}) resolved_permissions = dict( ( action, PermissionResolver.resolve_permission_on_obj_with_referred_graph( resource_json, referred_objects_graph, action, current_token.user_id, current_token.group_ids) ) for action in actions ) return resolved_permissions
def post(self): args = self.post_parser.parse_args() if g.current_user_id: return error_response(message='you are already registered', code=403) user_json = jsonify_argument(args['user_json'], key='user_json') # type: dict check_argument_type(user_json, (dict, ), key='user_json') return_projection = jsonify_argument(args.get('return_projection', None), key='return_projection') check_argument_type(return_projection, (dict, ), key='return_projection', allow_none=True) _validate_projection(return_projection) return_projection = projection_helper.modified_projection( return_projection, mandatory_attrs=['_id', 'jsonClass']) try: new_user_id = users_helper.create_new_user( g.users_colln, user_json, initial_agents=g.initial_agents) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) except ValidationError as e: return error_response(message='invalid schema for user_json', code=403, details={"error": str(e)}) new_user_json = g.users_colln.get(new_user_id, projection=return_projection) return new_user_json
def get(self, file_id): ool_data = g.objstore_colln.find_one({"_id": file_id}) if ool_data is None: return error_response(message="file not found", code=404) if not PermissionResolver.resolve_permission( ool_data, ObjectPermissions.READ, current_token.user_id, current_token.group_ids, g.objstore_colln): return error_response(message='permission denied', code=403) namespace = ool_data.get('namespace', None) identifier = ool_data.get('identifier', None) url = ool_data.get('url', None) if None in (namespace, identifier, url): return error_response(message='resource is not ool_data', code=404) if namespace == '_vedavaapi': local_namespace_data = ool_data.get('namespaceData', {}) mimetype = local_namespace_data.get('mimetype', None) print({"mimetype": mimetype}, file=sys.stderr) file_path = os.path.join(g.data_dir_path, identifier.lstrip('/')) if not os.path.isfile(file_path): return error_response(message='no representation available', code=404) file_dir = os.path.dirname(file_path) file_name = os.path.basename(file_path) from flask import send_from_directory return send_from_directory( directory=file_dir, filename=file_name, mimetype=mimetype ) else: return flask.redirect(url)
def get(self, user_id): args = self.get_parser.parse_args() user_selector_doc = users_helper.get_user_selector_doc(_id=user_id) projection = jsonify_argument(args.get('projection', None), key='projection') check_argument_type(projection, (dict, ), key='projection', allow_none=True) _validate_projection(projection) projection = projection_helper.modified_projection( projection, mandatory_attrs=["_id", "jsonClass"]) user = JsonObject.make_from_dict( g.users_colln.find_one(user_selector_doc, projection=None)) if user is None: return error_response(message='user not found', code=404) if not PermissionResolver.resolve_permission( user, ObjectPermissions.READ, current_token.user_id, current_token.group_ids, g.users_colln): return error_response(message='permission denied', code=403) user_json = user.to_json_map() projected_user_json = users_helper.project_user_json( user_json, projection=projection) return projected_user_json
def delete(self, agent_id): if not current_token.user_id: return error_response(message='not authorized', code=401) args = self.post_parser.parse_args() actions = jsonify_argument(args.get('actions'), key='actions') check_argument_type(actions, (list, ), key='actions') user_ids = jsonify_argument(args.get('user_ids', None), key='user_ids') or [] check_argument_type(user_ids, (list, ), key='user_ids') group_ids = jsonify_argument(args.get('group_ids', None), key='group_ids') or [] check_argument_type(group_ids, (list, ), key='group_ids') try: objstore_helper.remove_from_permissions_agent_set( g.users_colln, agent_id, current_token.user_id, current_token.group_ids, actions, args['agents_set_name'], user_ids=user_ids, group_ids=group_ids) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) agent_json = g.users_colln.get(agent_id, projection={"permissions": 1}) agent_permissions = agent_json['permissions'] return agent_permissions
def get(self, provider_name): if not (current_token.client_id and not current_token.user_id): return error_response( message='invalid operation. resource is not in scope of users.' ) args = self.get_parser.parse_args() oauth_client = get_oauth_client( provider_name, client_name=args.get('oauth_client_name')) # type: OAuthClient if not oauth_client: return error_response( message='currently oauth provider "{}" not supported.'.format( provider_name), code=400) oauth_callback_url = api.url_for(OauthCallback, provider_name=provider_name, _external=True) state = { "vedavaapi_client_id": current_token.client_id, "vedavaapi_access_token": current_token.access_token, "client_callback_url": args.get('callback_url'), "provider_client_name": args.get('oauth_client_name') } return oauth_client.redirect_for_authorization( callback_url=oauth_callback_url, state=json.dumps(state), scope=args.get('scope', None))
def delete(self, group_identifier): args = self.delete_parser.parse_args() identifier_type = args.get('identifier_type', '_id') # noinspection PyProtectedMember group_selector_doc = GroupResource._group_selector_doc( group_identifier, identifier_type) user_ids = jsonify_argument(args['member_ids'], key='member_ids') check_argument_type(user_ids, (list, ), key='member_ids') for user_id in user_ids: if not isinstance(user_id, six.string_types): return error_response(message='invalid user_ids', code=400) try: # noinspection PyUnusedLocal modified_count = groups_helper.remove_users_from_group( g.users_colln, group_selector_doc, user_ids, current_token.user_id, current_token.group_ids) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) member_ids = g.users_colln.find_one(group_selector_doc, projection={ "members": 1 }).get('members', []) return member_ids
def get(self, group_identifier): args = self.get_parser.parse_args() identifier_type = args.get('identifier_type', '_id') group_selector_doc = self._group_selector_doc(group_identifier, identifier_type) projection = jsonify_argument(args.get('projection', None), key='projection') check_argument_type(projection, (dict, ), key='projection', allow_none=True) _validate_projection(projection) projection = projection_helper.modified_projection( projection, mandatory_attrs=["_id", "jsonClass"]) group = JsonObject.make_from_dict( g.users_colln.find_one(group_selector_doc, projection=None)) if group is None: return error_response(message='group not found', code=404) if not PermissionResolver.resolve_permission( group, ObjectPermissions.READ, current_token.user_id, current_token.group_ids, g.users_colln): return error_response(message='permission denied', code=403) group_json = group.to_json_map() projected_group_json = projection_helper.project_doc( group_json, projection) return projected_group_json
def get(self, agent_id): if not current_token.user_id: return error_response( message='request should be on behalf of a user', code=400) args = self.get_parser.parse_args() actions = jsonify_argument(args.get( 'actions', None), key='actions') or list(ObjectPermissions.ACTIONS) check_argument_type(actions, (list, ), key='actions') agent = objstore_helper.get_resource(g.users_colln, agent_id, projection=None) if not agent: return error_response(message='resource not found', code=404) resolved_permissions = dict( (action, PermissionResolver.resolve_permission(agent, action, current_token.user_id, current_token.group_ids, g.users_colln, true_if_none=False)) for action in actions) return resolved_permissions
def post(self): args = self.post_parser.parse_args() args = parse_json_args(args, self.post_json_parse_directives) resource_jsons = args['resource_jsons'] return_projection = args['return_projection'] created_resource_jsons = [] for n, rj in enumerate(resource_jsons): try: if 'jsonClass' not in rj: raise ObjModelException('jsonClass attribute should exist for update/creation', 403) created_resource_id = objstore_helper.create_or_update( g.objstore_colln, rj, current_token.user_id, current_token.group_ids, initial_agents=g.initial_agents) if created_resource_id is None: created_resource_jsons.append(None) continue created_resource_json = g.objstore_colln.get( created_resource_id, projection=projection_helper.modified_projection(return_projection, ["_id", "jsonClass"])) created_resource_jsons.append(created_resource_json) except ObjModelException as e: return error_response( message='action not allowed at resource {}'.format(n), code=e.http_response_code, details={"error": e.message}) except (ValidationError, TypeError) as e: return error_response( message='schema validation error at resource {}'.format(n), code=400, details={"error": str(e)}) return created_resource_jsons
def post(self): if g.current_user_id is None: return error_response(message='not authorized', code=401) current_user_group_ids = [ group['_id'] for group in groups_helper.get_user_groups( g.users_colln, g.current_user_id, groups_projection={"_id": 1}) ] args = self.post_parser.parse_args() client_json = jsonify_argument(args['client_json'], key='client_json') check_argument_type(client_json, (dict, ), key='client_json') client_type = args['client_type'] try: new_client_id = clients_helper.create_new_client( g.oauth_colln, client_json, client_type, g.current_user_id, current_user_group_ids, initial_agents=None) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) except ValidationError as e: return error_response(message='invalid schema for client_json', code=403, details={"error": str(e)}) new_client_json = g.oauth_colln.find_one({"_id": new_client_id}) if not jsonify_argument(args['marshal_to_google_structure']): return new_client_json return marshal_to_google_structure(new_client_json)
def post(self, group_identifier): args = self.get_parser.parse_args() identifier_type = args.get('identifier_type', '_id') group_selector_doc = self._group_selector_doc(group_identifier, identifier_type) update_doc = jsonify_argument(args['update_doc'], key='update_doc') check_argument_type(update_doc, (dict, ), key='update_doc') if 'jsonClass' not in update_doc: update_doc['jsonClass'] = 'UsersGroup' if update_doc['jsonClass'] != 'UsersGroup': return error_response(message='invalid jsonClass', code=403) if identifier_type == '_id': update_doc.pop('groupName', None) if update_doc['_id'] != group_identifier: return error_response(message='invalid user_id', code=403) else: group_id = groups_helper.get_group_id(g.users_colln, group_identifier) if not group_id: return error_response(message='no group with group_name {}'. format(group_identifier)) update_doc['_id'] = group_id return_projection = jsonify_argument(args.get('return_projection', None), key='return_projection') check_argument_type(return_projection, (dict, ), key='return_projection', allow_none=True) _validate_projection(return_projection) return_projection = projection_helper.modified_projection( return_projection, mandatory_attrs=['_id', 'jsonClass']) try: group_update = JsonObject.make_from_dict(update_doc) updated_group_id = objstore_helper.update_resource( g.users_colln, group_update.to_json_map(), current_token.user_id, current_token.group_ids, not_allowed_attributes=['members', 'groupName']) if updated_group_id is None: raise ObjModelException('group not exist', 404) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) except ValueError as e: return error_response(message='schema validation error', code=403, details={"error": str(e)}) group_json = g.users_colln.find_one(group_selector_doc, projection=return_projection) return group_json
def post(self, user_id): ''' if not current_token.user_id: return error_response(message='not authorized', code=401) ''' args = self.post_parser.parse_args() user_selector_doc = users_helper.get_user_selector_doc(_id=user_id) update_doc = jsonify_argument(args['update_doc'], key='update_doc') check_argument_type(update_doc, (dict, ), key='update_doc') if '_id' not in update_doc: update_doc['_id'] = user_id if 'jsonClass' not in update_doc: update_doc['jsonClass'] = 'User' if update_doc['_id'] != user_id: return error_response(message='invalid user_id', code=403) if update_doc['jsonClass'] != 'User': return error_response(message='invalid jsonClass', code=403) return_projection = jsonify_argument(args.get('return_projection', None), key='return_projection') check_argument_type(return_projection, (dict, ), key='return_projection', allow_none=True) _validate_projection(return_projection) return_projection = projection_helper.modified_projection( return_projection, mandatory_attrs=['_id', 'jsonClass']) try: user_update = JsonObject.make_from_dict(update_doc) updated_user_id = objstore_helper.update_resource( g.users_colln, user_update.to_json_map(), current_token.user_id, current_token.group_ids, not_allowed_attributes=[ 'hashedPassword', 'externalAuthentications', 'password' ]) if updated_user_id is None: raise ObjModelException('user not exist', 404) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) except (ValueError, ValidationError, TypeError) as e: return error_response(message='schema validation error', code=403, details={"error": str(e)}) user_json = g.users_colln.find_one(user_selector_doc, projection=return_projection) return user_json
def get(self, provider_name): args = self.get_parser.parse_args() state = jsonify_argument(args.get('state', None), key='state') check_argument_type(state, (dict, ), key='state') oauth_client = get_oauth_client(provider_name, client_name=state.get( 'provider_client_name', None)) # type: OAuthClient if not oauth_client: return error_response( message= 'currently oauth provider "{}" not supported. or client_name is invalid' .format(provider_name), code=400) auth_code = oauth_client.extract_auth_code() callback_url = api.url_for(OauthCallback, provider_name=provider_name, _external=True) access_token_response = oauth_client.exchange_code_for_access_token( auth_code, registered_callback_url=callback_url) redirect_furl = furl(state.get('client_callback_url', None)) redirect_furl.args.update(access_token_response) return redirect_js_response(redirect_furl.url, message_if_none='error', message_if_invalid='error')
def delete(self): args = self.delete_parser.parse_args() resource_ids = jsonify_argument(args['resource_ids']) check_argument_type(resource_ids, (list, )) ids_validity = False not in [ isinstance(_id, str) for _id in resource_ids ] if not ids_validity: return error_response(message='ids should be strings', code=404) delete_report = [] for resource_id in resource_ids: deleted, deleted_res_ids = objstore_helper.delete_tree( g.registry_colln, resource_id, current_token.user_id, current_token.group_ids) delete_report.append({ "deleted": deleted, "deleted_resource_ids": deleted_res_ids }) return delete_report
def post(self, resource_id): current_org_name = get_current_org() args = parse_json_args(self.post_delete_parser.parse_args(), self.post_delete_payload_json_parse_directives) actions = args['actions'] user_ids = args['user_ids'] group_ids = args['group_ids'] def get_user_fn(user_id, projection=None): return get_user(current_org_name, user_id, projection=projection) # TODO! def get_group_fn(group_id, projection=None): return get_group(current_org_name, group_id, projection=projection) try: objstore_helper.add_to_permissions_agent_set( g.objstore_colln, resource_id, current_token.user_id, current_token.group_ids, actions, args['agents_set_name'], get_user_fn, get_group_fn, user_ids=user_ids, group_ids=group_ids) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) resource_json = g.objstore_colln.find_one( objstore_helper.resource_selector_doc(resource_id), projection={"permissions": 1}) resource_permissions = resource_json['permissions'] return resource_permissions
def get(self, path): qparams = request.args params = {} for key in qparams: values = qparams.getlist(key) params[key] = values[0] if len(values) == 1 else values access_token_request_data = { 'grant_type': 'refresh_token', 'refresh_token': creds_dict()['refresh_token'], 'client_id': creds_dict()['client_id'], 'client_secret': creds_dict()['client_secret'] } atr = requests.post(creds_dict()['token_uri'], data=access_token_request_data) access_token = atr.json().get('access_token', creds_dict()['token']) print(access_token) sheets_api_uri_prefix = 'https://sheets.googleapis.com/v4/spreadsheets/' sheets_request_url = sheets_api_uri_prefix + path auth_headers = { 'Authorization': atr.json().get('token_type') + ' ' + access_token } api_response = requests.get(sheets_request_url, params=params, headers=auth_headers) response_json = api_response.json() response_code = api_response.status_code if 'error' in response_json: return error_response(inherited_error_table=response_json) return response_json, response_code
def get(self, root_node_id): args = self.get_parser.parse_args() max_depth = args['max_depth'] specific_resources_filter = jsonify_argument(args['sections_filter']) or {} annotations_filter = jsonify_argument(args['annotations_filter']) or {} root_node_projection = projection_helper.modified_projection( jsonify_argument(args['root_node_projection']), mandatory_attrs=['_id']) _validate_projection(root_node_projection) specific_resources_projection = projection_helper.modified_projection( jsonify_argument(args['sections_projection']), mandatory_attrs=['_id']) _validate_projection(specific_resources_projection) annotations_projection = projection_helper.modified_projection( jsonify_argument(args['annotations_projection']), mandatory_attrs=['_id']) _validate_projection(annotations_projection) try: tree = objstore_helper.read_tree( g.objstore_colln, root_node_id, max_depth, current_token.user_id, current_token.group_ids, specific_resources_filter=specific_resources_filter, annotations_filter=annotations_filter, root_node_projection=root_node_projection, specific_resources_projection=specific_resources_projection, annotations_projection=annotations_projection ) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) return tree
def post(self): user_id = resolve_user_id() group_ids = [ group['_id'] for group in groups_helper.get_user_groups( g.users_colln, user_id, groups_projection={"_id": 1}) ] args = self.post_parser.parse_args() update_doc = jsonify_argument(args['update_doc'], key='update_doc') check_argument_type(update_doc, (dict,), key='update_doc') if '_id' not in update_doc: update_doc['_id'] = user_id if 'jsonClass' not in update_doc: update_doc['jsonClass'] = 'User' if update_doc['_id'] != user_id: return error_response(message='invalid _id', code=403) if update_doc['jsonClass'] != 'User': return error_response(message='invalid jsonClass', code=403) if 'password' in update_doc: update_doc['hashedPassword'] = bcrypt.hashpw( update_doc['password'].encode('utf-8'), bcrypt.gensalt()).decode('utf-8') update_doc.pop('password') return_projection = jsonify_argument(args.get('return_projection', None), key='return_projection') check_argument_type(return_projection, (dict,), key='return_projection', allow_none=True) _validate_projection(return_projection) return_projection = projection_helper.modified_projection( return_projection, mandatory_attrs=['_id', 'jsonClass']) try: user_update = JsonObject.make_from_dict(update_doc) updated_user_id = objstore_helper.update_resource( g.users_colln, user_update.to_json_map(), user_id, group_ids, not_allowed_attributes=('externalAuthentications', 'password')) if updated_user_id is None: raise ObjModelException('user not exist', 404) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) except ValueError as e: return error_response(message='schema validation error', code=403, details={"error": str(e)}) user_json = g.users_colln.get(user_id, projection=return_projection) return user_json
def resolve_user_id(): if current_token.user_id: return current_token.user_id elif g.current_user_id: return g.current_user_id else: message = "not authorized on behalf of any user" if current_token.client_id else "not authorized" error = error_response(message=message, code=401) abort_with_error_response(error)
def get(self, resource_id): args = self.get_parser.parse_args() args = parse_json_args(args, self.get_payload_json_parse_directives) projection = args['projection'] resource_json = g.objstore_colln.find_one( objstore_helper.resource_selector_doc(resource_id), projection=None ) if not resource_json: return error_response(message='resource not found', code=404) if not PermissionResolver.resolve_permission( resource_json, ObjectPermissions.READ, current_token.user_id, current_token.group_ids, g.objstore_colln): return error_response(message='permission denied', code=403) projection_helper.project_doc(resource_json, projection, in_place=True) return resource_json
def get(self, user_id): ''' if not current_token.user_id: return error_response() ''' args = self.get_parser.parse_args() user_selector_doc = users_helper.get_user_selector_doc(_id=user_id) groups_projection = jsonify_argument(args.get('groups_projection', None), key='groups_projection') check_argument_type(groups_projection, (dict, ), allow_none=True) _validate_projection(groups_projection) user_json = g.users_colln.find_one(user_selector_doc, projection={ "_id": 1, "permissions": 1 }) if not user_json: return error_response(message='user not found', code=404) if not PermissionResolver.resolve_permission( user_json, ObjectPermissions.READ, current_token.user_id, current_token.group_ids, g.users_colln): return error_response(message='permission denied', code=403) user_group_jsons = groups_helper.get_user_groups( g.users_colln, user_id, groups_projection=None) permitted_user_group_jsons = [] for group_json in user_group_jsons: if PermissionResolver.resolve_permission(group_json, ObjectPermissions.READ, current_token.user_id, current_token.group_ids, g.users_colln): projection_helper.project_doc(group_json, groups_projection, in_place=True) permitted_user_group_jsons.append(group_json) return permitted_user_group_jsons, 200
def _group_selector_doc(group_identifier, identifier_type): if identifier_type not in ('_id', 'groupName'): error = error_response(message='invalid identifier_type', code=400) abort_with_error_response(error) return { "_id": groups_helper.get_group_selector_doc(_id=group_identifier), "userName": groups_helper.get_group_selector_doc(group_name=group_identifier) }.get(identifier_type)
def get_requested_agents(args, colln, user_id, group_ids, filter_doc=None): selector_doc = jsonify_argument(args.get('selector_doc', None), key='selector_doc') or {} check_argument_type(selector_doc, (dict, ), key='selector_doc') if filter_doc is not None: selector_doc.update(filter_doc) projection = jsonify_argument(args.get('projection', None), key='projection') check_argument_type(projection, (dict, ), key='projection', allow_none=True) _validate_projection(projection) projection = projection_helper.modified_projection( projection, mandatory_attrs=["_id", "jsonClass"]) lrs_request_doc = jsonify_argument(args.get('linked_resources', None), 'linked_resources') check_argument_type(lrs_request_doc, (dict, ), key='linked_resources', allow_none=True) sort_doc = jsonify_argument(args.get('sort_doc', None), key='sort_doc') check_argument_type(sort_doc, (dict, list), key='sort_doc', allow_none=True) ops = OrderedDict() if sort_doc is not None: ops['sort'] = [sort_doc] if args.get('start', None) is not None and args.get('count', None) is not None: ops['skip'] = [args['start']] ops['limit'] = [args['count']] try: resource_repr_jsons = objstore_helper.get_read_permitted_resource_jsons( colln, user_id, group_ids, selector_doc, projection=projection, ops=ops) except (TypeError, ValueError): error = error_response(message='arguments to operations seems invalid', code=400) abort_with_error_response(error) if lrs_request_doc is not None: # noinspection PyUnboundLocalVariable for rj in resource_repr_jsons: linked_resources = objstore_helper.get_linked_resource_ids( colln, rj['_id'], lrs_request_doc) rj['linked_resources'] = linked_resources return resource_repr_jsons
def get(self, group_identifier): args = self.get_parser.parse_args() identifier_type = args.get('identifier_type', '_id') # noinspection PyProtectedMember group_selector_doc = GroupResource._group_selector_doc( group_identifier, identifier_type) group = JsonObject.make_from_dict( g.users_colln.find_one(group_selector_doc, projection=None)) if group is None: return error_response(message='group not found', code=404) if not PermissionResolver.resolve_permission( group, ObjectPermissions.READ, current_token.user_id, current_token.group_ids, g.users_colln): return error_response(message='permission denied', code=403) member_ids = group.members if hasattr(group, 'members') else [] return member_ids
def post(self): args = self.post_parser.parse_args() service_json = jsonify_argument(args['service_json'], key='service_json') check_argument_type(service_json, (dict, )) if service_json.get('jsonClass', None) != VedavaapiService.json_class: return error_response(message='invalid jsonClass', code=400) if 'source' not in service_json: service_json['source'] = g.registry_resource_id if service_json['source'] != g.registry_resource_id: return error_response(message='invalid service', code=403) return_projection = jsonify_argument( args.get('return_projection', None), key='return_projection') or { "permissions": 0 } try: projection_helper.validate_projection(return_projection) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) try: service_id = objstore_helper.create_or_update( g.registry_colln, service_json, current_token.user_id, current_token.group_ids, initial_agents=get_initial_agents(), non_updatable_attributes=['service_name']) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) except ValidationError as e: return error_response(message='schema validation error', code=400, details={"error": str(e)}) updated_service_json = g.registry_colln.find_one( {"_id": service_id}, projection=return_projection) return updated_service_json
def get(self): args = parse_json_args( self.get_parser.parse_args(), self.get_payload_json_parse_directives) start_nodes_selector = args['start_nodes_selector'] start_nodes_sort_doc = args['start_nodes_sort_doc'] start_find_ops = OrderedDict() if start_nodes_sort_doc is not None: start_find_ops['sort'] = [start_nodes_sort_doc] if args.get('start_nodes_offset', None) is not None: start_find_ops['skip'] = [args['start_nodes_offset']] if args.get('start_nodes_count', None) is not None: start_find_ops['limit'] = [args['start_nodes_count']] edge_filters_list = args['edge_filters_list'] for ef in edge_filters_list: if not isinstance(ef, dict): return error_response(message='invalid edge_filters_list', code=400) json_class_projection_map = args['json_class_projection_map'] include_ool_data_graph = args['include_ool_data_graph'] from_link_field = args.get('from_link_field') to_link_field = args.get('to_link_field') max_hops = args.get('max_hops', 0) network, start_nodes_ids = objstore_graph_helper.get_network( g.objstore_colln, start_nodes_selector, start_find_ops, {"nodes": {}, "edges": {}}, edge_filters_list, from_link_field, to_link_field, max_hops, current_token.user_id, current_token.group_ids) objstore_graph_helper.project_graph_nodes(network['nodes'], json_class_projection_map, in_place=True) objstore_graph_helper.project_graph_nodes(network['edges'], json_class_projection_map, in_place=True) response = {"network": network, "start_nodes_ids": start_nodes_ids} if include_ool_data_graph: nodes_ool_data_graph = objstore_graph_helper.get_ool_data_graph( g.objstore_colln, network['nodes'], current_token.user_id, current_token.group_ids ) edges_ool_data_graph = objstore_graph_helper.get_ool_data_graph( g.objstore_colln, network['edges'], current_token.user_id, current_token.group_ids ) ool_data_graph = nodes_ool_data_graph ool_data_graph.update(edges_ool_data_graph) objstore_graph_helper.project_graph_nodes(ool_data_graph, json_class_projection_map, in_place=True) response['ool_data_graph'] = ool_data_graph return response, 200
def get_requested_resource_jsons(args): json_parse_directives = { "selector_doc": { "allowed_types": (dict, ), "default": {} }, "projection": { "allowed_types": (dict, ), "allow_none": True, "custom_validator": _validate_projection }, "sort_doc": { "allowed_types": (dict, list), "allow_none": True } } args = parse_json_args(args, json_parse_directives) selector_doc = args['selector_doc'] projection = args['projection'] sort_doc = args['sort_doc'] ops = OrderedDict() if sort_doc is not None: ops['sort'] = [sort_doc] if args.get('start', None) is not None and args.get('count', 100) is not None: ops['skip'] = [args['start']] ops['limit'] = [args['count']] try: resource_repr_jsons = objstore_helper.get_read_permitted_resource_jsons( g.objstore_colln, current_token.user_id, current_token.group_ids, selector_doc, projection=projection, ops=ops ) return resource_repr_jsons, 200 except (TypeError, ValueError): error = error_response(message='arguments to operations seems invalid', code=400) abort_with_error_response(error) except Exception as e: error = error_response(message='invalid arguments', code=400) abort_with_error_response(error)
def post(self): args = self.post_parser.parse_args() root_node_return_projection = projection_helper.modified_projection( jsonify_argument(args['root_node_return_projection']), mandatory_attrs=['_id']) _validate_projection(root_node_return_projection) specific_resources_return_projection = projection_helper.modified_projection( jsonify_argument(args['sections_return_projection']), mandatory_attrs=['_id']) _validate_projection(specific_resources_return_projection) annotations_return_projection = projection_helper.modified_projection( jsonify_argument(args['annotations_return_projection']), mandatory_attrs=['_id']) _validate_projection(annotations_return_projection) trees = jsonify_argument(args['trees'], key='trees') check_argument_type(trees, (list,), key='trees') result_trees = [] try: for i, tree in enumerate(trees): result_tree = objstore_helper.update_tree( g.objstore_colln, current_token.user_id, current_token.group_ids, tree, 'tree{}'.format(i), None, root_node_return_projection=root_node_return_projection, specific_resources_return_projection=specific_resources_return_projection, annotations_return_projection=annotations_return_projection, initial_agents=g.initial_agents) result_trees.append(result_tree) except objstore_helper.TreeValidationError as e: return error_response( message="error in posting tree", code=e.http_status_code, invalid_node_path=e.invalid_node_path, invalid_node_json=e.invalid_node_json, error=str(e.error), succeded_trees=result_trees ) except (ValidationError, TypeError) as e: return error_response(message='schema validation error', error=str(e), code=400) return result_trees
def get(self): if g.current_user_id is None: return error_response(message='not authorized', code=401) args = self.get_parser.parse_args() projection = jsonify_argument(args.get('projection', None), key='projection') try: projection_helper.validate_projection(projection) except ObjModelException as e: return error_response(message=e.message, code=e.http_response_code) clients_selector_doc = { "jsonClass": "OAuth2Client", "user_id": g.current_user_id } client_jsons = (g.oauth_colln.find(clients_selector_doc, projection=projection)) print(args['marshal_to_google_structure']) if not jsonify_argument(args['marshal_to_google_structure']): return client_jsons return [marshal_to_google_structure(cj) for cj in client_jsons]