Exemplo n.º 1
0
def add_answer(question_id):
    try:
        question = question_data_manager.get_question(question_id)
    except ValueError:
        abort(404)
    if request.method == 'GET':
        session['prev'] = request.url
        return render_template('new_answer.html', question=question)
    elif request.method == 'POST':
        if len(request.form.get('message')) < 10:
            flash('Your answer isn\'t long enough!')
            return render_template('new_answer.html',
                                   question=question,
                                   form_message=request.form.get('message'))
        else:
            answer = {
                'submission_time': int(time.time()),
                'vote_number': 0,
                'question_id': question_id,
                'message': request.form.get('message'),
                'image': request.files.get('image').filename,
                'user_name': session.get('user_name')
            }
            answer_data_manager.new_answer(answer)
            return redirect(
                url_for('display_question', question_id=question_id))
Exemplo n.º 2
0
def new_comment_for_question(question_id):
    try:
        question = question_data_manager.get_question(question_id)
    except ValueError:
        abort(404)
    if request.method == 'GET':
        session['prev'] = request.url
        return render_template('new_comment.html', entry=question)
    elif request.method == 'POST':
        if len(request.form.get('message')) < 10:
                flash('Your comment isn\'t long enough!')
                return render_template(
                    'new_comment.html', entry=question, form_message=request.form.get('message')
                )
        else:
            comment = {
                'question_id': question_id,
                'answer_id': None,
                'message': request.form.get('message'),
                'submission_time': int(time.time()),
                'edit_count': 0,
                'user_name': session.get('user_name')
            }
            comment_data_manager.new_comment(comment)
            return redirect(
                url_for('display_question', question_id=question_id)
            )
Exemplo n.º 3
0
def edit_answer(answer_id):
    try:
        answer = answer_data_manager.get_answer(answer_id)
    except ValueError:
        abort(404)
    question_id = answer.get('question_id')
    question = question_data_manager.get_question(question_id)
    if request.method == 'GET':
        session['prev'] = request.url
        return render_template('new_answer.html',
                               question=question,
                               form_message=answer.get('message'))
    elif request.method == 'POST':
        if len(request.form.get('message')) < 10:
            flash('Your answer isn\'t long enough!')
            return render_template('new_answer.html',
                                   question=question,
                                   form_message=request.form.get('message'))
        else:
            answer['submission_time'] = int(time.time())
            answer['message'] = request.form.get('message')
            answer['image'] = request.files.get('image').filename
            answer_data_manager.update_answer(answer)
            return redirect(
                url_for('display_question', question_id=question_id))
Exemplo n.º 4
0
def edit_comment(comment_id):
    try:
        comment = comment_data_manager.get_comment(comment_id)
    except ValueError:
        abort(404)
    question_id = get_question_id(comment_id)
    if comment['answer_id'] is None:
        session['prev'] = request.url
        entry = question_data_manager.get_question(comment['question_id'])
    else:
        entry = answer_data_manager.get_answer(comment['answer_id'])
    if request.method == 'GET':
        return render_template('new_comment.html', entry=entry, form_message=comment.get('message'))
    elif request.method == 'POST':
        if len(request.form.get('message')) < 10:
                flash('Your comment isn\'t long enough!')
                return render_template(
                    'new_comment.html', entry=entry, form_message=request.form.get('message')
                )
        else:
            comment['submission_time'] = int(time.time())
            comment['message'] = request.form.get('message')
            comment['edit_count'] += 1
            comment_data_manager.update_comment(comment)
            return redirect(url_for('display_question', question_id=question_id))
Exemplo n.º 5
0
def edit_question(question_id):
    try:
        question = question_data_manager.get_question(question_id)
    except ValueError:
        abort(404)
    form_title = question['title']
    form_message = question['message']
    if request.method == 'GET':
        session['prev'] = request.url
        return render_template('new_question.html',
                               form_title=form_title,
                               form_message=form_message)
    elif request.method == 'POST':
        if len(request.form.get('message')) < 10:
            flash('Your question isn\'t long enough!')
            return render_template('new_question.html',
                                   form_title=request.form.get('title'),
                                   form_message=request.form.get('message'))
        else:
            question['title'] = request.form.get('title')
            question['message'] = request.form.get('message')
            question['submission_time'] = int(time.time())
            question_data_manager.update_question(question)
            return redirect(
                url_for('display_question', question_id=question_id))
Exemplo n.º 6
0
def get_entry(entry_type, entry_id):
    try:
        if entry_type == 'question':
            entry = question_data_manager.get_question(entry_id)
        elif entry_type == 'answer':
            entry = answer_data_manager.get_answer(entry_id)
        elif entry_type == 'comments':
            entry = comment_data_manager.get_comment(entry_id)
    except ValueError:
        abort(404)
    return entry
Exemplo n.º 7
0
def upvote_question(question_id):
    try:
        question = question_data_manager.get_question(question_id)
    except ValueError:
        abort(404)
    question['vote_number'] += 1
    question_data_manager.update_question(question)
    flash('Successfully upvoted this question!')
    if request.args.get('next') is not None:
        return redirect(url_for('display_question', question_id=question_id))
    else:
        return redirect(url_for('index'))
Exemplo n.º 8
0
def display_one_question(question_id):
    session['prev'] = request.url
    try:
        question = question_data_manager.get_question(question_id)
    except ValueError:
        abort(404)
    if 'visited_questions' in session:
        if question_id not in session['visited_questions']:
            update_view_count(question_id, question)
    else:
        session['visited_questions'] = list()
        update_view_count(question_id, question)
    answers = answer_data_manager.get_answers(question_id)
    for answer in answers:
        answer['comments'] = comment_data_manager.get_comments_for_answer(answer['id'])
    question['answer_count'] = len(answers)
    question['comments'] = comment_data_manager.get_comments_for_question(question_id)
    return render_template('question.html', question=question, answers=answers)
Exemplo n.º 9
0
def accepted_answer(answer_id):
    try:
        answer = answer_data_manager.get_answer(answer_id)
    except ValueError:
        abort(404)
    question = question_data_manager.get_question(answer['question_id'])
    question_id = question['id']
    if session['user_name'] != question['user_name']:
        flash('This is not your question!')
        return redirect(session['prev'])
    else:
        if question['has_accepted_answer'] is True:
            flash('There is already an accepted answer!')
            return redirect(session['prev'])
        elif answer['accepted_answer'] is False:
            answer['accepted_answer'] = True
            question['has_accepted_answer'] = True
            answer_data_manager.update_answer(answer)
            question_data_manager.update_question(question)
            return redirect(
                url_for('display_question', question_id=question_id))