Ejemplo n.º 1
0
def add_comment(post_id, parent_id=None):
    is_login()
    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,
                              recipients=[post.author.email])


        return redirect(comment.url)
    
    return render_template("post/add_comment.html",
                           parent=parent,
                           post=post,
                           form=form)
Ejemplo n.º 2
0
def edit(comment_id):
    is_login()
    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)