Esempio n. 1
0
def table_onvalidate(form):
    '''
    The callback function for the tables form
    '''
    # before creation
    from plugin_lookout import db_got_table
    if 'new' in request.args:
        connection_name = db.plugin_lookout_connections[form.vars.connection_id].alias
        mydb = globals()[connection_name]
        tab_in_sqldb, msg = db_got_table(mydb, form.vars.table_name)
        
        if tab_in_sqldb:
            form.vars.table_migrate = False
        else:
            form.vars.table_migrate = True

        form.errors.table_name = IS_VALID_SQL_TABLE_NAME(mydb,
            check_reserved = ('common', 'postgres', )
        )(form.vars.table_name)[1]
    
    # before deletion
    if form.vars.delete_this_record=='on':
#        join = db.plugin_lookout_tables.connection_id==db.plugin_lookout_connections.id
        where = db.plugin_lookout_tables.id==request.vars.id
        rec_table = db(where).select(
            db.plugin_lookout_tables.table_name,
            db.plugin_lookout_tables.table_migrate,
            db.plugin_lookout_tables.is_view,
            db.plugin_lookout_tables.is_active,
            db.plugin_lookout_tables.connection_name
        ).first()
        table_name = rec_table.table_name
        table_migrate = rec_table.table_migrate
        table_is_view = rec_table.is_view
        connection_name = rec_table.connection_name
        table_is_active = rec_table.is_active
        
        key = '%s_%s' % (globals()[connection_name]._uri_hash, table_name)
        db(db.auth_group.role.contains(key)).delete()
        
        db(db.plugin_lookout_fields.table_id==request.vars.id).delete()
        
#        for row in db(db.plugin_lookout_fields.tables.contains(form.vars.id)).select():
#            ids = row.tables
#            ids.remove(int(form.vars.id))
#            row.update_record(tables=ids)
            
        if not table_migrate and table_is_view:
            try:
                mydb.executesql('DROP VIEW %s;' % table_name)
            except Exception, error:
                session.flash = str(error)
        elif table_migrate and table_is_active:
            try:
                globals()[connection_name][table_name].drop()
            except Exception, error:
                msg = T('Table %s not removed: %s') % (table_name, str(error))
                session.flash = msg
                # this will block record deletion
                form.errors.external_error = str(msg)
Esempio n. 2
0
    if k not in globals().keys() + ['db']:
        exec('%s=v' % k)
        del v


####################################################################### TABLES #

from plugin_lookout import db_got_table

db.define_table('plugin_lookout_tables',
    Field('table_name', label=T('Table name'), required=True,
        ondelete='CASCADE'
    ),
    Field('table_migrate', 'boolean',
        compute = lambda row: not (db_got_table(globals()[db\
            .plugin_lookout_connections[row['connection_id']]\
                .alias], row['table_name'])[0] or row['is_view'])),
    Field('table_singular', label=T('Singular')),
    Field('table_plural', label=T('Plural')),
    Field('restricted', 'boolean', default=False),
    Field('is_active', 'boolean', default=True, label=T('Active'), comment=T('Let the table be recognized from db?')),
    Field('is_view', 'boolean', label=T('View'), default=False, writable=False, readable=True),
    Field('connection_id', db.plugin_lookout_connections, required=True,
        requires = IS_IN_DB(db(db.plugin_lookout_connections.created_by==auth.user_id), 'plugin_lookout_connections.id', '%(alias)s: %(dsn)s')
    ),
    Field('connection_name', compute=lambda row: db.plugin_lookout_connections[row['connection_id']].alias),
    auth.signature.created_by,
    singular=T('Table'), plural=T('Tables'),
    format='%(table_name)s'
)
#db.plugin_lookout_tables.table_name.requires = IS_VALID_SQL_TABLE_NAME(wich_db(request), check_reserved=('common', 'postgres', ))