Exemple #1
0
def route_question(question_id):
    question = data_handler.get_data_by_id(question_id, 'question')[0]
    if request.method == "GET" and not request.args.get("answer_id"):
        question["view_number"] = int(question["view_number"]) + 1
        data_handler.edit_row('question', question)

    answer_id_for_comment = request.args.get("answer_id")
    comment_to_question = request.args.get("comment_to_question")

    answers = data_handler.get_answers_by_qid(question_id)
    comments = data_handler.get_comments_by_question(question)

    if request.args.get("answer_to_edit"):
        answer_to_edit = data_handler.get_data_by_id(
            str(request.args.get("answer_to_edit")), "answer")[0]
    else:
        answer_to_edit = {}
    comment_to_edit = dict(request.form)
    user_data = {}
    if 'username' in request.cookies:
        username = request.cookies.get('username')
        user_data = data_handler.get_data_by_username(username)[0]

    return render_template("question.html",
                           question=question,
                           answers=answers,
                           comments=comments,
                           answer_id=answer_id_for_comment,
                           comment_to_question=comment_to_question,
                           comment_to_edit=comment_to_edit,
                           answer_to_edit=answer_to_edit,
                           user_data=user_data)
Exemple #2
0
def route_answer_vote_down(answer_id):
    answer = data_handler.get_data_by_id(answer_id, 'answer')[0]
    question_id = str(answer["question_id"])
    answer["vote_number"] = int(answer["vote_number"]) - 1
    question = data_handler.get_data_by_id(question_id, 'question')[0]
    question["view_number"] = int(question["view_number"]) - 1
    data_handler.edit_row("question", question)
    data_handler.edit_row("answer", answer)
    return redirect(url_for("route_question", question_id=question_id))
Exemple #3
0
def route_delete_comment(comment_id):
    comment = data_handler.get_data_by_id(comment_id, 'comment')[0]
    if comment['question_id']:
        question_id = comment['question_id']
    else:
        answer = data_handler.get_data_by_id(str(comment["answer_id"]),
                                             "answer")
        question_id = answer[0]["question_id"]

    data_handler.delete_data_by_id(str(comment.get("id")), "comment")

    return redirect(url_for("route_question", question_id=question_id))
Exemple #4
0
def route_change_accept(answer_id):
    answer = data_handler.get_data_by_id(answer_id, 'answer')[0]
    accept = True
    question_id = data_handler.get_data_by_id(answer_id,
                                              "answer")[0]["question_id"]

    rows_with_accept = data_handler.check_any_accept('answer', question_id)
    print(rows_with_accept)

    if rows_with_accept[0]['count'] == 0:
        data_handler.update_answer_accept(answer['id'], accept)
    else:
        print('question already answered')
    return redirect(url_for('route_question', question_id=question_id))
Exemple #5
0
def route_edit_comment(comment_id):
    if request.method == "POST":

        edited_comment = dict(request.form)
        edited_comment["edited_count"] = int(
            edited_comment["edited_count"]) + 1
        data_handler.edit_row("comment", edited_comment)

        if request.form.get("question_id") != 'NULL':
            question_id = data_handler.get_data_by_id(
                comment_id, "comment")[0]["question_id"]
        else:
            question_id = \
                data_handler.get_data_by_id(str(data_handler.get_data_by_id(comment_id, "comment")[0]["answer_id"]),
                                            "answer")[0]["question_id"]

        return redirect(url_for("route_question", question_id=question_id))
Exemple #6
0
def route_edit(question_id):
    if request.method == "POST":
        data_handler.edit_row("question", request.form)
        return redirect(
            url_for("route_question", question_id=request.form.get("id")))

    question = data_handler.get_data_by_id(question_id, 'question')[0]
    return render_template("add-question.html", question=question)
Exemple #7
0
def route_edit_answer(answer_id):
    data_handler.edit_row("answer", request.form)
    question_id = data_handler.get_data_by_id(answer_id,
                                              "answer")[0]["question_id"]

    return redirect(url_for("route_question", question_id=question_id))
Exemple #8
0
def route_new_answer_comment(answer_id):
    if request.method == "POST":
        data_handler.add_new_row_to_table(request.form, "comment")
        question_id = data_handler.get_data_by_id(answer_id,
                                                  "answer")[0]["question_id"]
        return redirect(url_for('route_question', question_id=question_id))
Exemple #9
0
def route_question_vote_down(question_id):
    question = data_handler.get_data_by_id(question_id, 'question')[0]
    question["vote_number"] = int(question["vote_number"]) - 1
    question["view_number"] = int(question["view_number"]) - 1
    data_handler.edit_row("question", question)
    return redirect(url_for("route_question", question_id=question_id))
Exemple #10
0
def route_delete_answer(answer_id):
    answer = data_handler.get_data_by_id(answer_id, 'answer')[0]
    question_id = answer.get('question_id')
    data_handler.delete_data_by_id(str(answer.get("id")), "answer")
    return redirect(url_for("route_question", question_id=question_id))