def answer(qid, aid): answer = Answer.query.filter_by(id=aid).first_or_404() question = Question.query.filter_by(id=answer.answer_question.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') if (datetime.datetime.now()-answer.created_at).days == 0: answer_created_time = datetime.datetime.strftime(answer.created_at, '%H:%M') else: answer_created_time = datetime.datetime.strftime(answer.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.answer', qid=answer.answer_question.id, aid=answer.id)) question_comments = question.question_comments form_answer_comment = CommentForm(prefix="form_answer_comment") if form_answer_comment.validate_on_submit() and form_answer_comment.submit.data: comment = Comment(content=form_answer_comment.content.data) comment.comment_answer = answer 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.answer', qid=answer.answer_question.id, aid=answer.id)) answer_comments = answer.answer_comments return render_template('answer.html', question=question, answer=answer, tags=tags, created_time=created_time, answer_created_time=answer_created_time, form_question_comment=form_question_comment, question_comments=question_comments, form_answer_comment=form_answer_comment, answer_comments=answer_comments)
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)