示例#1
0
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

    form = CommentForm()

    if form.validate_on_submit():
        comment = Comment(post=post,
                          parent=parent,
                          author=g.user)

        form.populate_obj(comment)

        db.session.add(comment)
        db.session.commit()

        signals.comment_added.send(post)

        flash(_("Thanks for your comment"), "success")

        author = parent.author if parent else post.author

        if author.email_alerts and author.id != g.user.id:

            subject = _("Somebody replied to your comment") if parent else \
                _("Somebody commented on your post")

            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,
                              sender=current_app.config.get(
                                  'DEFAULT_MAIL_SENDER'),
                              recipients=[post.author.email])

        return redirect(comment.url)

    return render_template("post/add_comment.html",
                           parent=parent,
                           post=post,
                           form=form)
示例#2
0
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)

        db.session.commit()

        flash(_("Your comment has been updated"), "success")

        return redirect(comment.url)

    return render_template("comment/edit_comment.html",
                           comment=comment,
                           form=form)