Example #1
0
def add_new_comment_to_answer(answer_id: int):
    question_id = data_manager.get_question_id_by_answer_id(answer_id)
    question_id = question_id[0].get('id')
    user_name = data_manager.get_answers_for_questions(question_id)
    answer_data = data_manager.get_answer_by_id(answer_id)
    question_data = data_manager.get_question_by_id(question_id)
    answer_comment = data_manager.get_comments_for_answer(answer_id)
    if request.method == 'POST':
        if 'username' in session:
            username = session['username']
            user_id = data_manager.get_user_id_by_name(username)
            site_input = [
                answer_id, request.form['comment'], 'answer',
                user_id[0].get('id')
            ]
            data_manager.add_comment(site_input)
        else:
            site_input = [answer_id, request.form['comment'], 'answer', None]
            data_manager.add_comment(site_input)
        return redirect(
            url_for('add_new_comment_to_answer', answer_id=answer_id))

    return render_template('add_answer_comment.html',
                           answer_data=answer_data,
                           answer_comment=answer_comment,
                           question_id=question_id,
                           question_data=question_data,
                           user_name=user_name)
Example #2
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))
def route_add_voted_planet_in_db():
    input_planet_name = request.json['value']
    input_username = request.json['username']
    input_user_id = data_manager.get_user_id_by_name(input_username)
    all_planets_with_user_id = data_manager.get_planet_name_and_user_id()

    vote_in_db = False
    for entry in all_planets_with_user_id:
        if input_planet_name == entry[
                'planet_name'] and input_user_id == entry['user_id']:
            vote_in_db = True

    if vote_in_db == False:
        data_manager.insert_vote(input_planet_name, input_user_id)
    return redirect(url_for('index_page'))
Example #4
0
def add_question():
    if request.method == 'GET':
        return render_template('add_a_question.html')

    if 'username' in session:
        username = session['username']
        user_id = data_manager.get_user_id_by_name(username)
        site_input = [
            request.form['title'], request.form['message'],
            user_id[0].get('id')
        ]
        data_manager.add_question(site_input)
    else:
        site_input = [request.form['title'], request.form['message'], None]
        data_manager.add_question(site_input)
    return redirect(url_for('show_all_questions'))
Example #5
0
def add_answer(question_id: int):
    answer_data = data_manager.get_question_by_id(question_id)
    if request.method == 'POST':
        if 'username' in session:
            username = session['username']
            user_id = data_manager.get_user_id_by_name(username)
            site_input = request.form['new-answer']
            data_manager.add_answer(site_input, user_id[0].get('id'),
                                    question_id)
        else:
            site_input = request.form['new-answer']
            data_manager.add_answer(site_input, None, question_id)
        return redirect(
            url_for('show_specific_question', question_id=question_id))

    return render_template('add_answer.html',
                           question_id=question_id,
                           answer_data=answer_data)
Example #6
0
def add_new_comment_to_question(question_id: int):
    if request.method == 'GET':
        question = data_manager.get_question_details(question_id)
        user_name = data_manager.get_answers_for_questions(question_id)
        return render_template('add_question_comment.html',
                               question=question,
                               user_name=user_name)

    if 'username' in session:
        username = session['username']
        user_id = data_manager.get_user_id_by_name(username)
        site_input = [
            question_id, request.form['comment'], 'question',
            user_id[0].get('id')
        ]
        data_manager.add_comment(site_input)
    else:
        site_input = [question_id, request.form['comment'], 'question', None]
        data_manager.add_comment(site_input)

    return redirect(
        url_for('add_new_comment_to_question', question_id=question_id))