本文整理汇总了Python中camelot.core.orm.Session.object_session方法的典型用法代码示例。如果您正苦于以下问题:Python Session.object_session方法的具体用法?Python Session.object_session怎么用?Python Session.object_session使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类camelot.core.orm.Session
的用法示例。
在下文中一共展示了Session.object_session方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_current_version
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def set_current_version( cls, fixture_class = None, fixture_version = 0 ):
"""Set the current version of the fixtures in the database for a certain
fixture class.
:param fixture_class: the fixture class for which to get the version
:param fixture_version: the version number to which to set the fixture
version
"""
from sqlalchemy.orm.session import Session
obj = Session().query( cls ).filter_by( fixture_class = fixture_class ).first()
if not obj:
obj = FixtureVersion( fixture_class = fixture_class )
obj.fixture_version = fixture_version
Session.object_session( obj ).flush()
示例2: flush
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def flush(self, entity_instance):
"""Flush the pending changes of this entity instance to the backend"""
from sqlalchemy.orm.session import Session
session = Session.object_session( entity_instance )
if session:
objects_to_flush = set([entity_instance])
self._expand_compounding_objects( objects_to_flush )
#
# Create a list of changes
#
changes = []
for obj_to_flush in objects_to_flush:
if obj_to_flush in session.dirty:
modifications = {}
try:
modifications = self.get_modifications( obj_to_flush )
except Exception, e:
# todo : there seems to be a bug in sqlalchemy that causes the
# get history to fail in some cases
logger.error( 'could not get modifications from object', exc_info = e )
primary_key = self.primary_key( obj_to_flush )
if modifications and (None not in primary_key):
change = memento_change( model = unicode( self.entity.__name__ ),
memento_type = 'before_update',
primary_key = primary_key,
previous_attributes = modifications )
changes.append( change )
session.flush( objects_to_flush )
#
# If needed, track the changes
#
memento = self.get_memento()
if changes and memento != None:
memento.register_changes( changes )
示例3: delete
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def delete(self, entity_instance):
"""Delete an entity instance"""
session = Session.object_session( entity_instance )
#
# new and deleted instances cannot be deleted
#
if session:
if entity_instance in session.new:
session.expunge(entity_instance)
elif entity_instance not in session.deleted:
#
# only if we know the primary key, we can keep track of its history
#
primary_key = self.primary_key( entity_instance )
if not None in primary_key:
# save the state before the update
memento = self.get_memento()
if memento != None:
modifications = entity_to_dict( entity_instance )
change = memento_change( model = unicode( self.entity.__name__ ),
memento_type = 'before_delete',
primary_key = primary_key,
previous_attributes = modifications )
memento.register_changes( [change] )
session.delete( entity_instance )
self.flush( entity_instance )
示例4: flush
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def flush(self, entity_instance):
"""Flush the pending changes of this entity instance to the backend"""
from sqlalchemy.orm.session import Session
session = Session.object_session( entity_instance )
if session:
modifications = {}
try:
modifications = self.get_modifications( entity_instance )
except Exception, e:
# todo : there seems to be a bug in sqlalchemy that causes the
# get history to fail in some cases
logger.error( 'could not get modifications from object', exc_info = e )
session.flush( [entity_instance] )
#
# If needed, track the changes
#
primary_key = self.primary_key( entity_instance )
if modifications and (None not in primary_key):
memento = self.get_memento()
if memento != None:
change = memento_change( model = unicode( self.entity.__name__ ),
memento_type = 'before_update',
primary_key = primary_key,
previous_attributes = modifications )
memento.register_changes( [change] )
示例5: delete
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def delete(self, entity_instance):
"""Delete an entity instance"""
from sqlalchemy.orm.session import Session
session = Session.object_session( entity_instance )
#
# new and deleted instances cannot be deleted
#
if session:
if entity_instance in session.new:
session.expunge(entity_instance)
elif (entity_instance not in session.deleted) and \
(entity_instance in session): # if the object is not in the session, it might already be deleted
#
# only if we know the primary key, we can keep track of its history
#
primary_key = self.primary_key( entity_instance )
if not None in primary_key:
# save the state before the update
memento = self.get_memento()
if memento != None:
modifications = dict()
change = memento_change( model = unicode( self.entity.__name__ ),
memento_type = 'before_delete',
primary_key = primary_key,
previous_attributes = modifications )
memento.register_changes( [change] )
session.delete( entity_instance )
session.flush( [entity_instance] )
示例6: add
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def add( self, obj ):
"""Adds the entity instance to the default session, if it is not
yet attached to a session"""
import elixir
session = Session.object_session( obj )
if session == None:
elixir.session.add( obj )
示例7: flush
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def flush(self, entity_instance):
"""Flush the pending changes of this entity instance to the backend"""
from sqlalchemy.orm.session import Session
session = Session.object_session( entity_instance )
if session:
modifications = {}
try:
modifications = self.get_modifications( entity_instance )
except Exception, e:
# todo : there seems to be a bug in sqlalchemy that causes the
# get history to fail in some cases
logger.error( 'could not get modifications from object', exc_info = e )
session.flush( [entity_instance] )
#
# If needed, track the changes
#
primary_key = self.primary_key( entity_instance )
if modifications and (primary_key != None) and len(primary_key)==1:
from camelot.model.memento import Memento
# only register the update when the camelot model is active
if hasattr(Memento, 'query'):
from camelot.model.authentication import get_current_authentication
history = Memento( model = unicode( self.entity.__name__ ),
memento_type = 'before_update',
primary_key = primary_key[0],
previous_attributes = modifications,
authentication = get_current_authentication() )
try:
history.flush()
except exc.DatabaseError, e:
self.logger.error( 'Programming Error, could not flush history', exc_info = e )
示例8: refresh
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def refresh(self, entity_instance):
"""Undo the pending changes to the backend and restore the original
state"""
from sqlalchemy.orm.session import Session
session = Session.object_session( entity_instance )
if session:
if not self.is_deleted( entity_instance ):
session.refresh( entity_instance )
示例9: is_persistent
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def is_persistent(self, obj):
""":return: True if the object has a persisted state, False otherwise"""
from sqlalchemy.orm.session import Session
session = Session.object_session( obj )
if session:
if obj in session.new:
return False
if obj in session.deleted:
return False
return True
return False
示例10: remove_fixture
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def remove_fixture( cls, entity, fixture_key, fixture_class ):
"""
Remove data from the database, if it was stored through the fixture
mechanism.
:param entity: the class of the stored data
:param fixture_key: a string used to refer to the stored data
:param fixture_class: a string used to refer to a group of stored data
"""
# remove the object itself
from sqlalchemy.orm.session import Session
obj = cls.find_fixture( entity, fixture_key, fixture_class)
obj.delete()
Session.object_session( obj ).flush()
# if this succeeeds, remove the reference
reference = cls.find_fixture_reference( entity,
fixture_key,
fixture_class )
reference.delete()
Session.object_session( reference ).flush()
示例11: insert_or_update_fixture
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def insert_or_update_fixture( cls,
entity,
fixture_key,
values,
fixture_class = None ):
"""Store data in the database through the fixture mechanism, to be
able to keep track of it later.
:param entity: the class of the stored data
:param fixture_key: a string used to refer to the stored data
:param values: a dictionary with the data that should be insert or
updated in the database
:param fixture_class: a string used to refer to a group of stored data
:return: an object of type entity, either created or modified
"""
from sqlalchemy.orm.session import Session
obj = cls.find_fixture( entity, fixture_key, fixture_class )
store_fixture = False
if not obj:
obj = entity()
store_fixture = True
obj.from_dict( values )
Session.object_session( obj ).flush()
if store_fixture:
#
# The fixture itself might have been deleted, but the reference
# might be intact, so this should be updated
#
reference = cls.find_fixture_reference( entity,
fixture_key,
fixture_class )
if not reference:
reference = cls( model = unicode( entity.__name__ ),
primary_key = obj.id,
fixture_key = fixture_key,
fixture_class = fixture_class )
else:
reference.primary_key = obj.id
Session.object_session( reference ).flush()
return obj
示例12: delete
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def delete(self, entity_instance):
"""Delete an entity instance"""
from sqlalchemy.orm.session import Session
session = Session.object_session( entity_instance )
#
# new and deleted instances cannot be deleted
#
if session:
if entity_instance in session.new:
session.expunge(entity_instance)
elif (entity_instance not in session.deleted) and \
(entity_instance in session): # if the object is not in the session, it might already be deleted
history = None
#
# only if we know the primary key, we can keep track of its history
#
primary_key = self.mapper.primary_key_from_instance(entity_instance)
#
# we can only store history of objects where the primary key has only
# 1 element
# @todo: store history for compound primary keys
#
if not None in primary_key and len(primary_key)==1:
pk = primary_key[0]
# save the state before the update
from camelot.model.memento import Memento
# only register the delete when the camelot model is active
if hasattr(Memento, 'query'):
from camelot.model.authentication import get_current_authentication
history = Memento( model = unicode( self.entity.__name__ ),
memento_type = 'before_delete',
primary_key = pk,
previous_attributes = {},
authentication = get_current_authentication() )
entity_instance.delete()
session.flush( [entity_instance] )
if history:
Session.object_session( history ).flush( [history] )
示例13: expunge
# 需要导入模块: from camelot.core.orm import Session [as 别名]
# 或者: from camelot.core.orm.Session import object_session [as 别名]
def expunge(self, entity_instance):
"""Expunge the entity from the session"""
from sqlalchemy.orm.session import Session
session = Session.object_session( entity_instance )
if session:
session.expunge( entity_instance )