def appendResourceToCollection(self, coll_id, res_id): """Add resource to collection""" self.db_conn.connect() raw_collection = self.db_conn.get_entity(CommonDatabaseAPI.ET_COLLECTION, coll_id) collection = Collection(raw_collection) resource = ResourceOperationalAPI() res = resource.getResourceInfo(res_id) collection.append_resource(res) self.db_conn.save_entity(CommonDatabaseAPI.ET_COLLECTION, collection.to_dict()) self.db_conn.close()
def removeResourceFromCollection(self, coll_id, res_id): """Delete resource from collection""" self.db_conn.connect() raw_collection = self.db_conn.get_entity(CommonDatabaseAPI.ET_COLLECTION, coll_id) collection = Collection(raw_collection) resource = ResourceOperationalAPI() res = resource.getResourceInfo(res_id) collection.remove_resource(res) self.db_conn.save_entity(CommonDatabaseAPI.ET_COLLECTION, collection.to_dict()) self.db_conn.close()
def _search_elem(self, resource_filter, element): obj_list = [] if isinstance(element, ConnectionOperationalAPI): obj_list = element.findConnection(resource_filter) resource = ResourceOperationalAPI() specification = ManagementAPI() elems_list = [] for item in obj_list: s = item.to_dict() #print s spec = specification.findSpecification('connection', {'type_name': s['specification_name']})[0].to_dict() s['id'] = s.pop('_id') s['connected_res_name'] = resource.getResourceInfo(s['connected_resource']).to_dict()['resource_name'] s['connecting_res_name'] = resource.getResourceInfo(s['connecting_resource']).to_dict()['resource_name'] s['connecting_type'] = spec['connecting_type'] s['connected_type'] = spec['connected_type'] elems_list.append(s) #print elems_list return elems_list
def get_resource_params(self, request): """ Implement GET method for get resource info """ if request.GET.get('elem_id', None): res_id = request.GET['elem_id'] resource = ResourceOperationalAPI() res_info = resource.getResourceInfo(res_id).to_dict() res_spec = self._search_spec(ELEM_TYPES['IS_RES'], res_info['specification_name'])[0] # find allowed collections for resource spec_filter = {'allowed_types': str(res_info['specification_name'])} raw_spec_list = self.specification.findSpecification(ELEM_TYPES['IS_COLL'], spec_filter) allowed_spec_coll_list = [] if raw_spec_list: for spec in raw_spec_list: spec_dict = spec.to_dict() allowed_spec_coll_list.append(spec_dict['type_name']) # find created collections and fill 'Allowed collection' on web page collection = CollectionOperationalAPI() raw_allow_coll_list = collection.findCollections({'specification_name__in': allowed_spec_coll_list}) allow_coll_list = [] for coll in raw_allow_coll_list: coll_dict = coll.to_dict() # change key for display in collection on web page time_dict = dict() time_dict['value'] = coll_dict.pop('_id') time_dict['content'] = coll_dict.pop('specification_name') allow_coll_list.append(time_dict) # find assigned resource in collections and fill 'Assigned collection' on web page raw_coll_list = collection.findCollections({'resources': res_id}) assigned_coll_list = [] for coll in raw_coll_list: coll_dict = coll.to_dict() assigned_coll_list.append(coll_dict['_id']) # for treeMenu. replace keys :( TODO for refactoring spec_params_json = json.dumps(res_spec['params_spec']) to_text = spec_params_json.replace('param_name','text') items = to_text.replace('children_spec','items') json_data = {'res_spec': res_spec['params_spec'], 'res_info': res_info, 'items': json.loads(items), 'allowed_coll': allow_coll_list, 'assigned_coll': assigned_coll_list} time.sleep(0.1) return HttpResponse(json.dumps(json_data), mimetype="application/json") return HttpResponse()
def connectResources(self, conn_type, connecting_res_id, connected_res_id, conn_desc=None, **add_params): """ Connecting resource by connect type """ filter_conn = {'specification_name': conn_type, 'connecting_resource': connecting_res_id, 'connected_resource': connected_res_id} if connecting_res_id == connected_res_id: raise BIException('Connection between similar resources is not allowed!') elif self.findConnection(filter_conn): raise BIException('Connection between resources already exist!') connection = Connection(specification_name=conn_type, connecting_resource=connecting_res_id, connected_resource=connected_res_id, description=conn_desc, additional_parameters=add_params) res = ResourceOperationalAPI() connecting_res = res.getResourceInfo(connecting_res_id) connected_res = res.getResourceInfo(connected_res_id) if connecting_res.to_dict() and connected_res.to_dict(): spec = ManagementAPI() connecting_res_d = connecting_res.to_dict() connected_res_d = connected_res.to_dict() connecting_res_spec = spec.findSpecification(self.RESOURCE, {'type_name': connecting_res_d['specification_name'], 'spec_type': self.RESOURCE})[0] connected_res_spec = spec.findSpecification(self.RESOURCE, {'type_name': connected_res_d['specification_name'], 'spec_type': self.RESOURCE})[0] Resource.setup_specification([ResourceSpecification(connecting_res_spec.to_dict())]) Resource.setup_specification([ResourceSpecification(connected_res_spec.to_dict())]) connection.connect(connecting_res, connected_res) self.db_conn.connect() self.db_conn.save_entity(self.CONNECTION, connection.to_dict()) self.db_conn.close()
def get_connection_by_type(self, request): """ Get connection type for resource """ if request.is_ajax(): res_id = request.GET.get('elem_id', None) conn_type = request.GET.get('conn_id', None) connection = ConnectionOperationalAPI() resource = ResourceOperationalAPI() connected_res = connection.getLinkedResources(res_id, conn_type=conn_type, conn_direction=CONNECTED) connecting_res = connection.getLinkedResources(res_id, conn_type=conn_type, conn_direction=CONNECTING) connected_list = [] for tt in connecting_res: conn_res_id = tt.to_dict()['connected_resource'] res_info = resource.getResourceInfo(conn_res_id).to_dict() connected_list.append(res_info['resource_name']) connecting_list = [] for tt in connected_res: conn_res_id = tt.to_dict()['connecting_resource'] res_info = resource.getResourceInfo(conn_res_id).to_dict() connecting_list.append(res_info['resource_name']) return HttpResponse(json.dumps({'connecting_list': connecting_list, 'connected_list': connected_list}), mimetype="application/json")