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 get_collection_params(self, request): """ Implement GET method """ if request.GET.get('coll_id', None): collection = CollectionOperationalAPI() coll_info = collection.getCollectionInfo(request.GET['coll_id']).to_dict() coll_spec = self._search_spec(ELEM_TYPES['IS_COLL'], coll_info['specification_name'])[0] # for TreeMenu spec_params_json = json.dumps(coll_spec['params_spec']) to_text = spec_params_json.replace('param_name','text') items = to_text.replace('children_spec','items') json_data = {'coll_spec': coll_spec['params_spec'], 'coll_info': coll_info, 'allowed_types': coll_spec['allowed_types'], 'items': json.loads(items)} return HttpResponse(json.dumps(json_data), mimetype="application/json") return HttpResponse()
def _update_collections(self, old_assigned_to_coll, new_assigned_to_coll, res_id): """ Append or remove resource from collections """ diff_list = set(old_assigned_to_coll) new_list = diff_list.symmetric_difference(new_assigned_to_coll) collection = CollectionOperationalAPI() for coll_id in new_list: if coll_id in new_assigned_to_coll: coll = collection.getCollectionInfo(coll_id).to_dict() coll_spec = self._search_spec(ELEM_TYPES['IS_COLL'], coll['specification_name'])[0] Collection.setup_specification([CollectionSpecification(coll_spec)]) collection.appendResourceToCollection(coll_id, res_id) elif coll_id in old_assigned_to_coll: collection.removeResourceFromCollection(coll_id, res_id)
def delete(self, resource_id, params): resource = CollectionOperationalAPI() resource.deleteCollection(resource_id) return self.search(params)
def save_element(self, request): """ Saving and updating elements """ elem_type = request.POST.get('res_name',) elem_desc = request.POST.get('elem_desc',) # must be elem_desc elem_id = request.POST.get('elem_id',) res_status = request.POST.get('res_status',) res_name = request.POST.get('res_name1',) res_sys = request.POST.get('res_sys',) res_loc = request.POST.get('res_loc',) res_dep = request.POST.get('res_dep',) res_own = request.POST.get('res_own',) res_type = request.POST.get('res_type',) raw_param_res = request.POST.get('res_param',) old_assigned_to_coll = request.POST.get('old_assigned_to_coll',) new_assigned_to_coll = request.POST.get('new_assigned_to_coll',) if old_assigned_to_coll: old_assigned_to_coll = json.loads(old_assigned_to_coll) if new_assigned_to_coll: new_assigned_to_coll = json.loads(new_assigned_to_coll) # resource if res_type == ELEM_TYPES['IS_RES']: resource = ResourceOperationalAPI() spec = self._search_spec(ELEM_TYPES['IS_RES'], elem_type)[0] Resource.setup_specification([ResourceSpecification(spec)]) if elem_id: # UPDATE self._update_collections(old_assigned_to_coll, new_assigned_to_coll, elem_id) resource.updateResource(elem_id, res_name, res_status, elem_desc, res_sys, res_loc, res_dep, res_own, **json.loads(raw_param_res)) else: # CREATE resource.createResource(elem_type, res_name, res_status, elem_desc, res_sys, res_loc, res_dep, res_own, **json.loads(raw_param_res)) return HttpResponse() # collections if res_type == ELEM_TYPES['IS_COLL']: collection = CollectionOperationalAPI() coll_spec = self._search_spec(ELEM_TYPES['IS_COLL'], elem_type)[0] Collection.setup_specification([CollectionSpecification(coll_spec)]) if elem_id: collection.updateCollectionInfo(elem_id, elem_desc, additional_parameters=json.loads(raw_param_res)) else: collection.createCollection(elem_type, elem_desc, additional_parameters=json.loads(raw_param_res)) return HttpResponse() # connections if res_type == ELEM_TYPES['IS_CONN']: connection = ConnectionOperationalAPI() connecting_res_id = request.POST.get('connecting_res_id',) connected_res_id = request.POST.get('connected_res_id',) conn_spec = self._search_spec(ELEM_TYPES['IS_CONN'], elem_type)[0] Connection.setup_specification([ConnectionSpecification(conn_spec)]) if elem_id: connection.updateConnection(elem_id, elem_desc, connecting_res_id, connected_res_id, additional_parameters=json.loads(raw_param_res)) else: connection.connectResources(elem_type, connecting_res_id, connected_res_id, elem_desc, **json.loads(raw_param_res)) return HttpResponse()