def add_comment(post_id, parent_id=None): post = Post.query.get_or_404(post_id) parent = Comment.query.get_or_404(parent_id) if parent_id else None form = CommentForm() if form.validate_on_submit(): comment = Comment(post=post, parent=parent, ip=ip2long(request.environ['REMOTE_ADDR'])) form.populate_obj(comment) if g.user: comment.author = g.user db.session.add(comment) db.session.commit() signals.comment_added.send(post) flash(_("Thanks for your comment"), "success") return redirect(comment.url) return render_template("blog/add_comment.html", parent=parent, post=post, form=form)
def post(self, year, month, day, slug): """ add comment """ post = Post.query.get_by_slug(slug) form = self.forms.CommentForm(self.request.arguments) if form.validate(): captcha = form.captcha.data if self.get_secure_cookie("captcha") == captcha: comment = Comment(post=post, ip=self.request.remote_ip) form.populate_obj(comment) if self.current_user: comment.author_id = self.current_user.id db.session.add(comment) db.session.commit() self.redirect(comment.url) return form.captcha.errors.append(self._("Captcha don't match")) self.render("blog/view.html", post=post, form=form) return