예제 #1
0
파일: main.py 프로젝트: n30phyte/CMPUT291
def answer():
    global question_post, answer_post, CURRENT_STATE

    clear_term("Answer")

    print("Original question:")
    question_table = PrettyTable()

    for key in question_post:
        question_table.add_row([key, question_post[key]])

    question_table.field_names = ["field", "data"]
    question_table.header = False
    question_table._max_table_width = term.width
    question_table._max_width = {"data": 80}

    print(term.center(str(question_table)))

    print("\nAnswer: ")

    answers_table = PrettyTable()
    for key in answer_post:
        answers_table.add_row([key, answer_post[key]])

    answers_table.field_names = ["field", "data"]
    answers_table.header = False
    answers_table._max_table_width = term.width
    answers_table._max_width = {"data": 80}

    print(term.center(str(answers_table)))

    print("Select an action:")
    print("1. Vote")
    print("2. Go back to menu")
    action = input()
    if action == "1":
        result = db.vote(answer_post["Id"], user_id)
        if result:
            answer_post["Score"] += 1
            print("Vote successful!")
            input("Press enter key to continue")
        else:
            print("You already voted on this!")
            input("Press enter key to continue")
    elif action == "2":
        CURRENT_STATE = "PROMPT"
    else:
        print("error: please choose one of the actions")
        input("Press enter key to continue")
예제 #2
0
파일: main.py 프로젝트: n30phyte/CMPUT291
def question():
    clear_term("Question")

    question_table = PrettyTable()

    for key in question_post:
        question_table.add_row([key, question_post[key]])

    question_table.field_names = ["field", "data"]
    question_table.header = False
    question_table._max_table_width = term.width
    question_table._max_width = {"data": 80}

    print(term.center(str(question_table)))

    print("Select an action:")
    print("1. Answer")
    print("2. List Answers")
    print("3. Vote")
    print("4. Go back to menu")
    action = input()
    if action == "1":
        answer_question()
    elif action == "2":
        list_answers()
    elif action == "3":
        result = db.vote(question_post["Id"], user_id)
        if result:
            question_post["Score"] += 1
            print("Vote successful!")
            input("Press enter key to continue")
        else:
            print("You already voted on this!")
            input("Press enter key to continue")
    elif action == "4":
        global CURRENT_STATE
        CURRENT_STATE = "PROMPT"
    else:
        print("error: please choose one of the actions")
        input("Press enter key to continue")
예제 #3
0
파일: main.py 프로젝트: n30phyte/CMPUT291
def list_answers():
    clear_term("Answers")
    (accepted_answer, answers) = db.get_answers(question_post["Id"])

    all_answers = []

    global CURRENT_STATE

    answers_table = PrettyTable()
    answers_table.field_names = [
        "# ", "Accepted", "Id", "Answer", "Post Date", "Score"
    ]
    answers_table._max_width = {"Answer": 80}
    answers_table._max_table_width = term.width - 10
    answers_table.hrules = prettytable.ALL

    count = 0

    if accepted_answer is not None:
        answers_table.add_row([
            (count % 5) + 1,
            "*",
            accepted_answer["Id"],
            accepted_answer["Body"].replace("\n", "")[:80],
            accepted_answer["CreationDate"],
            accepted_answer["Score"],
        ])

        all_answers.append(accepted_answer)
        count += 1

    if len(answers) != 0:
        for ans in answers:
            answers_table.add_row([
                (count % 5) + 1,
                "",
                ans["Id"],
                ans["Body"].replace("\n", "")[:80],
                ans["CreationDate"],
                ans["Score"],
            ])

            count += 1

        all_answers.extend(answers)

    if len(all_answers) == 0:
        print("No Answers. Going back to Question view.")
        input("Press enter key to continue")
        CURRENT_STATE = "QUESTION"
    else:
        print(answers_table)

        while True:
            selection = int(
                input(
                    "\nPlease select an answer to read (or 0 to return to menu): "
                ))

            if selection == 0:
                CURRENT_STATE = "PROMPT"
                break
            elif selection <= len(all_answers):
                global answer_post
                answer_post = all_answers[selection - 1]
                CURRENT_STATE = "ANSWER"
                break
            else:
                print("Incorrect number. Please try again.")
                input("Press enter key to continue")
예제 #4
0
파일: main.py 프로젝트: n30phyte/CMPUT291
def search():
    global CURRENT_STATE, question_post

    clear_term("Search for Question")

    keywords = input("search keywords: ")

    results = list(db.search_question(keywords))
    page = 0

    results_table = PrettyTable()
    results_table.field_names = [
        "no.",
        "Id",
        "Title",
        "Creation Date",
        "Score",
        "Answers",
    ]
    results_table._max_width = {"Title": 80}
    results_table._max_table_width = term.width
    results_table.hrules = prettytable.ALL

    count = 0
    for post_result in results:
        results_table.add_row([
            (count % 5) + 1,
            post_result["Id"],
            post_result["Title"],
            post_result["CreationDate"],
            post_result["Score"],
            post_result["AnswerCount"],
        ])
        count += 1

    if len(results) == 0:
        print("No results found.")
        input("Press enter key to go to main menu")
        CURRENT_STATE = "PROMPT"
    else:
        while True:
            print(results_table.get_string(start=page * 5, end=(page + 1) * 5))
            print(term.move_down())

            print("6. show more")
            print(
                "Select a post by it's order in the table or enter 0 to return to menu"
            )
            action = input()

            if action == "0":
                CURRENT_STATE = "PROMPT"
                break
            elif action == "":
                pass
            elif int(action) <= min(len(results), 5):
                question_post = results[(int(action) - 1) + (5 * page)]
                db.visit_question(question_post["Id"])
                question_post["ViewCount"] += 1

                CURRENT_STATE = "QUESTION"
                break
            elif action == "6":
                # show more results
                if len(results) - 1 == page:
                    print("no more results")
                    input("Press enter key to continue")
                else:
                    page += 1
            else:
                print("error: please choose one of the actions")
                input("Press enter key to continue")