def post(id): post = Post.query.get_or_404(id) form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, author=current_user._get_current_object()) db.session.add(comment) flash('Your comment has been published.') return redirect(url_for('.post', id=post.id, page=-1)) page = request.args.get('page', 1, type=int) if page == -1: page = (post.comments.count() - 1) / \ current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 if page == 0: page = 1 print(page) pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('post.html', posts=[post], form=form, comments=comments, pagination=pagination)
def create_reply(reply_id): form = CommentForm() reply = Reply.query.get_or_404(reply_id) if request.method == 'POST' and form.validate_on_submit(): comment = Comment(user=g.user, content=form.content.data, create_date=datetime.now(), reply=reply) db.session.add(comment) db.session.commit() return redirect('{}#comment_{}'.format( url_for('post.detail', post_id=reply.post.id), comment.id)) return render_template('comment/comment_form.html', form=form)
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 post(id): form = CommentForm() post = Post.query.filter_by(id=id).first_or_404() if form.validate_on_submit(): comment = Comment(body=form.comment.data, author=current_user, source=post) db.session.add(comment) db.session.commit() flash('Comment successful.') return redirect(url_for('post', id=id)) comments = post.comments return render_template('idpost.html', post=post, form=form, comments=comments)
def modify_post(comment_id): comment = Comment.query.get_or_404(comment_id) if g.user != comment.user: flash('수정권한이 없습니다') return redirect(url_for('post.detail', post_id=comment.post.id)) if request.method == 'POST': form = CommentForm() if form.validate_on_submit(): form.populate_obj(comment) comment.modify_date = datetime.now() # 수정일시 저장 db.session.commit() return redirect('{}#comment_{}'.format( url_for('post.detail', post_id=comment.post.id), comment.id)) else: form = CommentForm(obj=comment) return render_template('comment/comment_form.html', form=form)
def comment(): form = CommentForm() page = request.url post_id = request.args.get('post_id') post = Post.query.filter_by(id=post_id).first_or_404() body = request.args.get('body') comments = Comment.query.filter_by(post_id=post_id).order_by(Comment.timestamp.desc()) #comments = comments.query.order_by(comments.timestamp.desc()) if form.validate_on_submit(): language = guess_language(form.comment.data) if language == 'UNKNOWN' or len(language) > 5: language = '' comment = Comment(body=form.comment.data, author=current_user, language=language, post_id=post_id) db.session.add(comment) db.session.commit() flash(_('Your comment is now live!')) return redirect(page) #(url_for('main.explore')) return render_template('comment.html', title=_('Comment'), form=form, body=body, post=post, comments=comments)
def get_article(news_id): article = Article.query.filter_by(id=news_id).first() form = CommentForm() if article is not None: with open(article.text) as file: text = file.read() if form.validate_on_submit(): comment = Comment(author=form.name.data, content=form.comment.data, article_id=article.id) article.Comments.append(comment) db.session.add(comment) db.session.commit() return render_template('Article.html', title=article.title, article=article, text=text, form=form, comments=article.Comments) else: abort(404, message='Article not found')
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)