def delete_resourcegroupitem(item_id,**kwargs): group_item_i = _get_item(item_id) scenario._check_can_edit_scenario(group_item_i.scenario_id, kwargs['user_id']) DBSession.delete(group_item_i) DBSession.flush() return 'OK'
def delete_resourcegroupitem(item_id, **kwargs): group_item_i = _get_item(item_id) scenario._check_can_edit_scenario(group_item_i.scenario_id, kwargs['user_id']) DBSession.delete(group_item_i) DBSession.flush() return 'OK'
def _delete_resourcescenario(scenario_id, resource_scenario): ra_id = resource_scenario.resource_attr_id try: sd_i = DBSession.query(ResourceScenario).filter(ResourceScenario.scenario_id==scenario_id, ResourceScenario.resource_attr_id==ra_id).one() except NoResultFound: raise HydraError("ResourceAttr %s does not exist in scenario %s."%(ra_id, scenario_id)) DBSession.delete(sd_i)
def update_dataset(dataset_id, name, data_type, val, units, dimension, metadata={}, **kwargs): """ Update an existing dataset """ if dataset_id is None: raise HydraError("Dataset must have an ID to be updated.") user_id = kwargs.get('user_id') dataset = DBSession.query(Dataset).filter(Dataset.dataset_id==dataset_id).one() #This dataset been seen before, so it may be attached #to other scenarios, which may be locked. If they are locked, we must #not change their data, so new data must be created for the unlocked scenarios locked_scenarios = [] unlocked_scenarios = [] for dataset_rs in dataset.resourcescenarios: if dataset_rs.scenario.locked == 'Y': locked_scenarios.append(dataset_rs) else: unlocked_scenarios.append(dataset_rs) #Are any of these scenarios locked? if len(locked_scenarios) > 0: #If so, create a new dataset and assign to all unlocked datasets. dataset = add_dataset(data_type, val, units, dimension, metadata=metadata, name=name, user_id=kwargs['user_id']) for unlocked_rs in unlocked_scenarios: unlocked_rs.dataset = dataset else: dataset.set_val(data_type, val) dataset.set_metadata(metadata) dataset.data_type = data_type dataset.data_units = units dataset.data_name = name dataset.data_dimen = dimension dataset.created_by = kwargs['user_id'] dataset.data_hash = dataset.set_hash() #Is there a dataset in the DB already which is identical to the updated dataset? existing_dataset = DBSession.query(Dataset).filter(Dataset.data_hash==dataset.data_hash, Dataset.dataset_id != dataset.dataset_id).first() if existing_dataset is not None and existing_dataset.check_user(user_id): log.warn("An identical dataset %s has been found to dataset %s." " Deleting dataset and returning dataset %s", existing_dataset.dataset_id, dataset.dataset_id, existing_dataset.dataset_id) DBSession.delete(dataset) dataset = existing_dataset return dataset
def update_value_from_mapping(source_resource_attr_id, target_resource_attr_id, source_scenario_id, target_scenario_id, **kwargs): """ Using a resource attribute mapping, take the value from the source and apply it to the target. Both source and target scenarios must be specified (and therefor must exist). """ rm = aliased(ResourceAttrMap, name='rm') #Check the mapping exists. mapping = DBSession.query(rm).filter( or_( and_( rm.resource_attr_id_a == source_resource_attr_id, rm.resource_attr_id_b == target_resource_attr_id ), and_( rm.resource_attr_id_a == target_resource_attr_id, rm.resource_attr_id_b == source_resource_attr_id ) ) ).first() if mapping is None: raise ResourceNotFoundError("Mapping between %s and %s not found"% (source_resource_attr_id, target_resource_attr_id)) #check scenarios exist s1 = _get_scenario(source_scenario_id, False, False) s2 = _get_scenario(target_scenario_id, False, False) rs = aliased(ResourceScenario, name='rs') rs1 = DBSession.query(rs).filter(rs.resource_attr_id == source_resource_attr_id, rs.scenario_id == source_scenario_id).first() rs2 = DBSession.query(rs).filter(rs.resource_attr_id == target_resource_attr_id, rs.scenario_id == target_scenario_id).first() #3 possibilities worth considering: #1: Both RS exist, so update the target RS #2: Target RS does not exist, so create it with the dastaset from RS1 #3: Source RS does not exist, so it must be removed from the target scenario if it exists return_value = None#Either return null or return a new or updated resource scenario if rs1 is not None: if rs2 is not None: log.info("Destination Resource Scenario exists. Updating dastaset ID") rs2.dataset_id = rs1.dataset_id else: log.info("Destination has no data, so making a new Resource Scenario") rs2 = ResourceScenario(resource_attr_id=target_resource_attr_id, scenario_id=target_scenario_id, dataset_id=rs1.dataset_id) DBSession.add(rs2) DBSession.flush() return_value = rs2 else: log.info("Source Resource Scenario does not exist. Deleting destination Resource Scenario") if rs2 is not None: DBSession.delete(rs2) DBSession.flush() return return_value
def delete_resourcegroup(group_id, **kwargs): """ Add a new group to a scenario. """ group_i = _get_group(group_id) #This should cascaded to delete all the group items. DBSession.delete(group_i) return 'OK'
def delete_dataset_collection(collection_id,**kwargs): try: collection = DBSession.query(DatasetCollection).filter(DatasetCollection.collection_id==collection_id).one() except NoResultFound: raise ResourceNotFoundError("No dataset collection found with id %s"%collection_id) DBSession.delete(collection) DBSession.flush()
def purge_note(note_id, **kwargs): """ Remove a note from the DB permenantly """ note_i = _get_note(note_id) DBSession.delete(note_i) DBSession.flush()
def delete_resourcegroup(group_id,**kwargs): """ Add a new group to a scenario. """ group_i = _get_group(group_id) #This should cascaded to delete all the group items. DBSession.delete(group_i) return 'OK'
def delete_project(project_id, **kwargs): """ Set the status of a project to 'X' """ user_id = kwargs.get('user_id') # check_perm(user_id, 'delete_project') project = _get_project(project_id) project.check_write_permission(user_id) DBSession.delete(project) DBSession.flush()
def _delete_resourcescenario(scenario_id, resource_scenario): ra_id = resource_scenario.resource_attr_id try: sd_i = DBSession.query(ResourceScenario).filter( ResourceScenario.scenario_id == scenario_id, ResourceScenario.resource_attr_id == ra_id).one() except NoResultFound: raise HydraError("ResourceAttr %s does not exist in scenario %s." % (ra_id, scenario_id)) DBSession.delete(sd_i)
def purge_scenario(scenario_id, **kwargs): """ Set the status of a scenario. """ _check_can_edit_scenario(scenario_id, kwargs['user_id']) scenario_i = _get_scenario(scenario_id, False, False) DBSession.delete(scenario_i) DBSession.flush() return 'OK'
def delete_project(project_id,**kwargs): """ Set the status of a project to 'X' """ user_id = kwargs.get('user_id') #check_perm(user_id, 'delete_project') project = _get_project(project_id) project.check_write_permission(user_id) DBSession.delete(project) DBSession.flush()
def delete_role(role_id,**kwargs): """ """ check_perm(kwargs.get('user_id'), 'edit_role') try: role_i = DBSession.query(Role).filter(Role.role_id==role_id).one() DBSession.delete(role_i) except InvalidRequestError: raise ResourceNotFoundError("Role (role_id=%s) does not exist"%(role_id)) return 'OK'
def delete_perm(perm_id,**kwargs): """ """ check_perm(kwargs.get('user_id'), 'edit_perm') try: perm_i = DBSession.query(Perm).filter(Perm.perm_id==perm_id).one() DBSession.delete(perm_i) except InvalidRequestError: raise ResourceNotFoundError("Permission (id=%s) does not exist"%(perm_id)) return 'OK'
def delete_role(role_id,**kwargs): """ """ #check_perm(kwargs.get('user_id'), 'edit_role') try: role_i = DBSession.query(Role).filter(Role.role_id==role_id).one() DBSession.delete(role_i) except InvalidRequestError: raise ResourceNotFoundError("Role (role_id=%s) does not exist"%(role_id)) return 'OK'
def delete_resourcegroupitems(scenario_id, item_ids, **kwargs): """ Delete specified items in a group, in a scenario. """ user_id = int(kwargs.get('user_id')) scenario = _get_scenario(scenario_id, include_data=False, include_items=False) _check_network_ownership(scenario.network_id, user_id) for item_id in item_ids: rgi = DBSession.query(ResourceGroupItem).\ filter(ResourceGroupItem.item_id==item_id).one() DBSession.delete(rgi)
def delete_user_role(deleted_user_id, role_id,**kwargs): #check_perm(kwargs.get('user_id'), 'edit_role') try: _get_user(deleted_user_id) _get_role(role_id) roleuser_i = DBSession.query(RoleUser).filter(RoleUser.user_id==deleted_user_id, RoleUser.role_id==role_id).one() DBSession.delete(roleuser_i) except NoResultFound: raise ResourceNotFoundError("User Role does not exist") return 'OK'
def delete_perm(perm_id,**kwargs): """ """ #check_perm(kwargs.get('user_id'), 'edit_perm') try: perm_i = DBSession.query(Perm).filter(Perm.perm_id==perm_id).one() DBSession.delete(perm_i) except InvalidRequestError: raise ResourceNotFoundError("Permission (id=%s) does not exist"%(perm_id)) return 'OK'
def delete_user(deleted_user_id,**kwargs): """ """ #check_perm(kwargs.get('user_id'), 'edit_user') try: user_i = DBSession.query(User).filter(User.user_id==deleted_user_id).one() DBSession.delete(user_i) except NoResultFound: raise ResourceNotFoundError("User (user_id=%s) does not exist"%(deleted_user_id)) return 'OK'
def delete_user_role(deleted_user_id, role_id,**kwargs): check_perm(kwargs.get('user_id'), 'edit_role') try: _get_user(deleted_user_id) _get_role(role_id) roleuser_i = DBSession.query(RoleUser).filter(RoleUser.user_id==deleted_user_id, RoleUser.role_id==role_id).one() DBSession.delete(roleuser_i) except NoResultFound: raise ResourceNotFoundError("User Role does not exist") return 'OK'
def delete_user(deleted_user_id,**kwargs): """ """ check_perm(kwargs.get('user_id'), 'edit_user') try: user_i = DBSession.query(User).filter(User.user_id==deleted_user_id).one() DBSession.delete(user_i) except NoResultFound: raise ResourceNotFoundError("User (user_id=%s) does not exist"%(deleted_user_id)) return 'OK'
def delete_role_perm(role_id, perm_id,**kwargs): check_perm(kwargs.get('user_id'), 'edit_perm') _get_perm(perm_id) _get_role(role_id) try: roleperm_i = DBSession.query(RolePerm).filter(RolePerm.role_id==role_id, RolePerm.perm_id==perm_id).one() DBSession.delete(roleperm_i) except NoResultFound: raise ResourceNotFoundError("Role Perm does not exist") return 'OK'
def delete_role_perm(role_id, perm_id,**kwargs): #check_perm(kwargs.get('user_id'), 'edit_perm') _get_perm(perm_id) _get_role(role_id) try: roleperm_i = DBSession.query(RolePerm).filter(RolePerm.role_id==role_id, RolePerm.perm_id==perm_id).one() DBSession.delete(roleperm_i) except NoResultFound: raise ResourceNotFoundError("Role Perm does not exist") return 'OK'
def delete_resource_attr_collection(ctx, collection_id): """ Delete a resource attribute collection """ collection_i = DBSession.query(ResourceAttrCollection).filter(ResourceAttrCollection.collection_id==collection_id).first() if collection_i is None: raise HydraError("No collection with ID %s", collection_id) DBSession.delete(collection_i) return 'OK'
def remove_dataset_from_collection(dataset_id, collection_id, **kwargs): """ Add a single dataset to a dataset collection. """ _get_collection(collection_id) collection_item = _get_collection_item(collection_id, dataset_id) if collection_item is None: raise HydraError("Dataset %s is not in collection %s.", dataset_id, collection_id) DBSession.delete(collection_item) DBSession.flush() return 'OK'
def delete_resource_attribute(resource_attr_id, **kwargs): """ Deletes a resource attribute and all associated data. """ user_id = kwargs.get('user_id') try: ra = DBSession.query(ResourceAttr).filter(ResourceAttr.resource_attr_id == resource_attr_id).one() except NoResultFound: raise ResourceNotFoundError("Resource Attribute %s not found"%(resource_attr_id)) ra.check_write_permission(user_id) DBSession.delete(ra) DBSession.flush() return 'OK'
def delete_resource_attribute(resource_attr_id, **kwargs): """ Deletes a resource attribute and all associated data. """ user_id = kwargs.get('user_id') try: ra = DBSession.query(ResourceAttr).filter( ResourceAttr.resource_attr_id == resource_attr_id).one() except NoResultFound: raise ResourceNotFoundError("Resource Attribute %s not found" % (resource_attr_id)) ra.check_write_permission(user_id) DBSession.delete(ra) DBSession.flush() return 'OK'
def delete_dataset(dataset_id,**kwargs): """ Removes a piece of data from the DB. CAUTION! Use with care, as this cannot be undone easily. """ try: d = DBSession.query(Dataset).filter(Dataset.dataset_id==dataset_id).one() except NoResultFound: raise HydraError("Dataset %s does not exist."%dataset_id) dataset_rs = DBSession.query(ResourceScenario).filter(ResourceScenario.dataset_id==dataset_id).all() if len(dataset_rs) > 0: raise HydraError("Cannot delete %s. Dataset is used by resource scenarios."%dataset_id) DBSession.delete(d) DBSession.flush()
def remove_items_from_attr_collection(ctx, collection_id, resource_attr_ids): """ Add new items to a resource attribute collection """ collection_i = DBSession.query(ResourceAttrCollection).filter(ResourceAttrCollection.collection_id==collection_id).first() if collection_i is None: raise HydraError("No collection with ID %s", collection_id) for item in collection_i.items: if item.resource_attr_id in resource_attr_ids: DBSession.delete(item) DBSession.flush() return 'OK'
def delete_mappings_in_network(network_id, network_2_id=None, **kwargs): """ Delete all the resource attribute mappings in a network. If another network is specified, only delete the mappings between the two networks. """ qry = DBSession.query(ResourceAttrMap).filter(or_(ResourceAttrMap.network_a_id == network_id, ResourceAttrMap.network_b_id == network_id)) if network_2_id is not None: qry = qry.filter(or_(ResourceAttrMap.network_a_id==network_2_id, ResourceAttrMap.network_b_id==network_2_id)) mappings = qry.all() for m in mappings: DBSession.delete(m) DBSession.flush() return 'OK'
def delete_attribute_mapping(resource_attr_a, resource_attr_b, **kwargs): """ Define one resource attribute from one network as being the same as that from another network. """ user_id = kwargs.get('user_id') rm = aliased(ResourceAttrMap, name='rm') log.info("Trying to delete attribute map. %s -> %s", resource_attr_a, resource_attr_b) mapping = DBSession.query(rm).filter( rm.resource_attr_id_a == resource_attr_a, rm.resource_attr_id_b == resource_attr_b).first() if mapping is not None: log.info("Deleting attribute map. %s -> %s", resource_attr_a, resource_attr_b) DBSession.delete(mapping) DBSession.flush() return 'OK'
def delete_mappings_in_network(network_id, network_2_id=None, **kwargs): """ Delete all the resource attribute mappings in a network. If another network is specified, only delete the mappings between the two networks. """ qry = DBSession.query(ResourceAttrMap).filter( or_(ResourceAttrMap.network_a_id == network_id, ResourceAttrMap.network_b_id == network_id)) if network_2_id is not None: qry = qry.filter( or_(ResourceAttrMap.network_a_id == network_2_id, ResourceAttrMap.network_b_id == network_2_id)) mappings = qry.all() for m in mappings: DBSession.delete(m) DBSession.flush() return 'OK'
def purge_rule(rule_id, **kwargs): rule_i = _get_rule(rule_id) DBSession.delete(rule_i) DBSession.flush()
def update_value_from_mapping(source_resource_attr_id, target_resource_attr_id, source_scenario_id, target_scenario_id, **kwargs): """ Using a resource attribute mapping, take the value from the source and apply it to the target. Both source and target scenarios must be specified (and therefor must exist). """ rm = aliased(ResourceAttrMap, name='rm') #Check the mapping exists. mapping = DBSession.query(rm).filter( or_( and_(rm.resource_attr_id_a == source_resource_attr_id, rm.resource_attr_id_b == target_resource_attr_id), and_(rm.resource_attr_id_a == target_resource_attr_id, rm.resource_attr_id_b == source_resource_attr_id))).first() if mapping is None: raise ResourceNotFoundError( "Mapping between %s and %s not found" % (source_resource_attr_id, target_resource_attr_id)) #check scenarios exist s1 = _get_scenario(source_scenario_id, False, False) s2 = _get_scenario(target_scenario_id, False, False) rs = aliased(ResourceScenario, name='rs') rs1 = DBSession.query(rs).filter( rs.resource_attr_id == source_resource_attr_id, rs.scenario_id == source_scenario_id).first() rs2 = DBSession.query(rs).filter( rs.resource_attr_id == target_resource_attr_id, rs.scenario_id == target_scenario_id).first() #3 possibilities worth considering: #1: Both RS exist, so update the target RS #2: Target RS does not exist, so create it with the dastaset from RS1 #3: Source RS does not exist, so it must be removed from the target scenario if it exists return_value = None #Either return null or return a new or updated resource scenario if rs1 is not None: if rs2 is not None: log.info( "Destination Resource Scenario exists. Updating dastaset ID") rs2.dataset_id = rs1.dataset_id else: log.info( "Destination has no data, so making a new Resource Scenario") rs2 = ResourceScenario(resource_attr_id=target_resource_attr_id, scenario_id=target_scenario_id, dataset_id=rs1.dataset_id) DBSession.add(rs2) DBSession.flush() return_value = rs2 else: log.info( "Source Resource Scenario does not exist. Deleting destination Resource Scenario" ) if rs2 is not None: DBSession.delete(rs2) DBSession.flush() return return_value