Example #1
0
def vote_answers(vote_method, answer_id):
    answer = data_manager.get_answer_by_answer_id(answer_id)
    question_id = answer["question_id"]

    user_name = session["user"]
    user = data_manager.get_user_id_by_name(user_name)
    user.update({"user_name": user_name, "vote_method": vote_method})

    if data_manager.check_if_user_voted_on_answer(user_name, answer_id):
        result = data_manager.check_if_user_voted_on_answer(
            user_name, answer_id)
        voted = data_manager.delete_vote_on_answer_from_votes_db(
            result, vote_method)
        if voted:
            data_manager.vote_answer(vote_method, answer_id)

            author = data_manager.get_author_by_answer_id(
                answer_id)["user_name"]
            author_repu = data_manager.get_reputation(author)
            new_repu = data_manager.annul_calc_reputation(
                "answer", vote_method, author_repu)
            data_manager.update_user_reputation(author, new_repu)
        return redirect(f'/questions/{question_id}')
    else:
        author = data_manager.get_author_by_answer_id(answer_id)["user_name"]
        author_repu = data_manager.get_reputation(author)
        new_repu = data_manager.calculate_reputation("answer", vote_method,
                                                     author_repu)
        data_manager.update_user_reputation(author, new_repu)

        data_manager.create_vote_on_answer_in_votes_db(answer_id, user)
        data_manager.vote_answer(vote_method, answer_id)
        return redirect(url_for("manage_questions", question_id=question_id))
Example #2
0
def edit_answer(answer_id):
    current_answer = data_manager.get_answer_by_answer_id(answer_id)
    question_id = current_answer['question_id']
    if request.method == "POST":
        update_answer = dict(request.form)
        data_manager.update_answer(answer_id, update_answer)
        return redirect(f'/questions/{question_id}')

    return render_template("edit-answer.html",
                           answer_id=answer_id,
                           answer=current_answer)
Example #3
0
def show_answer_and_comments(answer_id):
    question_id = request.args.get("question_id")
    answer = data_manager.get_answer_by_answer_id(answer_id)
    comments_for_answer = data_manager.get_comments_for_answer(answer_id)

    return render_template('display_answer_with_comments.html',
                           comments=comments_for_answer,
                           answer=answer,
                           answer_id=answer_id,
                           question_id=question_id,
                           title="Answer and comments")
Example #4
0
def add_comment_to_answer(answer_id):
    if request.method == 'POST':
        user_id = data_manager.get_user_id_by_username(session['username'])
        comment_message = request.form['message']
        data_manager.add_comment_to_answer(answer_id, comment_message, user_id)
        question_id = data_manager.get_question_id_by_answer_id(answer_id)
        return redirect(url_for('route_question', question_id=question_id))
    question = data_manager.get_question_by_answer_id(answer_id)
    answer = data_manager.get_answer_by_answer_id(answer_id)
    return render_template('add-comment-to-answer.html',
                           question=question,
                           answer=answer)
Example #5
0
def answer_edit(answer_id):
    answer = data_manager.get_answer_by_answer_id(answer_id)
    question_id = answer['question_id']
    question = data_manager.get_question_by_question_id(question_id)
    try:
        if request.method == 'GET':
            return render_template('edit-answer.html', answer=answer, question_id=question_id, question=question)
        elif request.method == 'POST':
            submission_time = datetime.now().isoformat(timespec='seconds')
            message = request.form['message']
            image = request.form['image']
            if image == "":
                image = None
            data_manager.edit_answer(submission_time, message, image, answer_id)
            return redirect(f'/question/{question_id}')
    except IndexError:
        abort(404)
Example #6
0
def edit_answer(answer_id):
    answer_user_id = data_manager.get_user_id_by_answer_id(answer_id)
    if session['user_id'] == answer_user_id:
        if request.method == 'POST':
            edited_answer = dict(request.form)
            new_image = save_file(request.files['image_file'])
            edited_answer['image'] = new_image
            edited_answer['id'] = answer_id
            data_manager.edit_answer(edited_answer)

            question_id = data_manager.get_question_id_by_answer_id(answer_id)
            return redirect(url_for('route_question', question_id=question_id))
        question = data_manager.get_question_by_answer_id(answer_id)
        answer_to_edit = data_manager.get_answer_by_answer_id(answer_id)
        return render_template('edit.html',
                               answer_to_edit=answer_to_edit,
                               question=question)
    else:
        return "You don't have permission to edit this answer"
Example #7
0
def add_comment_to_answer(answer_id):
    answer = data_manager.get_answer_by_answer_id(answer_id)
    if request.method == "POST":
        question_id = None
        message = request.form['message'].capitalize()
        submission_time = datetime.now().isoformat(timespec='seconds')
        edited_count = None
        try:
            user_id = session['id']
        except KeyError:
            login_error = True
            return render_template('add-answer-comment.html', login_error=login_error, question_id=answer['question_id'], answer_id=answer_id,
                                   answer=answer)
        data_manager.add_new_comment(question_id, answer_id, message, submission_time, edited_count, user_id)
        return redirect(url_for('question_page', question_id=answer['question_id']))
    elif request.method == "GET":
        try:
            return render_template('add-answer-comment.html', question_id=answer['question_id'], answer_id=answer_id,
                                   answer=answer)
        except (IndexError, UndefinedError):
            abort(404)
Example #8
0
def delete_answer(answer_id):
    answer = data_manager.get_answer_by_answer_id(answer_id)
    question_id = answer["question_id"]
    data_manager.delete_answer(answer_id)
    return redirect(url_for("manage_questions", question_id=question_id))