def add_user(admin=False): new_user = Users() print 'Add user to database' while True: user_name = raw_input('User (a-z, A-Z, 2-64 chars): ') if test_username(user_name): new_user.user_name = user_name break while True: user_password = getpass.getpass('Password: '******'Password (again): ') if user_password != user_password_again: print "Passwords don't match" else: if test_password(user_password): new_user.set_password(user_password) break while True: user_email = raw_input('Email: ') if is_email(user_email): new_user.user_email = user_email break if admin: new_user.user_restriction = 'admin' else: new_user.user_restriction = 'guest' new_user.user_theme = 'light' try: with session_scope() as db_session: db_session.add(new_user) sys.exit(0) except sqlalchemy.exc.OperationalError: print( "Failed to create user. You most likely need to create the DB before trying to create users.") sys.exit(1) except sqlalchemy.exc.IntegrityError: print("Username already exists.") sys.exit(1)
def change_password(username): print 'Changing password for {}'.format(username) with session_scope() as db_session: user = db_session.query(Users).filter(Users.user_name == username).one() while True: user_password = getpass.getpass('Password: '******'Password (again): ') if user_password != user_password_again: print "Passwords don't match" else: try: if test_password(user_password): user.set_password(user_password) break except sqlalchemy.orm.exc.NoResultFound: print("No user found with this name.") sys.exit(1)