Example #1
0
def entity_setstate(entity, d):
    """Set the state of an SQLAlchemy entity

    In:
      - ``entity`` -- the newly created and not yet initialized SQLAlchemy entity
      - ``d`` -- the state dictionary (created by ``entity_getstate()``)
    """
    # Copy the _not_ SQLAlchemy managed attributes to our entity
    key = d.pop('_sa_key', None)
    entity.__dict__.update(d)

    if key is not None:
        # Fetch a new and initialized SQLAlchemy from the database
        x = session.query(entity.__class__).get(key)
        session.expunge(x)

        # Copy its state to our entity
        entity.__dict__.update(x.__dict__)

        # Adjust the entity SQLAlchemy state
        state = x._sa_instance_state.__getstate__()
        state['instance'] = entity
        entity._sa_instance_state.__setstate__(state)

        # Add the entity to the current database session
        session.add(entity)
Example #2
0
def entity_setstate(entity, d):
    """Set the state of an SQLAlchemy entity

    In:
      - ``entity`` -- the newly created and not yet initialized SQLAlchemy entity
      - ``d`` -- the state dictionary (created by ``entity_getstate()``)
    """
    # Copy the _not_ SQLAlchemy managed attributes to our entity
    key = d.pop('_sa_key', None)
    entity.__dict__.update(d)

    if key is not None:
        # Fetch a new and initialized SQLAlchemy from the database
        x = session.query(entity.__class__).get(key)
        session.expunge(x)

        # Copy its state to our entity
        entity.__dict__.update(x.__dict__)

        # Adjust the entity SQLAlchemy state
        state = x._sa_instance_state.__getstate__()
        state['instance'] = entity
        entity._sa_instance_state.__setstate__(state)

        # Add the entity to the current database session
        session.add(entity)
Example #3
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)
Example #4
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 allready 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 BeforeDelete

                    # only register the delete when the camelot model is active
                    if hasattr(BeforeDelete, "query"):
                        from camelot.model.authentication import getCurrentAuthentication

                        history = BeforeDelete(
                            model=unicode(self.entity.__name__),
                            primary_key=pk,
                            previous_attributes={},
                            authentication=getCurrentAuthentication(),
                        )
                entity_instance.delete()
                session.flush([entity_instance])
                if history:
                    Session.object_session(history).flush([history])
Example #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 allready 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 BeforeDelete
                 # only register the delete when the camelot model is active
                 if hasattr(BeforeDelete, 'query'):
                     from camelot.model.authentication import getCurrentAuthentication
                     history = BeforeDelete(
                         model=unicode(self.entity.__name__),
                         primary_key=pk,
                         previous_attributes={},
                         authentication=getCurrentAuthentication())
             entity_instance.delete()
             session.flush([entity_instance])
             if history:
                 Session.object_session(history).flush([history])
Example #6
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)