def edit(comment_id): comment = Comment.query.get_or_404(comment_id) #comment.permissions.edit.test(403) form = CommentForm(obj=comment) if form.validate_on_submit(): form.populate_obj(comment) comment.save() flash(u"你的评论已更新", "successfully") return redirect(comment.url) return render_template("comment/edit.html", comment=comment, form=form)
def post_comments(): form = CommentForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): new_comment = Comment() new_comment.user_id = request.json["user_id"] new_comment.event_id = request.json["event_id"] form.populate_obj(new_comment) db.session.add(new_comment) db.session.commit() return new_comment.to_dict() return 'Bad Data'
def modify_question(comment_id): comment = Comment.query.get_or_404(comment_id) if g.user != comment.user: flash('수정권한이 없습니다') return redirect( url_for('question.detail', question_id=comment.question.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( url_for('question.detail', question_id=comment.question.id)) else: form = CommentForm(obj=comment) return render_template('comment/comment_form.html', form=form)
def add_comment(post_id, parent_id=None): post = Post.query.get_or_404(post_id) post.permissions.view.test(403) parent = Comment.query.get_or_404(parent_id) if parent_id else None #user = User.query.first() form = CommentForm() if form.validate_on_submit(): comment = Comment(post=post, parent=parent, author=current_user) form.populate_obj(comment) comment.save() post.num_comments += 1 post.save() save_action(u"评论了条目 " + u'"' + post.title + u'"' ) #comment_added.send() flash(u"谢谢你的评论", "successfully") author = parent.author if parent else post.author if author.email_alerts and author.id != current_user.id: if setting.MAIL_ENABLE: subject = u"有人回复了你的评论" if parent else \ u"有人给你的提交添加了评论" template = "emails/comment_replied.html" if parent else \ "emails/post_commented.html" body = render_template(template, author=author, post=post, parent=parent, comment=comment) mail.send_message(subject=subject, body=body, recipients=[post.author.email]) flash(u"谢谢,你的邮件已发出", "successfully") else: flash(u"邮件服务器未开启,请联系管理员", "error") return redirect(comment.url) return render_template("post/add_comment.html", parent=parent, post=post, form=form)
def add_comment(post_id, parent_id=None): post = Post.query.get_or_404(post_id) post.permissions.view.test(403) parent = Comment.query.get_or_404(parent_id) if parent_id else None #user = User.query.first() form = CommentForm() if form.validate_on_submit(): comment = Comment(post=post, parent=parent, author=current_user) form.populate_obj(comment) comment.save() post.num_comments += 1 post.save() save_action(u"评论了条目 " + u'"' + post.title + u'"') #comment_added.send() flash(u"谢谢你的评论", "successfully") author = parent.author if parent else post.author if author.email_alerts and author.id != current_user.id: if setting.MAIL_ENABLE: subject = u"有人回复了你的评论" if parent else \ u"有人给你的提交添加了评论" template = "emails/comment_replied.html" if parent else \ "emails/post_commented.html" body = render_template(template, author=author, post=post, parent=parent, comment=comment) mail.send_message(subject=subject, body=body, recipients=[post.author.email]) flash(u"谢谢,你的邮件已发出", "successfully") else: flash(u"邮件服务器未开启,请联系管理员", "error") return redirect(comment.url) return render_template("post/add_comment.html", parent=parent, post=post, form=form)
def new_comment(postId): form = CommentForm() data = request.get_json(force=True) form['csrf_token'].data = request.cookies['csrf_token'] # form['userId'].data = '1' form['text'].data = data['data'] form['userId'].data = current_user.id form['postId'].data = postId if form.validate_on_submit(): newComment = Comment() form.populate_obj(newComment) db.session.add(newComment) db.session.commit() return { "postId": postId, "text": data['data'], "userId": current_user.id } return {'errors': validation_errors_to_error_messages(form.errors)}, 401
def new_comment(): # data = request.get_json() # print(data,'balls') # userId = data['userId'] # postId = data['postId'] # commentText = data['commentText'] # # userId = data['userId'] # new_comment = Comment(commentText=commentText, postId=postId,userId=userId) # db.session.add(new_comment) # db.session.commit() form = CommentForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): data = Comment() form.populate_obj(data) data.userId = current_user.id db.session.add(data) db.session.commit() return data.to_dict() return 'bad data'