def add_user(user, password): """ :param username: :param password: :return: """ try: #add user to admin user table #AdminUserModel.create(username=user, password=password) # add user to psql conn = connect_to_db('postgres', 'postgres') cursor = conn.cursor() add_query = "CREATE ROLE %s WITH CREATEDB LOGIN PASSWORD %s" add_data = (AsIs(user), password) cursor.execute(add_query, add_data) except IntegrityError: return False return True
def edit_database(username): """ User is taken here if they would like to edit an existing database All of the databases that are associated with the user's username Are displayed, and the user can scroll through the pick the database they would like to edit The database that they select is sent to edit_selected_database_menu() the username and database name are sent :param username: :return: """ screen = curses.initscr() screen.clear() screen.keypad(1) screen.addstr(3, 5, 'Choose a database to edit/view', curses.A_BOLD | curses.A_UNDERLINE) """ Here we query all the databases associated with this user And put all the names as elements of 'databases' variable Also, query the count of databases and save that as 'databases_count' """ #databases = display_databases(username) conn = connect_to_db('postgres', 'postgres') cursor = conn.cursor() # check for duplicate database name find_query = "SELECT datname FROM pg_catalog.pg_database d WHERE pg_catalog.pg_get_userbyid(d.datdba) = %s;" cursor.execute(find_query, (username,)) length = cursor.rowcount databases = [] for db_name in cursor: databases.append(db_name[0]) selection = -1 option = 0 selected_database = None while selection < 0: choices = [0] * (length + 1) choices[option] = curses.A_REVERSE screen.addstr(1, 1, 'BACK TO MAIN MENU', choices[0]) screen.addstr(1, 23, 'length: ' + str(length)) y = 5 i = 1 for db in databases: screen.addstr(y, 5, db, choices[i]) i += 1 y += 3 screen.refresh() action = screen.getch() if action == curses.KEY_UP: option = (option - 1) % (length + 1) elif action == curses.KEY_DOWN: option = (option + 1) % (length + 1) elif action == ord('\n'): selection = option screen.addstr(19, 5, 'option: ' + str(option)) screen.addstr(20, 5, 'selection: ' + str(selection)) screen.refresh() if selection == 0: dashboard_database.database_menu(username) selected_database = databases[selection] edit_selected_database_menu(username, selected_database)