Esempio n. 1
0
 def test_refresh( self ):
     from camelot.core.orm import Session
     from camelot.model.party import Person
     refresh_action = application_action.Refresh()
     session = Session()
     #
     # 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' )
Esempio n. 2
0
 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
Esempio n. 3
0
 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' )
Esempio n. 4
0
 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
Esempio n. 5
0
    def get_filter_data(self, admin):
        """
        :return:  a :class:`filter_data` object
        """
        from sqlalchemy.sql import select
        from camelot.core.orm import Session
        session = Session()
        filter_names = []
        joins = []
        #
        # in case of inheritance, use the local table to be able to join,
        # otherwise use the mapped table, to be able to filter on views
        #
        if admin.mapper!=admin.mapper.base_mapper:
            table = admin.mapper.local_table
        else:
            table = admin.mapper.mapped_table
        path = self.attribute.split('.')
        for field_name in path:
            attributes = admin.get_field_attributes(field_name)
            filter_names.append(attributes['name'])
            # @todo: if the filter is not on an attribute of the relation, but on the relation itselves
            if 'target' in attributes:
                admin = attributes['admin']
                joins.append(field_name)
                if attributes['direction'] == 'manytoone':
                    table = admin.entity.table.join( table )
                else:
                    table = admin.entity.table

        col = getattr( admin.entity, field_name )
        query = select([col], distinct=True, order_by=col.asc()).select_from(table)
          
        def create_decorator(col, attributes, value, joins):
            def decorator(q):
                if joins:
                    q = q.join( *joins, aliased=True)
                if 'precision' in attributes:
                    delta = pow( 10,  -1*attributes['precision'])
                    return q.filter( sql.and_(col < value+delta, col > value-delta) )
                return q.filter(col==value)
              
            return decorator

        options = [ filter_option( name = _('All'),
                                   value = Filter.All,
                                   decorator = lambda q:q ) ]
        
        for value in session.execute(query):
            if 'to_string' in attributes:
                option_name = attributes['to_string'](value[0])
            else:
                option_name = value[0]
            if attributes.get( 'translate_content', False ):
                option_name = _( option_name )
        
            options.append( filter_option( name = option_name,
                                           value = value[0],
                                           decorator = create_decorator(col, attributes, value[0], joins) ) )
        
        return filter_data( name = filter_names[0],
                            options = options,
                            default = self.default )
Esempio n. 6
0
    def get_filter_data(self, admin):
        """
        :return:  a :class:`filter_data` object
        """
        from sqlalchemy.sql import select
        from camelot.core.orm import Session
        session = Session()
        filter_names = []
        joins = []
        #
        # in case of inheritance, use the local table to be able to join,
        # otherwise use the mapped table, to be able to filter on views
        #
        if admin.mapper!=admin.mapper.base_mapper:
            table = admin.mapper.local_table
        else:
            table = admin.mapper.mapped_table
        path = self.attribute.split('.')
        for field_name in path:
            attributes = admin.get_field_attributes(field_name)
            filter_names.append(attributes['name'])
            # @todo: if the filter is not on an attribute of the relation, but on the relation itselves
            if 'target' in attributes:
                admin = attributes['admin']
                joins.append(field_name)
                if attributes['direction'] == 'manytoone':
                    table = admin.mapper.mapped_table.join( table )
                else:
                    table = admin.mapper.mapped_table

        col = getattr( admin.entity, field_name )
        query = select([col], distinct=True, order_by=col.asc()).select_from(table)
          
        def create_decorator(col, attributes, value, joins):
            def decorator(q):
                if joins:
                    q = q.join( *joins, aliased=True)
                if 'precision' in attributes:
                    delta = pow( 10,  -1*attributes['precision'])
                    return q.filter( sql.and_(col < value+delta, col > value-delta) )
                return q.filter(col==value)
              
            return decorator

        options = [ filter_option( name = _('All'),
                                   value = Filter.All,
                                   decorator = lambda q:q ) ]
        
        for value in session.execute(query):
            if 'to_string' in attributes:
                option_name = attributes['to_string'](value[0])
            else:
                option_name = value[0]
            if attributes.get( 'translate_content', False ):
                option_name = _( option_name )
        
            options.append( filter_option( name = option_name,
                                           value = value[0],
                                           decorator = create_decorator(col, attributes, value[0], joins) ) )
        
        return filter_data( name = filter_names[0],
                            options = options,
                            default = self.default )