示例#1
0
def get_current_user():
    cursor = database.cursor()
    if isinstance(database, MySQLDatabase):
        cursor.execute('select user()')
    elif isinstance(database, PostgresqlDatabase):
        cursor.execute('select current_user')
    data = cursor.fetchone()
    current_user = data[0].split('@')[0]
    return current_user
示例#2
0
def auto_update_trigger(database):
    cursor = database.cursor()
    for table in all_tables[1:]:
        cursor.execute("""
        CREATE OR REPLACE FUNCTION data_trigger_func() RETURNS TRIGGER
        LANGUAGE plpgsql
        AS
        $$
        BEGIN
            if (TG_OP = 'INSERT') then
                NEW.created_by = user;
                NEW.created_date = CURRENT_TIMESTAMP;
                NEW.update_by = user;
                NEW.update_time = CURRENT_TIMESTAMP;
                insert into log_table(
                    table_name,
                    event_data_id,
                    event,
                    event_date,
                    event_by_user)
                values (
                    TG_TABLE_NAME,
                    NEW.id,
                    TG_OP,
                    CURRENT_TIMESTAMP,
                    user);
                RETURN NEW;
            elsif (TG_OP = 'UPDATE') then
                NEW.update_by = user;
                NEW.update_time = CURRENT_TIMESTAMP;
                insert into log_table(
                    table_name,
                    event_data_id,
                    event,
                    event_date,
                    event_by_user)
                values (
                    TG_TABLE_NAME,
                    NEW.id,
                    TG_OP,
                    CURRENT_TIMESTAMP,
                    user);
                RETURN NEW;
            elsif (TG_OP = 'DELETE') then
                insert into log_table(
                    table_name,
                    event_data_id,
                    event,
                    event_date,
                    event_by_user)
                values (
                    TG_TABLE_NAME,
                    OLD.id,
                    TG_OP,
                    CURRENT_TIMESTAMP,
                    user);
                RETURN OLD;
            end if;
            RETURN null;
        END;
        $$;

        CREATE TRIGGER data_trigger
            BEFORE insert or update or delete
            ON %s
            FOR EACH ROW
            EXECUTE PROCEDURE data_trigger_func();
        """ % table._meta.table.__name__)
示例#3
0
def get_current_user():
    cursor = database.cursor()
    cursor.execute('select user')
    data = cursor.fetchone()
    current_user = data[0].split('@')[0]
    return current_user