def route_delete_comment(comment_id):
    comment = data_manager.get_single_entry('comment', comment_id)
    if data_manager.comment_belongs_to_user(session.get('username'),
                                            comment_id):
        answer_id_of_comment = comment['answer_id']
        question_id_of_comment = comment['question_id']

        if request.method == "GET":
            if answer_id_of_comment:
                answer_data = data_manager.get_single_entry(
                    'answer', answer_id_of_comment)
                return render_template('database_ops/delete_comment.html',
                                       answer=answer_data['message'],
                                       comment=comment)
            question_data = data_manager.get_single_entry(
                'question', question_id_of_comment)
            return render_template('database_ops/delete_comment.html',
                                   question=question_data,
                                   comment=comment)

        if request.form['delete-button'] == 'Yes':
            data_manager.delete_comment(comment_id)

    return redirect(url_for('display_question_and_answers',
                            question_id=comment['question_id']),
                    code=307)
def route_add_comment_to_answer(question_id, answer_id):
    """
    It redirects you to the page where you can add your inputs for comment, it also shows you the answer that you want to
    comment.
    :param question_id: id integer of the specific question
    :param answer_id: id integer of the specific answer
    :return:
    """
    if request.method == "GET":
        answer_by_id = data_manager.get_single_entry('answer', answer_id)
        return render_template('database_ops/new_comment.html',
                               answer_by_id=answer_by_id)

    # After you submit your comment for the specific answer
    # this program part will make a dictionary with the inputs and
    # insert it as a row in the table of comments.
    # After this process it redirects you to the specific page of the question.

    comment_message = request.form['message']
    user_id = session['user_id']
    data_manager.insert_comment(comment_message,
                                question_id,
                                answer_id=answer_id,
                                user_id=user_id)
    return redirect(url_for('display_question_and_answers',
                            question_id=question_id),
                    code=307)
def route_edit_answer(answer_id):
    answer_data = data_manager.get_single_entry('answer', answer_id)
    question_id = answer_data.get('question_id')
    question_data = data_manager.get_single_entry('question', question_id)

    if data_manager.answer_belongs_to_user(session.get('username'), answer_id):

        if request.method == 'GET':
            return render_template('database_ops/new_answer.html',
                                   answer=answer_data,
                                   question=question_data)

        user_inputs_for_answer = request.form.to_dict()
        user_inputs_for_answer['image'] = handle_image(request.files['image'])
        data_manager.update_entry('answer', answer_id, user_inputs_for_answer)

    return redirect(url_for('display_question_and_answers',
                            question_id=question_id),
                    code=307)
def route_add_comment_to_question(question_id):
    if request.method == 'GET':
        question = data_manager.get_single_entry('question', question_id)
        return render_template('database_ops/new_comment.html',
                               answer_by_id=question)

    user_id = session['user_id']
    comment_message = request.form['message']
    data_manager.insert_comment(comment_message, question_id, user_id)

    return redirect(url_for('display_question_and_answers',
                            question_id=question_id),
                    code=307)
def route_edit_comment(comment_id):
    comment_data = data_manager.get_single_entry('comment', comment_id)
    question_id = comment_data.get('question_id')

    if data_manager.comment_belongs_to_user(session.get('username'),
                                            comment_id):
        question_data = data_manager.get_single_entry('question', question_id)
        answer_data = None
        if comment_data['answer_id']:
            answer_id = comment_data['answer_id']
            answer_data = data_manager.get_single_entry('answer', answer_id)

        if request.method == 'GET':
            return render_template('database_ops/new_comment.html',
                                   comment=comment_data,
                                   answer=answer_data,
                                   question=question_data)

        new_comment_message = request.form['message']
        data_manager.update_comment_message(comment_data, new_comment_message)

    return redirect(url_for('display_question_and_answers',
                            question_id=question_id),
                    code=307)