예제 #1
0
def answer_edit2(id):
    answer = Answer.query.filter_by(id=id).first()
    form_answer_edit = AddAnswerForm(prefix="form_answer_edit")
    if form_answer_edit.validate_on_submit() and form_answer_edit.submit.data:
        answer.content = form_answer_edit.content.data
        answer.modified_at = datetime.datetime.now()
        db.session.add(answer)
        db.session.commit()
        return redirect(url_for('main.question', id=answer.answer_question.id))
    form_answer_edit.content.data = answer.content
    return render_template('answer_edit.html', form_answer_edit=form_answer_edit, answer=answer)
예제 #2
0
def question(id):
    question = Question.query.filter_by(id=id).first_or_404()
    tags = question.tags.all()
    if (datetime.datetime.now()-question.created_at).days == 0:
        created_time = datetime.datetime.strftime(question.created_at, '%H:%M')
    else:
        created_time = datetime.datetime.strftime(question.created_at, '%Y-%m-%d')
    form_question_comment = CommentForm(prefix="form_question_comment")
    if form_question_comment.validate_on_submit() and form_question_comment.submit.data:
        comment = Comment(content=form_question_comment.content.data)
        comment.comment_question = question
        author = User.query.filter_by(id=current_user.id).first()
        comment.comment_author = author
        db.session.add(comment)
        db.session.commit()
        flash('评论成功')
        return redirect(url_for('main.question', id=question.id))
    question_comments = question.question_comments
    form_add_answer = AddAnswerForm(prefix="form_add_answer")
    if form_add_answer.validate_on_submit() and form_add_answer.submit.data:
        ans = Answer(content=form_add_answer.content.data)
        ans.answer_question = question
        author = User.query.filter_by(id=current_user.id).first()
        ans.answer_author = author
        db.session.add(ans)
        db.session.commit()
        flash('回答成功')
        return redirect(url_for('main.question', id=question.id))
    answers = question.question_answers.all()
    for i in range(1,len(answers)):
        for j in range(0,len(answers)-i):
            if answers[j].users.count() < answers[j+1].users.count():
                answers[j],answers[j+1] = answers[j+1],answers[j]
    return render_template('question.html', question=question, tags=tags, created_time=created_time,
                               form_question_comment=form_question_comment, question_comments=question_comments,
                               form_add_answer=form_add_answer, answers=answers)