def printMainPage(name, isPriv): ''' Display the main menu interface along with the name of the user. inputs: name -- str isPriv -- bool ''' # underlined letter u_O = bcolor.u_ualphas[14] u_P = bcolor.u_ualphas[15] u_Q = bcolor.u_ualphas[16] u_S = bcolor.u_ualphas[18] userType = bcolor.cyan('privileged') if isPriv else '' print() print('* * WELCOME {}! * * {}'.format(name, userType)) print() print(bcolor.pink('[ M E N U ]')) print() print(' {}: {}ost a {}uestion'.format(bcolor.bold('pq'), u_P, u_Q)) print(' {}: {}earch for {}osts'.format(bcolor.bold('sp'), u_S, u_P)) print(' {}: {}ign {}ut'.format(bcolor.bold('so'), u_S, u_O)) print(' {}: {}uit'.format(bcolor.bold('q'), u_Q)) print()
def editPost(conn, curr, pid): ''' Edit the title and body of the selected post. inputs: conn: sqlite3.Connection curr: sqlite3.Cursor pid: pid ''' curr.execute("SELECT title, body FROM posts WHERE pid=?;", (pid, )) currT, currB = curr.fetchone() print(bcolor.pink("\n< Edit Post >")) confirmed = False while not confirmed: nTitle, nBody = changeTitleAndBody(currT, currB) confirmed = isChangeValid(nTitle, nBody) curr.execute(''' UPDATE posts SET title = ?, body = ? WHERE pid = ?;''', (nTitle, nBody, pid)) conn.commit() print(bcolor.green("\nPost Edited!"))
def castVote(conn, curr, pid, uid): ''' Prompt the user to cast a vote to the selected post. Inputs: conn -- sqlite3.Connection curr -- sqllite3.Connection pid -- selected post (str) uid -- uid of the current user (str) ''' print('\n' + bcolor.pink('< Vote on the Post >')) prompt = 'Do you want to vote on this post? [y/n] ' confirm = page.getValidInput(prompt, ['y', 'n']) if confirm == 'y': # checks if the user has already voted for the selected post curr.execute('SELECT * FROM votes WHERE pid = ? and uid = ?;', [pid, uid]) if curr.fetchone(): print( bcolor.errmsg( "action failed: you've already voted for this post.")) else: vdate = str(date.today()) vno = getVno(curr) curr.execute('INSERT INTO votes VALUES (?, ?, ?, ?);', [pid, vno, vdate, uid]) conn.commit() print() print(bcolor.green('Voting Completed!'))
def postAns(conn, curr, poster, qid): ''' Prompt the user to post an answer for the selected question. This function assumes the type of the selected post is question. Inputs: conn -- sqlite3.Connection curr -- sqllite3.Connection poster -- uid of the current user (str) qid -- selected post (str) ''' print() print(bcolor.pink('< Write an Answer >')) infoList = getPInfo(curr) if infoList: infoList.append(poster) # infoList = [pid, pdate, title, body, poster] curr.execute('INSERT INTO posts VALUES (?, ?, ?, ?, ?);', infoList) curr.execute('INSERT INTO answers VALUES (?, ?);', [infoList[0], qid]) conn.commit() print() print(bcolor.green('Posting Completed!'))
def printFirstScreen(): ''' Display the interface of the first screen of the program. ''' print() print(bcolor.pink('Menu:')) print() print(' {}: {}ign {}n'.format(bcolor.bold('si'), bcolor.u_ualphas[18], bcolor.u_ualphas[8])) print(' {}: {}ign {}p'.format(bcolor.bold('su'), bcolor.u_ualphas[18], bcolor.u_ualphas[20])) print(' {}: {}uit'.format(bcolor.bold('q'), bcolor.u_ualphas[16])) print()
def addTag(conn, curr, pid): ''' Add tags to the selected post. Inputs: conn -- sqlite3.Connection curr -- sqllite3.Cursor pid -- pid of the selected post (str) ''' print(bcolor.pink('\n< Add Tags >')) currentTags = getCurrentTag(curr, pid) displayCurrentTag(currentTags) valid = False while not valid: newTags = getValidTag() numNewTags = len(newTags) duplicates, nonDuplicates = getDuplicateTag(currentTags, newTags) numDups = len(duplicates) dsuffix = genSuffix(duplicates) tagsToAdd = True if numDups > 0: print(bcolor.errmsg('error: post already has the following tag{}: {}'.format(dsuffix, ', '.join(duplicates)))) if numNewTags == numDups: # user enters duplicates only tagsToAdd = False prompt = 'Do you want to add another tag to the post? [y/n] ' valid = not page.continueAction(prompt) else: newTags = nonDuplicates nsuffix = genSuffix(newTags) if tagsToAdd: prompt = 'Do you want to add: "{}" ? [y/n] '.format('", "'.join(newTags)) uin = page.getValidInput(prompt, ['y','n']) if uin == 'y': valid = True insertTag(conn, curr, pid, newTags) print(bcolor.green("\nTag{} Added!".format(nsuffix))) else: prompt = 'Do you still want to add tags to the post? [y/n] ' valid = not page.continueAction(prompt)
def giveBadge(conn, curr, uid): ''' Gives a badge to the poster of the selected post. Inputs: conn -- sqlite3.Connection curr -- sqlite3.Cursor uid -- poster of the selected post (str) ''' bdate = str(date.today()) if not badgeAvailable(curr): print(bcolor.errmsg("action failed: badge is not available now.")) elif isBadgeGivenTdy(curr, uid, bdate): print(bcolor.errmsg("action failed: this poster has already received a badge today.")) else: print(bcolor.pink('\n< Give a Badge >')) displayAvailBadges(curr) valid = False while not valid: bname = getBadge() badgeRow = getBadgeRow(curr, bname) if badgeRow: # badge already exists prompt = 'Do you want to give badge: "{}" to the poster? [y/n] '.format(badgeRow['bname']) uin = page.getValidInput(prompt, ['y','n']) if uin == 'y': curr.execute('INSERT INTO ubadges VALUES (?, ?, ?);',(uid, bdate, badgeRow['bname'])) conn.commit() print(bcolor.green('\nBadge Awarded to the poster!')) valid = True else: print(bcolor.errmsg('action failed: badge: "{}" is not available.'.format(bname))) if not valid: prompt = 'Do you still want to give a badge? [y/n] ' valid = not page.continueAction(prompt)
def postQ(conn, curr, poster): ''' Prompt the user for a question post. pid is generated by the system. -- format: p'x', where x is an integer 0 <= x <= 999. Inputs: conn -- sqlite3.Connection curr -- sqllite3.Cursor poster -- uid of the signed in user (str) ''' print('\n' + bcolor.pink('< Post a Question >')) infoList = getPInfo(curr) if infoList: infoList.append(poster) # infoList = [pid, pdate, title, body, poster] curr.execute('INSERT INTO posts VALUES (?, ?, ?, ?, ?);', infoList) curr.execute('INSERT INTO questions VALUES (?, NULL);', [infoList[0]]) conn.commit() print() print(bcolor.green('Posting Completed!'))
def getKeywords(): ''' Prompt the user for search keywords and return them. return: keywords -- dict ''' os.system('clear') print(bcolor.pink('< Search Posts >')) prompt = "Enter keywords to search, each separated by a comma: " valid = False while not valid: keywords = input(prompt).lower().split(',') keywords = filter(lambda kw: len(kw.strip()) > 0, keywords) keywords = list(map(lambda kw: '%' + kw.strip() + '%', keywords)) keywords = {'kw{}'.format(i): kw for i, kw in enumerate(keywords)} if len(keywords) > 0: valid = True else: print(bcolor.errmsg('error: search keywords cannot be empty')) return keywords
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()