def signIn(curr): ''' Prompts the user to enter their user ID and password. Checks if they exist in the database. If not, it returns the user ID. Inputs: curr -- sqlite3.Cursor Returns: str ''' validInfo = False while not validInfo: uid = input('\nEnter your user ID: ') pwd = getpass.getpass('Enter your password: '******'SELECT * FROM users WHERE uid = :userID COLLATE NOCASE AND pwd = :password;', {'userID': uid, 'password': pwd}) userRow = curr.fetchone() if userRow: validInfo = True print(bcolor.green('You have successfully signed in.')) return userRow['uid'] else: print(bcolor.errmsg('error: invalid user ID or password.')) prompt = bcolor.warning('Do you still want to sign in? [y/n] ') uin = getValidInput(prompt, ['y','n']) if uin == 'n': return None
def getNewID(curr): ''' Get an appropriate user id and return it. input: curr -- sqlite3.Cursor ''' valid = False uid = None while not valid: if not uid: uid = input("Enter your id: ") if uid.isalnum(): if isUidUnique(curr, uid): if len(uid) > 4: prompt = bcolor.warning("Warning: maximum id length is 4. Use '{}' instead? [y/n] ".format(uid[:4])) uin = getValidInput(prompt, ['y', 'n']) uid = uid[:4] if uin == 'y' else None else: valid = True else: print(bcolor.errmsg("error: user id already taken.")) uid = None else: print(bcolor.errmsg("error: password cannot contain any special characters.")) uid = None return uid
def signUp(conn, curr): ''' Prompt the user for necessary information for account creation, and save it into the database. inputs: conn -- sqlite3.Connection curr -- sqlite3.Cursor ''' valid = False while not valid: print() # ask for id, name, city, password uid = getNewID(curr) f_name = input("Enter your first name: ").capitalize() l_name = input("Enter your last name: ").capitalize() name = ' '.join((f_name, l_name)) city = input("Enter your city: ").capitalize() pwd = getNewPassword() crdate = str(date.today()) if checkValid(uid, name, city): valid = True else: cont = getValidInput(bcolor.warning('Do you still want to sign up? [y/n] '), ['y', 'n']) if cont == 'n': return print(bcolor.green("Sign up successful!")) curr.execute("INSERT INTO users VALUES (?, ?, ?, ?, ?);", [uid, name, pwd, city, crdate]) conn.commit()
def checkSignout(): ''' Check if the user wants to sign out. ''' so = getValidInput(bcolor.warning('Do you want to sign out? [y/n] '), ['y', 'n']) if so == 'y': return True return False
def checkValid(uid, name, city): ''' Prompt the user to double check their account information, and return True if yes. ''' print() print(bcolor.warning("Please double check your information: ")) print(" id: {}".format(uid)) print(" name: {}".format(name)) print(" city: {}".format(city)) print() prompt = 'Is this correct? [y/n] ' uin = getValidInput(prompt, ['y','n']) if uin == 'y': return True return False
def markAnswer(conn, curr, aid): ''' Mark the selected answer post as accepted and update it into the database. Prompts the user whether to overwrite if an accepted answer already exists. inputs: conn -- sqlite3.Connection curr -- sqlite3.Cursor aid -- pid of answer post (str) ''' print(bcolor.pink('\n< Mark as Accepted Answer >')) curr.execute("SELECT * FROM answers where pid=?;", (aid, )) qid = curr.fetchone()['qid'] prompt = 'Do you want to mark this post as an accepted answer? [y/n] ' uin = page.getValidInput(prompt, ['y','n']) if uin == 'y': curr.execute("SELECT * FROM questions where pid=? and theaid IS NOT NULL;", (qid, )) aaExists = True if curr.fetchone() else False # aa: accepted answer if aaExists: prompt = bcolor.warning("Warning: Accepted answer already exists. Proceed to change? [y/n] ") uin = page.getValidInput(prompt, ['y','n']) if uin == 'y': changeAA(conn, curr, qid, aid) conn.commit() else: print('\nMarking answer is cancelled.') else: changeAA(conn, curr, qid, aid) conn.commit()
def continueAction(prompt): ''' Confirm the users if they still want to continue the current action. ''' uin = getValidInput(bcolor.warning(prompt), ['y', 'n']) return True if uin == 'y' else False