Exemple #1
0
def view_post(post_id, slug=None):
    post = Post.query.filter_by(id=post_id).first()

    # abort if no post is found
    if not post:
        abort(404)

    # if you do not initialize the form, it will raise an error when user is
    # not registered
    form = None
    # check if the current user is authenticated
    if current_user.is_authenticated():
        # assign the `form` variable the `CommentForm` class
        form = CommentForm()

        # check if the form has any errors and is a `POST` request
        if form.validate_on_submit():
            # save the post
            form.save(current_user, post)
            flash(_("Your comment has been saved!"), "success")

            # and finally redirect to the post
            return redirect(url_for("blog.view_post", post_id=post.id,
                                    slug=post.slug))

    return render_template("blog/post.html", post=post, form=form)
Exemple #2
0
def new_comment(post_id):
    post = Post.query.filter_by(id=post_id).first()

    if not post:
        abort(404)

    form = CommentForm()
    if form.validate_on_submit():
        form.save(current_user, post)
        flash(_("Your comment has been saved!"), "success")
        return redirect(url_for("blog.view_post", post_id=post.id,
                                slug=post.slug))

    return render_template("blog/comment_form.html", post=post, form=form,
                           mode="new")
Exemple #3
0
def edit_comment(comment_id):
    comment = Comment.query.filter_by(id=comment_id).first()

    if not comment:
        abort(404)

    if not can_modify(comment, current_user):
        flash(_("You are not allowed to edit this comment"), "danger")
        return redirect(url_for("blog.view_post", post_id=comment.post.id,
                                slug=comment.post.slug))

    form = CommentForm()
    if form.validate_on_submit():
        form.populate_obj(comment)
        comment.save()
        flash(_("Your comment has been edited."), "success")
        return redirect(url_for("blog.view_post", post_id=comment.post.id,
                                slug=comment.post.slug))
    else:
        form.content.data = comment.content

    return render_template("blog/comment_form.html", form=form, mode="edit")