def display_user_page(user_id):
    user_data = data_manager.get_user_profile_by_id(user_id)
    columns_for_questions = [
        'id', 'submission_time', 'title', 'view_number', 'vote_number',
        'userid'
    ]
    columns_for_answers = [
        'id', 'submission_time', 'message', 'vote_number', 'question_id',
        'userid'
    ]
    columns_for_comment = [
        'id', 'question_id', 'answer_id', 'message', 'submission_time',
        'edited_count', 'userid'
    ]

    questions = data_manager.get_data_by_id(columns_for_questions, 'question',
                                            user_id, 'userid')
    comments_of_question = data_manager.get_data_by_id(columns_for_comment,
                                                       'comment', user_id,
                                                       'userid')
    answers_of_question = data_manager.get_data_by_id(columns_for_answers,
                                                      'answer', user_id,
                                                      'userid')

    return render_template('user_page.html',
                           user_data=user_data,
                           user_id=user_id,
                           questions=questions,
                           answers=answers_of_question,
                           comments=comments_of_question)
def delete_comments(comment_id):
    columns = ['id', 'question_id', 'answer_id']
    comment = data_manager.get_data_by_id(columns, 'comment', comment_id, 'id')
    columns_for_answer = ['id', 'question_id']
    question = data_manager.get_data_by_id(columns_for_answer, 'answer',
                                           comment[0]['answer_id'], 'id')
    data_manager.delete('comment', comment_id, 'id')
    if comment[0]['question_id'] is None:
        return redirect('/question/' + str(question[0]['question_id']))
    else:
        return redirect('/question/' + str(comment[0]['question_id']))
def vote_down_answers(answer_id):
    columns = ['id', 'question_id']
    answer = data_manager.get_data_by_id(columns, 'answer', answer_id, 'id')
    if request.method == 'POST':
        data_manager.decrement_vote_number('answer', answer_id)
        data_manager.lose_reputation('answer', answer_id)
    return redirect('/question/' + str(answer[0]['question_id']))
def vote_down_questions(question_id):
    columns = ['id']
    question = data_manager.get_data_by_id(columns, 'question', question_id,
                                           'id')
    if request.method == 'POST':
        data_manager.decrement_vote_number('question', question_id)
        data_manager.lose_reputation('question', question_id)
    return redirect('/question/' + str(question[0]['id']))
def answer_edit(answer_id):
    columns = ['id', 'message', 'question_id']
    answer = data_manager.get_data_by_id(columns, 'answer', answer_id, 'id')
    if request.method == 'GET':
        return render_template("edit_answer.html", answer=answer)
    if request.method == 'POST':
        message = request.form['message']
        data_manager.update_data('message', 'answer', message, answer_id)
        return redirect('/question/' + str(answer[0]['question_id']))
def delete_questions(question_id):
    data_manager.delete('comment', question_id, 'question_id')
    columns = ['id', 'question_id']
    answers = data_manager.get_data_by_id(columns, 'answer', question_id,
                                          'question_id')
    for answer in answers:
        data_manager.delete('comment', answer['id'], 'answer_id')
    data_manager.delete('answer', question_id, 'question_id')
    data_manager.delete('question_tag', question_id, 'question_id')
    data_manager.delete('question', question_id, 'id')
    return redirect('/')
def edit_comment(comment_id):
    columns = ['id', 'question_id', 'answer_id', 'message', 'edited_count']
    comment = data_manager.get_data_by_id(columns, 'comment', comment_id, 'id')
    if request.method == 'GET':
        return render_template('edit_comment.html', comment=comment)
    elif request.method == 'POST':
        message = request.form['message']
        edited_count = comment[0]['edited_count'] + 1
        data_manager.update_data('message', 'comment', message, comment_id)
        data_manager.update_data('edited_count', 'comment', edited_count,
                                 comment_id)
        if comment[0]['question_id'] is None:
            columns_for_answer = ['id', 'question_id']
            question = data_manager.get_data_by_id(columns_for_answer,
                                                   'answer',
                                                   comment[0]['answer_id'],
                                                   'id')
            return redirect('/question/' + str(question[0]['question_id']))
        else:
            return redirect('/question/' + str(comment[0]['question_id']))
