本文整理汇总了Python中HydraServer.db.DBSession.add方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.add方法的具体用法?Python DBSession.add怎么用?Python DBSession.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HydraServer.db.DBSession
的用法示例。
在下文中一共展示了DBSession.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_attribute
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def add_attribute(attr,**kwargs):
"""
Add a generic attribute, which can then be used in creating
a resource attribute, and put into a type.
.. code-block:: python
(Attr){
id = 1020
name = "Test Attr"
dimen = "very big"
}
"""
log.debug("Adding attribute: %s", attr.name)
if attr.dimen is None or attr.dimen.lower() == 'dimensionless':
log.info("Setting 'dimesionless' on attribute %s", attr.name)
attr.dimen = 'dimensionless'
try:
attr_i = DBSession.query(Attr).filter(Attr.attr_name == attr.name,
Attr.attr_dimen == attr.dimen).one()
log.info("Attr already exists")
except NoResultFound:
attr_i = Attr(attr_name = attr.name, attr_dimen = attr.dimen)
attr_i.attr_description = attr.description
DBSession.add(attr_i)
DBSession.flush()
log.info("New attr added")
return attr_i
示例2: _add_resourcegroupitem
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def _add_resourcegroupitem(group_item, scenario_id):
"""
Add a single resource group item (no DB flush, as it's an internal function)
"""
if group_item.id and group_item.id > 0:
try:
group_item_i = DBSession.query(ResourceGroupItem).filter(ResourceGroupItem.item_id == group_item.id).one()
except NoResultFound:
raise ResourceNotFoundError("ResourceGroupItem %s not found" % (group_item.id))
else:
group_item_i = ResourceGroupItem()
group_item_i.group_id = group_item.group_id
if scenario_id is not None:
group_item_i.scenario_id = scenario_id
ref_key = group_item.ref_key
group_item_i.ref_key = ref_key
if ref_key == 'NODE':
group_item_i.node_id =group_item.ref_id
elif ref_key == 'LINK':
group_item_i.link_id =group_item.ref_id
elif ref_key == 'GROUP':
group_item_i.subgroup_id =group_item.ref_id
DBSession.add(group_item_i)
return group_item_i
示例3: update_role
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def update_role(role,**kwargs):
"""
Update the role.
Used to add permissions and users to a role.
"""
check_perm(kwargs.get('user_id'), 'edit_role')
try:
role_i = DBSession.query(Role).filter(Role.role_id==role.id).one()
role_i.role_name = role.name
role_i.role_code = role.code
except NoResultFound:
raise ResourceNotFoundError("Role (role_id=%s) does not exist"%(role.id))
for perm in role.permissions:
_get_perm(perm.id)
roleperm_i = RolePerm(role_id=role.id,
perm_id=perm.id
)
DBSession.add(roleperm_i)
for user in role.users:
_get_user(user.id)
roleuser_i = RoleUser(user_id=user.id,
perm_id=perm.id
)
DBSession.add(roleuser_i)
DBSession.flush()
return role_i
示例4: update_value_from_mapping
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
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
示例5: add_perm
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def add_perm(perm,**kwargs):
"""
"""
check_perm(kwargs.get('user_id'), 'add_perm')
perm_i = Perm(perm_name=perm.name, perm_code=perm.code)
DBSession.add(perm_i)
DBSession.flush()
return perm_i
示例6: add_role
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def add_role(role,**kwargs):
"""
"""
check_perm(kwargs.get('user_id'), 'add_role')
role_i = Role(role_name=role.name, role_code=role.code)
DBSession.add(role_i)
DBSession.flush()
return role_i
示例7: set_role_perm
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def set_role_perm(role_id, perm_id,**kwargs):
check_perm(kwargs.get('user_id'), 'edit_perm')
_get_perm(perm_id)
_get_role(role_id)
roleperm_i = RolePerm(role_id=role_id, perm_id=perm_id)
DBSession.add(roleperm_i)
DBSession.flush()
return roleperm_i.role
示例8: add_dataset_collection
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def add_dataset_collection(collection,**kwargs):
coln_i = DatasetCollection(collection_name=collection.name)
for dataset_id in collection.dataset_ids:
datasetitem = DatasetCollectionItem(dataset_id=dataset_id)
coln_i.items.append(datasetitem)
DBSession.add(coln_i)
DBSession.flush()
return coln_i
示例9: convert_dataset
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def convert_dataset(dataset_id, to_unit,**kwargs):
"""Convert a whole dataset (specified by 'dataset_id' to new unit
('to_unit'). Conversion ALWAYS creates a NEW dataset, so function
returns the dataset ID of new dataset.
"""
ds_i = DBSession.query(Dataset).filter(Dataset.dataset_id==dataset_id).one()
dataset_type = ds_i.data_type
dsval = ds_i.get_val()
old_unit = ds_i.data_units
if old_unit is not None:
if dataset_type == 'scalar':
new_val = hydra_units.convert(float(dsval), old_unit, to_unit)
elif dataset_type == 'array':
dim = array_dim(dsval)
vecdata = arr_to_vector(dsval)
newvec = hydra_units.convert(vecdata, old_unit, to_unit)
new_val = vector_to_arr(newvec, dim)
elif dataset_type == 'timeseries':
new_val = []
for ts_time, ts_val in dsval.items():
dim = array_dim(ts_val)
vecdata = arr_to_vector(ts_val)
newvec = hydra_units.convert(vecdata, old_unit, to_unit)
newarr = vector_to_arr(newvec, dim)
new_val.append(ts_time, newarr)
elif dataset_type == 'descriptor':
raise HydraError('Cannot convert descriptor.')
new_dataset = Dataset()
new_dataset.data_units = to_unit
new_dataset.set_val(dataset_type, new_val)
new_dataset.data_dimen = ds_i.data_dimen
new_dataset.data_name = ds_i.data_name
new_dataset.data_type = ds_i.data_type
new_dataset.hidden = 'N'
new_dataset.set_metadata(ds_i.get_metadata_as_dict())
new_dataset.set_hash()
existing_ds = DBSession.query(Dataset).filter(Dataset.data_hash==new_dataset.data_hash).first()
if existing_ds is not None:
DBSession.expunge_all()
return existing_ds.dataset_id
DBSession.add(new_dataset)
DBSession.flush()
return new_dataset.dataset_id
else:
raise HydraError('Dataset has no units.')
示例10: set_user_role
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def set_user_role(new_user_id, role_id,**kwargs):
check_perm(kwargs.get('user_id'), 'edit_role')
try:
_get_user(new_user_id)
_get_role(role_id)
roleuser_i = RoleUser(user_id=new_user_id, role_id=role_id)
DBSession.add(roleuser_i)
DBSession.flush()
except: # Will occur if the foreign keys do not exist
raise ResourceNotFoundError("User or Role does not exist")
return roleuser_i.role
示例11: add_resourcegroup
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def add_resourcegroup(group, network_id,**kwargs):
"""
Add a new group to a network.
"""
group_i = ResourceGroup()
group_i.group_name = group.name
group_i.group_description = group.description
group_i.status = group.status
group_i.network_id = network_id
DBSession.add(group_i)
DBSession.flush()
return group_i
示例12: clone_dataset
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def clone_dataset(dataset_id,**kwargs):
"""
Get a single dataset, by ID
"""
user_id = int(kwargs.get('user_id'))
if dataset_id is None:
return None
dataset = DBSession.query(Dataset).filter(
Dataset.dataset_id==dataset_id).options(joinedload_all('metadata')).first()
if dataset is None:
raise HydraError("Dataset %s does not exist."%(dataset_id))
if dataset is not None and dataset.created_by != user_id:
owner = DBSession.query(DatasetOwner).filter(
DatasetOwner.dataset_id==Dataset.dataset_id,
DatasetOwner.user_id==user_id).first()
if owner is None:
raise PermissionError("User %s is not an owner of dataset %s and therefore cannot clone it."%(user_id, dataset_id))
DBSession.expunge(dataset)
make_transient(dataset)
dataset.data_name = dataset.data_name + "(Clone)"
dataset.dataset_id = None
dataset.cr_date = None
#Try to avoid duplicate metadata entries if the entry has been cloned previously
for m in dataset.metadata:
if m.metadata_name in ("clone_of", "cloned_by"):
del(m)
cloned_meta = Metadata()
cloned_meta.metadata_name = "clone_of"
cloned_meta.metadata_val = str(dataset_id)
dataset.metadata.append(cloned_meta)
cloned_meta = Metadata()
cloned_meta.metadata_name = "cloned_by"
cloned_meta.metadata_val = str(user_id)
dataset.metadata.append(cloned_meta)
dataset.set_hash()
DBSession.add(dataset)
DBSession.flush()
cloned_dataset = DBSession.query(Dataset).filter(
Dataset.dataset_id==dataset.dataset_id).first()
return cloned_dataset
示例13: add_attributes
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def add_attributes(attrs,**kwargs):
"""
Add a generic attribute, which can then be used in creating
a resource attribute, and put into a type.
.. code-block:: python
(Attr){
id = 1020
name = "Test Attr"
dimen = "very big"
}
"""
log.debug("Adding s: %s", [attr.name for attr in attrs])
#Check to see if any of the attributs being added are already there.
#If they are there already, don't add a new one. If an attribute
#with the same name is there already but with a different dimension,
#add a new attribute.
all_attrs = DBSession.query(Attr).all()
attr_dict = {}
for attr in all_attrs:
attr_dict[(attr.attr_name, attr.attr_dimen)] = attr
attrs_to_add = []
existing_attrs = []
for potential_new_attr in attrs:
if potential_new_attr.dimen is None or potential_new_attr.dimen.lower() == 'dimensionless':
potential_new_attr.dimen = 'dimensionless'
if attr_dict.get((potential_new_attr.name, potential_new_attr.dimen)) is None:
attrs_to_add.append(potential_new_attr)
else:
existing_attrs.append(attr_dict.get((potential_new_attr.name, potential_new_attr.dimen)))
new_attrs = []
for attr in attrs_to_add:
attr_i = Attr()
attr_i.attr_name = attr.name
attr_i.attr_dimen = attr.dimen
attr_i.attr_description = attr.description
DBSession.add(attr_i)
new_attrs.append(attr_i)
DBSession.flush()
new_attrs = new_attrs + existing_attrs
return new_attrs
示例14: add_dataset_to_collection
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def add_dataset_to_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 not None:
raise HydraError("Dataset Collection %s already contains dataset %s", collection_id, dataset_id)
new_item = DatasetCollectionItem()
new_item.dataset_id=dataset_id
new_item.collection_id=collection_id
DBSession.add(new_item)
DBSession.flush()
return 'OK'
示例15: set_attribute_mapping
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import add [as 别名]
def set_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')
ra_1 = get_resource_attribute(resource_attr_a)
ra_2 = get_resource_attribute(resource_attr_b)
mapping = ResourceAttrMap(resource_attr_id_a = resource_attr_a,
resource_attr_id_b = resource_attr_b,
network_a_id = ra_1.get_network().network_id,
network_b_id = ra_2.get_network().network_id )
DBSession.add(mapping)
return mapping