Пример #1
0
def edit(post_id):
    post = Post.query.get(post_id)
    if post is not None:
        if post.user == current_user:
            form = EditPostForm(obj=post)
            if form.has_been_submitted(request) and form.validate_on_submit():

                if post.title != form.title.data and not validate_post_title(form.title.data):
                    form.title.errors.append("You already posted an article with the same title today.")
                    return render_template("edit_post.html", form=form)

                post.title = form.title.data
                post.content = form.content.data
                post.set_title_slug()
                saved = post.save()
                if saved:
                    flash("Successfully saved the post.")
                    return redirect(url_for("blog.get", user_slug=current_user.blog_slug, post_slug=post.title_slug))
                else:
                    flash("Something went wrong...")

            return render_template("edit_post.html", form=form,
                                   syntax_highlighter_css=generate_syntax_highlighter_css(current_user))
        else:
            # The user trying to edit is not the actual owner
            flash("Your are not authorized to do that.")
            return redirect(url_for('index'))
    else:
        # The post has not been found
        return render_template("blog/blog_page_404", post_id=post_id)
Пример #2
0
def new():
    form = NewPostForm(request.form, prefix="post")
    if form.has_been_submitted(request):
        if form.validate_on_submit():
            new_post = Post(user=current_user, title=form.title.data, content=form.content.data)
            status = new_post.save()
            if status:
                flash("Successfully posted the article")
                return redirect(url_for('blog.index', user_slug=current_user.blog_slug))
            else:
                flash("Something went wrong...", category="error")

    return render_template("new_post.html", form=form,
                           syntax_highlighter_css=generate_syntax_highlighter_css(current_user))
Пример #3
0
def new():
    form = NewPostForm(request.form, prefix="post")
    if form.has_been_submitted(request):
        if form.validate_on_submit():
            new_post = Post(user=current_user,
                            title=form.title.data,
                            content=form.content.data)
            status = new_post.save()
            if status:
                flash("Successfully posted the article")
                return redirect(
                    url_for('blog.index', user_slug=current_user.blog_slug))
            else:
                flash("Something went wrong...", category="error")

    return render_template(
        "new_post.html",
        form=form,
        syntax_highlighter_css=generate_syntax_highlighter_css(current_user))
Пример #4
0
def edit(post_id):
    post = Post.query.get(post_id)
    if post is not None:
        if post.user == current_user:
            form = EditPostForm(obj=post)
            if form.has_been_submitted(request) and form.validate_on_submit():

                if post.title != form.title.data and not validate_post_title(
                        form.title.data):
                    form.title.errors.append(
                        "You already posted an article with the same title today."
                    )
                    return render_template("edit_post.html", form=form)

                post.title = form.title.data
                post.content = form.content.data
                post.set_title_slug()
                saved = post.save()
                if saved:
                    flash("Successfully saved the post.")
                    return redirect(
                        url_for("blog.get",
                                user_slug=current_user.blog_slug,
                                post_slug=post.title_slug))
                else:
                    flash("Something went wrong...")

            return render_template(
                "edit_post.html",
                form=form,
                syntax_highlighter_css=generate_syntax_highlighter_css(
                    current_user))
        else:
            # The user trying to edit is not the actual owner
            flash("Your are not authorized to do that.")
            return redirect(url_for('index'))
    else:
        # The post has not been found
        return render_template("blog/blog_page_404.html", post_id=post_id)