Esempio n. 1
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = CreatePostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            flash(f'update post error {e}', 'danger')
        else:
            flash(f'update post success', 'success')
        finally:
            return redirect(url_for('posts.post', post_id=post.id))

    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
    return render_template('create_post.html',
                           title=post.title,
                           form=form,
                           legend='Update Post')
Esempio n. 2
0
def new_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash("Your post has been posted")
        return redirect(url_for('application.home'))
    return render_template('create_post.html', title="New Post", form=form)
Esempio n. 3
0
def create_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created successfully', 'success')
        return redirect(url_for('main.home'))
    return render_template('create_post.html',
                           title='Add New Post',
                           form=form,
                           legend='Create New Post')
Esempio n. 4
0
def create_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        # ! If you don't add post.user_id it fails the NOT NULL integrity constant.
        # ? We can set this using the author backref.
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash("Post created succesfully!", "success")
        return redirect(url_for("main.home"))
    return render_template("create_post.html",
                           title="Create Post",
                           form=form,
                           legend="Create Post")
Esempio n. 5
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = CreatePostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('Your post has been updated successfully', 'success')
        return redirect(url_for('main.home', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
    return render_template('create_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post')
Esempio n. 6
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if current_user != post.author:
        abort(403)
    form = CreatePostForm()
    if form.validate_on_submit():
        post.content = form.content.data
        post.title = form.title.data
        db.session.commit(
        )  #already in database, dont need to add, only modify exiting database
        flash('Post has been updated!', 'success')
        return redirect(url_for('posts.post', post_id=post.id))
    elif request.method == 'GET':
        form.content.data = post.content
        form.title.data = post.title
    return render_template('create_post.html',
                           title="Update Post",
                           form=form,
                           legend="Update Post")
Esempio n. 7
0
def create_post():
    form = CreatePostForm()
    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        author = current_user
        post = Post(title=title,
                    date_posted=datetime.today().ctime(),
                    content=content,
                    author=author)
        try:
            db.session.add(post)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            flash(f'create post error {e}', 'danger')
        else:
            flash(f'Post {form.title.data} has been created!', 'success')
            return redirect(url_for('main.index'))
    return render_template('create_post.html',
                           title='New Post',
                           form=form,
                           legend='New Post')