本文整理汇总了Python中camelot.core.orm.Session类的典型用法代码示例。如果您正苦于以下问题:Python Session类的具体用法?Python Session怎么用?Python Session使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_i18n
def test_i18n( self ):
from camelot.model.i18n import Translation, ExportAsPO
session = Session()
session.execute( Translation.__table__.delete() )
self.assertEqual( Translation.translate( 'bucket', 'nl_BE' ), None )
# run twice to check all branches in the code
Translation.translate_or_register( 'bucket', 'nl_BE' )
Translation.translate_or_register( 'bucket', 'nl_BE' )
self.assertEqual( Translation.translate( 'bucket', 'nl_BE' ), 'bucket' )
self.assertEqual( Translation.translate( '', 'nl_BE' ), '' )
self.assertEqual( Translation.translate_or_register( '', 'nl_BE' ), '' )
# clear the cache
Translation._cache.clear()
# fill the cache again
translation = Translation( language = 'nl_BE', source = 'bucket',
value = 'emmer', uid=1 )
orm.object_session( translation ).flush()
self.assertEqual( Translation.translate( 'bucket', 'nl_BE' ), 'emmer' )
export_action = ExportAsPO()
model_context = MockModelContext()
model_context.obj = translation
try:
generator = export_action.model_run( model_context )
file_step = generator.next()
generator.send( ['/tmp/test.po'] )
except StopIteration:
pass
示例2: setUp
def setUp( self ):
super( CollectionProxyCase, self ).setUp()
session = Session()
self.collection = list( session.query( Person ).all() )
self.proxy = CollectionProxy( self.person_admin,
collection_getter=lambda:self.collection,
columns_getter=self.person_admin.get_columns )
示例3: test_refresh
def test_refresh( self ):
from camelot.core.orm import Session
from camelot.model.party import Person
refresh_action = application_action.Refresh()
session = Session()
session.expunge_all()
# create objects in various states
#
p1 = Person(first_name = u'p1', last_name = u'persistent' )
p2 = Person(first_name = u'p2', last_name = u'dirty' )
p3 = Person(first_name = u'p3', last_name = u'deleted' )
p4 = Person(first_name = u'p4', last_name = u'to be deleted' )
p5 = Person(first_name = u'p5', last_name = u'detached' )
p6 = Person(first_name = u'p6', last_name = u'deleted outside session' )
session.flush()
p3.delete()
session.flush()
p4.delete()
p2.last_name = u'clean'
#
# delete p6 without the session being aware
#
person_table = Person.table
session.execute( person_table.delete().where( person_table.c.party_id == p6.id ) )
#
# refresh the session through the action
#
list( refresh_action.model_run( self.context ) )
self.assertEqual( p2.last_name, u'dirty' )
示例4: SearchCase
class SearchCase( unittest.TestCase ):
"""Test the creation of search queries"""
def setUp( self ):
metadata.bind = settings.ENGINE()
metadata.create_all()
self.session = Session()
#
# insert the value of i in each column of T, that can be searched for
#
for (i,name), definition in types_to_test.items():
value = self.value_for_type( definition, i )
t = T()
setattr( t, name, value )
self.session.flush()
self.admin = TAdmin()
def value_for_type( self, definition, i ):
value = i
if issubclass( definition, sqlalchemy.types.DateTime ):
value = datetime.datetime( year = 2000, month = 1, day = 1, hour = 1, minute = i )
elif issubclass( definition, sqlalchemy.types.Date ):
value = datetime.date( year = 2000, month = 1, day = i%31 )
elif issubclass( definition, sqlalchemy.types.Time ):
value = datetime.time( hour = 1, minute = i )
elif issubclass( definition, sqlalchemy.types.String ):
value = str( i )
elif issubclass( definition, sqlalchemy.types.Boolean ):
value = True
return value
def test_search_decorator( self ):
"""Verify it search works for most common types"""
from camelot.view.search import create_entity_search_query_decorator
for (i,name), definition in types_to_test.items():
value = self.value_for_type( definition, i )
#
# @todo : search for types that need special conversion to string
# is skipped for now because the test would become too
# convoluted, this should work through a to_string field
# attribute.
#
if isinstance( value, ( datetime.date, datetime.time, bool) ):
continue
string_value = str( value )
search_decorator = create_entity_search_query_decorator( self.admin,
string_value )
query = self.session.query( T )
query = search_decorator( query )
#print query
self.assertTrue( query.count() > 0 )
示例5: set_current_version
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()
示例6: delete
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] )
示例7: flush
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] )
示例8: add
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 )
示例9: delete
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 )
示例10: flush
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 )
示例11: setUp
def setUp(self):
super( PartyCase, self ).setUp()
from camelot.admin.application_admin import ApplicationAdmin
self.session = Session()
self.app_admin = ApplicationAdmin()
self.person_admin = self.app_admin.get_related_admin( party.Person )
self.organization_admin = self.app_admin.get_related_admin( party.Organization )
示例12: flush
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 )
示例13: translate
def translate( cls, source, language ):
"""Translate source to language, return None if no translation is found"""
if source:
key = ( source, language )
if key in cls._cache:
return cls._cache[key]
query = Session().query( cls )
query = query.filter( sql.and_( cls.source == unicode( source ),
cls.language == language,
cls.uid != 0 ) )
translation = query.first()
if translation:
cls._cache[key] = translation.value
return translation.value
return None
return ''
示例14: refresh
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 )
示例15: test_fixture
def test_fixture( self ):
from camelot.model.party import Person
from camelot.model.fixture import Fixture
session = Session()
self.assertEqual( Fixture.find_fixture_key( Person, -1 ), None )
p1 = Person()
self.assertEqual( Fixture.find_fixture_key_and_class( p1 ),
(None, None) )
session.expunge( p1 )
# insert a new Fixture
p2 = Fixture.insert_or_update_fixture( Person, 'test',
{'first_name':'Peter',
'last_name':'Principle'},
fixture_class = 'test' )
# see if we can find it back
self.assertEqual( Fixture.find_fixture_key( Person, p2.id ), 'test' )
self.assertEqual( Fixture.find_fixture_key_and_class( p2 ),
('test', 'test') )
self.assertEqual( Fixture.find_fixture_keys_and_classes( Person )[p2.id],
('test', 'test') )
# delete the person, and insert it back in the same fixture
session.delete( p2 )
session.flush()
p3 = Fixture.insert_or_update_fixture( Person, 'test',
{'first_name':'Peter',
'last_name':'Principle'},
fixture_class = 'test' )
self.assertNotEqual( p2, p3 )
# remove all fixtures
Fixture.remove_all_fixtures( Person )