本文整理汇总了Python中HydraServer.db.DBSession.flush方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.flush方法的具体用法?Python DBSession.flush怎么用?Python DBSession.flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HydraServer.db.DBSession
的用法示例。
在下文中一共展示了DBSession.flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_attribute
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
def update_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"
}
"""
if attr.dimen is None or attr.dimen.lower() == 'dimensionless':
log.info("Setting 'dimesionless' on attribute %s", attr.name)
attr.dimen = 'dimensionless'
log.debug("Adding attribute: %s", attr.name)
attr_i = _get_attr(Attr.attr_id)
attr_i.attr_name = attr.name
attr_i.attr_dimen = attr.dimension
attr_i.attr_description = attr.description
#Make sure an update hasn't caused an inconsistency.
check_attr_dimension(attr_i.attr_id)
DBSession.flush()
return attr_i
示例2: update_role
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [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
示例3: update_rule
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
def update_rule(rule, **kwargs):
rule_i = _get_rule(rule.id)
if rule.ref_key != rule_i.ref_key:
raise HydraError("Cannot convert a %s rule to a %s rule. Please create a new rule instead."%(rule_i.ref_key, rule.ref_key))
if rule.ref_key == 'NETWORK':
rule_i.network_id = rule.ref_id
elif rule.ref_key == 'NODE':
rule_i.node_id = rule.ref_id
elif rule.ref_key == 'LINK':
rule_i.link_id = rule.ref_id
elif rule.ref_key == 'GROUP':
rule_i.group_id = rule.group_id
else:
raise HydraError("Ref Key %s not recognised.")
rule_i.scenario_id = rule.scenario_id
rule_i.rule_name = rule.name
rule_i.rule_description = rule.description
rule_i.rule_text = rule.text
DBSession.flush()
return rule_i
示例4: add_resource_attribute
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
def add_resource_attribute(resource_type, resource_id, attr_id, is_var,**kwargs):
"""
Add a resource attribute attribute to a resource.
attr_is_var indicates whether the attribute is a variable or not --
this is used in simulation to indicate that this value is expected
to be filled in by the simulator.
"""
attr = DBSession.query(Attr).filter(Attr.attr_id==attr_id).first()
if attr is None:
raise ResourceNotFoundError("Attribute with ID %s does not exist."%attr_id)
resource_i = _get_resource(resource_type, resource_id)
for ra in resource_i.attributes:
if ra.attr_id == attr_id:
raise HydraError("Duplicate attribute. %s %s already has attribute %s"
%(resource_type, resource_i.get_name(), attr.attr_name))
attr_is_var = 'Y' if is_var else 'N'
new_ra = resource_i.add_attribute(attr_id, attr_is_var)
DBSession.flush()
return new_ra
示例5: bulk_update_resourcedata
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
def bulk_update_resourcedata(scenario_ids, resource_scenarios,**kwargs):
"""
Update the data associated with a list of scenarios.
"""
user_id = kwargs.get('user_id')
res = None
res = {}
net_ids = DBSession.query(Scenario.network_id).filter(Scenario.scenario_id.in_(scenario_ids)).all()
if len(set(net_ids)) != 1:
raise HydraError("Scenario IDS are not in the same network")
for scenario_id in scenario_ids:
_check_can_edit_scenario(scenario_id, kwargs['user_id'])
scen_i = _get_scenario(scenario_id, False, False)
res[scenario_id] = []
for rs in resource_scenarios:
if rs.value is not None:
updated_rs = _update_resourcescenario(scen_i, rs, user_id=user_id, source=kwargs.get('app_name'))
res[scenario_id].append(updated_rs)
else:
_delete_resourcescenario(scenario_id, rs)
DBSession.flush()
return res
示例6: set_network_permission
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
def set_network_permission(network_id, usernames, read, write, share,**kwargs):
"""
Set permissions on a network to a list of users, identifed by
their usernames. The read flag ('Y' or 'N') sets read access, the write
flag sets write access. If the read flag is 'N', then there is
automatically no write access or share access.
"""
user_id = kwargs.get('user_id')
net_i = _get_network(network_id)
#Check if the user is allowed to share this network.
net_i.check_share_permission(user_id)
#You cannot edit something you cannot see.
if read == 'N':
write = 'N'
share = 'N'
for username in usernames:
user_i = _get_user(username)
#The creator of a network must always have read and write access
#to their project
if net_i.created_by == user_i.user_id:
raise HydraError("Cannot set permissions on network %s"
" for user %s as tis user is the creator." %
(network_id, username))
net_i.set_owner(user_i.user_id, read=read, write=write, share=share)
DBSession.flush()
示例7: delete_rule
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
def delete_rule(rule_id, **kwargs):
rule_i = _get_rule(rule_id)
rule_i.status = 'X'
DBSession.flush()
示例8: delete_resourcegroupitem
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
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'
示例9: add_attribute
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [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
示例10: update_resourcedata
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
def update_resourcedata(scenario_id, resource_scenarios,**kwargs):
"""
Update the data associated with a scenario.
Data missing from the resource scenario will not be removed
from the scenario. Use the remove_resourcedata for this task.
If the resource scenario does not exist, it will be created.
If the value of the resource scenario is specified as being None, the
resource scenario will be deleted.
If the value of the resource scenario does not exist, it will be created.
If the both the resource scenario and value already exist, the resource scenario
will be updated with the ID of the dataset.
If the dataset being set is being changed, already exists,
and is only used by a single resource scenario,
then the dataset itself is updated, rather than a new one being created.
"""
user_id = kwargs.get('user_id')
res = None
_check_can_edit_scenario(scenario_id, kwargs['user_id'])
scen_i = _get_scenario(scenario_id, False, False)
res = []
for rs in resource_scenarios:
if rs.value is not None:
updated_rs = _update_resourcescenario(scen_i, rs, user_id=user_id, source=kwargs.get('app_name'))
res.append(updated_rs)
else:
_delete_resourcescenario(scenario_id, rs)
DBSession.flush()
return res
示例11: assign_value
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
def assign_value(rs, data_type, val,
units, name, dimension, metadata={}, data_hash=None, user_id=None, source=None):
"""
Insert or update a piece of data in a scenario.
If the dataset is being shared by other resource scenarios, a new dataset is inserted.
If the dataset is ONLY being used by the resource scenario in question, the dataset
is updated to avoid unnecessary duplication.
"""
log.debug("Assigning value %s to rs %s in scenario %s",
name, rs.resource_attr_id, rs.scenario_id)
if rs.scenario.locked == 'Y':
raise PermissionError("Cannot assign value. Scenario %s is locked"
%(rs.scenario_id))
#Check if this RS is the only RS in the DB connected to this dataset.
#If no results is found, the RS isn't in the DB yet, so the condition is false.
update_dataset = False # Default behaviour is to create a new dataset.
if rs.dataset is not None:
#Has this dataset changed?
if rs.dataset.data_hash == data_hash:
log.debug("Dataset has not changed. Returning.")
return
connected_rs = DBSession.query(ResourceScenario).filter(ResourceScenario.dataset_id==rs.dataset.dataset_id).all()
#If there's no RS found, then the incoming rs is new, so the dataset can be altered
#without fear of affecting something else.
if len(connected_rs) == 0:
#If it's 1, the RS exists in the DB, but it's the only one using this dataset or
#The RS isn't in the DB yet and the datset is being used by 1 other RS.
update_dataset = True
if len(connected_rs) == 1 :
if connected_rs[0].scenario_id == rs.scenario_id and connected_rs[0].resource_attr_id==rs.resource_attr_id:
update_dataset = True
else:
update_dataset=False
if update_dataset is True:
log.info("Updating dataset '%s'", name)
dataset = data.update_dataset(rs.dataset.dataset_id, name, data_type, val, units, dimension, metadata, **dict(user_id=user_id))
rs.dataset = dataset
rs.dataset_id = dataset.dataset_id
else:
log.info("Creating new dataset %s in scenario %s", name, rs.scenario_id)
dataset = data.add_dataset(data_type,
val,
units,
dimension,
metadata=metadata,
name=name,
**dict(user_id=user_id))
rs.dataset = dataset
rs.source = source
DBSession.flush()
示例12: update_value_from_mapping
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [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
示例13: delete_dataset_collection
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
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()
示例14: add_perm
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [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
示例15: bulk_insert_data
# 需要导入模块: from HydraServer.db import DBSession [as 别名]
# 或者: from HydraServer.db.DBSession import flush [as 别名]
def bulk_insert_data(data, **kwargs):
datasets = _bulk_insert_data(data, user_id=kwargs.get('user_id'), source=kwargs.get('app_name'))
#This line exists to make the DBSession 'dirty',
#thereby telling it to flush the bulk insert.
datasets[0].data_name = datasets[0].data_name
DBSession.flush()
return datasets