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 = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        if form.picture.data:
            picture_file = save_post_picture(form.picture.data)
            post = Post(title=form.title.data,
                        content=form.content.data,
                        author=current_user,
                        post_image_file=picture_file)
        else:
            post = Post(title=form.title.data,
                        content=form.content.data,
                        author=current_user)
        db.session.commit()
        flash("Your post has been updated !", "success")
        return redirect(url_for('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="Update Post",
                           form=form,
                           legend='Update Post')
Esempio n. 2
0
def new_post():
    form = PostForm()
    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!', 'success')
        return redirect(url_for('home'))
    return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def create_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    description=form.description.data,
                    user_id=current_user.id)
        db.session.add(post)
        db.session.commit()
        flash(f'Post has been created', 'success')
        return redirect(url_for('home'))
    return render_template('create_post.html',
                           title="Add Post",
                           form=form,
                           heading="Create Post")
Esempio n. 4
0
def update_post(slug):
    post = Post.query.filter_by(slug=slug).first_or_404()
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('detail_post', slug=post.slug))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
    return render_template('blog/create_post.html',
                           form=form,
                           legend='Update 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 = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.update(post)
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('post'), post_id=post.id)
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
        
    return render_template('update_post.html', title='Update Post', form=form, legend='Update Post')
def update_post(post_id):
    form = PostForm()
    post = Post.query.get(post_id)
    if current_user != post.author:
        abort(403)

    if form.validate_on_submit():
        post.title = form.title.data
        post.description = form.description.data
        db.session.commit()
        flash('Your post has been updated', 'success')
        return redirect(url_for('home'))
    elif request.method == 'GET':
        form.title.data = post.title
        form.description.data = post.description
    return render_template('create_post.html',
                           title=f'Update Post {post_id}',
                           form=form,
                           heading="Update Post")
Esempio n. 7
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        if form.picture.data:
            print("Hello")
            picture_file = save_post_picture(form.picture.data)
            post = Post(title=form.title.data,
                        content=form.content.data,
                        author=current_user,
                        post_image_file=picture_file)
        else:
            post = Post(title=form.title.data,
                        content=form.content.data,
                        author=current_user)
        db.session.add(post)
        db.session.commit()
        flash("You post has been created", "success")
        return redirect(url_for('home'))
    return render_template('create_post.html',
                           title="New Post",
                           form=form,
                           legend='New Post')