Esempio n. 1
0
 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() 
Esempio n. 2
0
 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()
Esempio n. 3
0
    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 )
Esempio n. 4
0
 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] )
Esempio n. 5
0
 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] )
Esempio n. 6
0
 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 )
Esempio n. 7
0
 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=six.text_type(self.entity.__name__),
                         memento_type='before_delete',
                         primary_key=primary_key,
                         previous_attributes=modifications)
                     memento.register_changes([change])
             session.delete(entity_instance)
             session.flush()
Esempio n. 8
0
 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 )
Esempio n. 9
0
 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 )
Esempio n. 10
0
 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 )
Esempio n. 11
0
 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
Esempio n. 12
0
 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
Esempio n. 13
0
    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()
Esempio n. 14
0
    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()
Esempio n. 15
0
 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
Esempio n. 16
0
 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=six.text_type(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
Esempio n. 17
0
 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] )
Esempio n. 18
0
 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 as 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=six.text_type(type(obj_to_flush).__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)
Esempio n. 19
0
 def add(self, obj):
     """Adds the entity instance to the default session, if it is not
     yet attached to a session"""
     session = Session.object_session(obj)
     if session == None:
         Session().add(obj)
Esempio n. 20
0
 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 )