예제 #1
0
def delete_all_users():
    """deletes all users in the users database

    Returns:
        [boolean] -- [False if successful else True]
    """

    cursor_wrapper = db_handler.get_cursor(pg_pool)
    result = db_handler.clear_table(TABLE_NAME, COLUMNS[0], cursor_wrapper)
    return result
예제 #2
0
def delete_all_users_for_table(table_name):
    """deletes all users assigned to the selected table

    Returns:
        [boolean] -- [False if successful else True]
    """
    cursor_wrapper = db_handler.get_cursor(pg_pool)
    result = db_handler.delete_entry(table_name, TABLE_NAME, COLUMNS[1],
                                     cursor_wrapper)
    return result
예제 #3
0
def delete_users(users):
    """deletes multiple users from assignment table

    Arguments:
        users {[list]} -- [users to be removed]

    Returns:
        [Boolean] -- [True if failed, else False]
    """
    cursor_wrapper = db_handler.get_cursor(pg_pool)
    response = False
    for user in users:
        response ^= db_handler.delete_entry(user, TABLE_NAME, COLUMNS[0])
    return response
예제 #4
0
def add_entries(table_name, size, **kwargs):
    """adds one or more entries to a table

    Arguments:
        table_name {[string]} -- [name of the table to be inserted into]
        size {[string / integer]} -- [number of rows to be added]

    Returns:
        [Response] -- [HTTP response code]
    """

    # check if table_name and size valid
    try:
        size = int(size)
        if size < 0:
            return Response(status=400)
        elif size == 0:
            # print("no codes")
            return Response(status=200)
        table_name = str(table_name)
    except Exception as e:
        # invalid request parameters
        return Response(status=400)
    if table_name and size:
        try:
            # create a list of unique codes (check if they are in the table already)
            codes = []
            result = 1
            cursor_wrapper = db_handler.get_cursor()
            while len(codes) < size:
                code = str(get_random_code(CODE_LENGTH))
                # create a new code if the old one is in the table already
                while ((code in codes) and (not db_handler.select_row(
                        db_handler.COLUMNS[1], code, table_name,
                        cursor_wrapper, False))):
                    code = str(get_random_code(CODE_LENGTH))
                codes.append(code)
            # print(codes)
            if codes != []:
                # insert codes into the table
                result = db_handler.add_entries(codes, table_name,
                                                cursor_wrapper, True)
            return Response(status=201) if not result else Response(status=304)
        except pool.PoolError:
            return Response(status=500)
        except Exception:
            return Response(status=304)
    return Response(status=400)
예제 #5
0
def find_corresponding_table(user):
    """finds the table the user is assigned to

    Arguments:
        user {[string]} -- [username]

    Returns:
        [boolean] -- [False if successful else True]
    """

    cursor_wrapper = db_handler.get_cursor(pg_pool)
    result = db_handler.select_row(COLUMNS[0], user, TABLE_NAME,
                                   cursor_wrapper, True)
    if result != 1:
        return {"table_name": result[db_handler.COLUMNS[1]]}
    else:
        return 1
예제 #6
0
def add_users(users,
              table,
              cursor_wrapper=None,
              dispose=True,
              users_column=COLUMNS[0],
              tables_column=COLUMNS[1]):
    """adds users to users database and assigns them the specified table

    Arguments:
        users {list} -- [list of users to insert into the table (even for one, it must be a list)]
        table {string} -- [name of the table to add to these users]

    Keyword Arguments:
        cursor_wrapper {db_handler.get_cursor} -- [cursor wrapper to be executed with (for multiple actions with
            a single cursor)] (default: {new wrapper})
        dispose {boolean} -- [whether to dispose the cursor_wrapper after execution] (default: {True})
        users_column {string} -- [name of the column usernames are stored in] (default: {COLUMNS[0]})
        tables_column {string} -- [name of the column table names are stored in] (default: {COLUMNS[1]})

    Returns:
        [boolean] -- [False if successful else True]
    """

    # create a new wrapper
    if not cursor_wrapper:
        cursor_wrapper = db_handler.get_cursor(pg_pool)
    # create string with values (user:table (ie. (('user'), ('table')), ...))
    values_string = ""
    for i in range(len(users)):
        values_string += "("
        values_string += ("('" + users[i] + "')" + ", ")
        values_string += ("('" + table + "')")
        values_string += ")" + ", " * (i != len(users) - 1)

    # insert all values
    sql_string = "INSERT INTO %s (%s, %s) VALUES %s" % (
        TABLE_NAME, users_column, tables_column, values_string)
    command_result = 1
    try:
        command_result = db_handler.simple_sql_command(sql_string,
                                                       cursor_wrapper, dispose)
    except Exception as e:
        print(e)
    return command_result
예제 #7
0
def reassign_users(users, table_name):
    """reassigns list of users to a different table

    Arguments:
        users {[list]} -- [usernames to be reassigned]
        table_name {[string]} -- [name of the target table]

    Returns:
        [Boolean] -- [True if failed, False if OK]
    """
    cursor_wrapper = db_handler.get_cursor(pg_pool)
    tables = db_handler.get_tables(cursor_wrapper, False)
    if table_name not in tables:
        return True
    response = False
    for user in users:
        command = "UPDATE %s SET %s='%s' WHERE %s='%s'" % (
            db_handler.AUTH_TABLE_NAME, db_handler.AUTH_COLUMNS[1], table_name,
            db_handler.AUTH_COLUMNS[0], user)
        response ^= db_handler.simple_sql_select(command, cursor_wrapper)
    return response