Beispiel #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)
Beispiel #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)
Beispiel #3
0
def view(post_id, slug=None):
    post = Post.query.get_or_404(post_id)
    if not post.permissions.view:
        if not g.user:
            flash(_("You must be logged in to see this post"), "error")
            return redirect(url_for("account.login", next=request.path))
        else:
            flash(_("You must be a friend to see this post"), "error")
            abort(403)

    def edit_comment_form(comment):
        return CommentForm(obj=comment)

    return render_template("post/post.html",
                           comment_form=CommentForm(),
                           edit_comment_form=edit_comment_form,
                           post=post)
Beispiel #4
0
 def edit_comment_form(comment):
     return CommentForm(obj=comment)