Esempio n. 1
0
 def test_current_authentication(self):
     from camelot.model.authentication import get_current_authentication
     authentication = get_current_authentication()
     # current authentication cache should survive
     # a session expire + expunge
     orm.object_session(authentication).expire_all()
     orm.object_session(authentication).expunge_all()
     authentication = get_current_authentication()
     self.assertTrue(authentication.username)
     self.assertTrue(unicode(authentication))
Esempio n. 2
0
    def test_current_authentication(self):
        from camelot.model.authentication import get_current_authentication

        authentication = get_current_authentication()
        # current authentication cache should survive
        # a session expire + expunge
        orm.object_session(authentication).expire_all()
        orm.object_session(authentication).expunge_all()
        authentication = get_current_authentication()
        self.assertTrue(authentication.username)
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 test_memento(self):
     from camelot.model import memento
     from camelot.model.authentication import get_current_authentication
     m = memento.Memento(primary_key=1,
                         model='TestCase',
                         authentication=get_current_authentication(),
                         memento_type=1,
                         previous_attributes={'name': u'memento'})
     self.assertTrue(m.previous)
Esempio n. 5
0
 def test_memento( self ):
     from camelot.model import memento
     from camelot.model.authentication import get_current_authentication
     m = memento.Memento( primary_key = 1,
                          model = 'TestCase',
                          authentication = get_current_authentication(),
                          memento_type = 1,
                          previous_attributes = {'name':u'memento'} )
     self.assertTrue( m.previous )
Esempio n. 6
0
 def get_state(self, model_context):
     from camelot.model.authentication import get_current_authentication
     from camelot.view import art
     state = super(Authentication, self).get_state(model_context)
     authentication = get_current_authentication()
     state.verbose_name = authentication.username
     state.tooltip = ', '.join([g.name for g in authentication.groups])
     representation = authentication.get_representation()
     if representation is not None:
         state.icon = art.IconFromImage(representation)
     return state
Esempio n. 7
0
 def model_run(self, model_context):
     from camelot.model.authentication import get_current_authentication
     from camelot.view import action_steps
     from camelot.view.controls.editors.imageeditor import ImageEditor
     select_file = action_steps.SelectFile(
         file_name_filter=ImageEditor.filter)
     filenames = yield select_file
     for filename in filenames:
         yield action_steps.UpdateProgress(text=ugettext('Scale image'))
         image = QtGui.QImage(filename)
         image = image.scaled(self.image_size, self.image_size,
                              Qt.KeepAspectRatio)
         authentication = get_current_authentication()
         authentication.set_representation(image)
         yield action_steps.FlushSession(model_context.session)
Esempio n. 8
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. 9
0
 def _get_authentication_id( self ):
     """:return: the id to store in the memento table"""
     from camelot.model.authentication import get_current_authentication
     return get_current_authentication().id
Esempio n. 10
0
 def _get_authentication_id( self ):
     """:return: the id to store in the memento table"""
     from camelot.model.authentication import get_current_authentication
     return get_current_authentication().id