Beispiel #1
0
def edit_post(blog_id):
    # find the blog in the DB
    post = BlogPost.query.get(blog_id)
    if post:
        # if the post exists, fill in the fields in the form
        post_form = forms.CreatePostForm(title=post.title,
                                         subtitle=post.subtitle,
                                         img_url=post.img_url,
                                         author=current_user,
                                         body=post.body)
        # update the DB
        if post_form.validate_on_submit():
            post.title = post_form.title.data
            post.subtitle = post_form.subtitle.data
            post.img_url = post_form.img_url.data
            post.body = post_form.body.data
            db.session.commit()
            # redirect to the post page
            return redirect(f"/post/{post.id}")
        return render_template("make-post.html",
                               heading="Edit Post",
                               form=post_form,
                               year=get_current_year(),
                               logged_in=current_user.is_authenticated)
    # if no such post exists, just redirect to the main page
    return redirect("/")
def add_new_post():
    form = forms.CreatePostForm()
    if form.validate_on_submit():
        new_post = BlogPost(title=form.title.data,
                            subtitle=form.subtitle.data,
                            body=form.body.data,
                            img_url=form.img_url.data,
                            author=current_user,
                            date=date.today().strftime("%B %d, %Y"))
        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for("get_all_posts"))
    return render_template("make-post.html",
                           form=form,
                           logged_in=current_user.is_authenticated,
                           is_edit=False)
Beispiel #3
0
def add_post():
    post_form = forms.CreatePostForm()
    if post_form.validate_on_submit():
        new_post = BlogPost(title=post_form.title.data,
                            subtitle=post_form.subtitle.data,
                            date=get_current_date(),
                            body=post_form.body.data,
                            author=current_user,
                            img_url=post_form.img_url.data)
        db.session.add(new_post)
        db.session.commit()
        return redirect("/")
    return render_template("make-post.html",
                           heading="New Post",
                           form=post_form,
                           year=get_current_year(),
                           logged_in=current_user.is_authenticated)
Beispiel #4
0
def create_post(slug, id):
    try:
        board = Board.query.filter(Board.slug == slug).one()
    except sql_exc:
        return redirect(url_for('.index'))
    try:
        thread = Thread.query.filter(Thread.id == id).one()
    except sql_exc:
        return redirect(url_for('.board', slug=slug))

    form = forms.CreatePostForm()
    if form.validate_on_submit():
        p = Post(content=form.content.data, author=current_user)
        thread.posts.append(p)
        db.session.flush()
        thread.updated = p.created
        db.session.commit()

        return redirect(url_for('.thread', slug=slug, id=id))

    return render_template('forum/create_post.html',
                           board=board,
                           thread=thread,
                           form=form)