def comment_answer(answer_id):
    columns = ['id', 'question_id']
    question = data_manager.get_data_by_id(columns, 'answer', answer_id, 'id')
    if request.method == 'GET':
        return render_template("answer_comment.html", answer_id=answer_id)
    if request.method == 'POST':
        comment = request.form.get('comment_answer')
        user_id = session['user_id']
        data_manager.add_comment_to_answer(comment, answer_id, 'comment',
                                           user_id)

    return redirect('/question/' + str(question[0]['question_id']))
def display_question(question_id):
    comments_of_answers = {}
    columns_for_questions = [
        'id', 'submission_time', 'title', 'message', 'view_number',
        'vote_number', 'userid'
    ]
    columns_for_answers = [
        'id', 'submission_time', 'message', 'vote_number', 'question_id',
        'userid', 'answer_state'
    ]
    columns_for_comment = [
        'id', 'question_id', 'answer_id', 'message', 'submission_time',
        'edited_count', 'userid'
    ]

    question = data_manager.get_data_by_id(columns_for_questions, 'question',
                                           question_id, 'id')
    comments_of_question = data_manager.get_data_by_id(columns_for_comment,
                                                       'comment', question_id,
                                                       'question_id')
    answers_of_question = data_manager.get_data_by_id(columns_for_answers,
                                                      'answer', question_id,
                                                      'question_id')
    answer_ids = data_manager.get_id_question_or_answer(question_id)

    for answer_id in answer_ids:
        comments_of_answer = data_manager.get_data_by_id(
            columns_for_comment, 'comment', answer_id['id'], 'answer_id')
        comments_of_answers[answer_id['id']] = comments_of_answer
    get_tag = data_manager.get_tags_name(question_id)

    return render_template("question_display.html",
                           id=question_id,
                           question=question,
                           answers=answers_of_question,
                           comments=comments_of_question,
                           comments_of_answers=comments_of_answers,
                           tags=get_tag)
def edit_question(question_id):
    if request.method == 'GET':
        columns = ['id', 'title', 'message']
        question_data = data_manager.get_data_by_id(columns, 'question',
                                                    question_id, 'id')
        return render_template('form.html',
                               form_type=2,
                               question_data=question_data)
    if request.method == 'POST':
        title = request.form['title']
        message = request.form['message']
        data_manager.update_data('message', 'question', message, question_id)
        data_manager.update_data('title', 'question', title, question_id)
        return redirect('/question/' + question_id)
def answer_question(question_id):
    columns_for_questions = [
        'id', 'submission_time', 'title', 'message', 'view_number',
        'vote_number'
    ]
    columns_for_answers = [
        'id', 'submission_time', 'message', 'vote_number', 'question_id'
    ]
    question = data_manager.get_data_by_id(columns_for_questions, 'question',
                                           question_id, 'id')
    limit = None
    answers_of_question = data_manager.get_all_data_from_file(
        columns_for_answers, 'answer', 'submission_time', 'DESC', limit)
    if request.method == 'GET':
        return render_template("new_answer.html",
                               id=question_id,
                               question=question,
                               answers=answers_of_question)
    elif request.method == 'POST':
        message = request.form.get('message')
        user_id = session['user_id']
        data_manager.answer_question(message, question_id, 'answer', user_id)
        return redirect(url_for('display_question', question_id=question_id))
def delete_answers(answers_id):
    columns = ['id', 'question_id']
    answer = data_manager.get_data_by_id(columns, 'answer', answers_id, 'id')
    data_manager.delete('comment', answers_id, 'answer_id')
    data_manager.delete('answer', answers_id, 'id')
    return redirect('/question/' + str(answer[0]['question_id']))
def answer_accept(answer_id):
    columns = ['id', 'message', 'question_id']
    answer = data_manager.get_data_by_id(columns, 'answer', answer_id, 'id')
    data_manager.update_data('answer_state', 'answer', 'accepted', answer_id)
    data_manager.gain_reputation('answer_accept', answer_id)
    return redirect('/question/' + str(answer[0]['question_id']))
Beispiel #14
0
def select_story_by_id(story_id):
    dictified_id = dictify_id(story_id)
    selected_story = data_manager.get_data_by_id(dictified_id)[0]
    return selected_story