def update_post(id): post = Post.query.get(id) form = UpdatePostForm() if request.method == 'POST': if form.validate_on_submit(): title = request.form.get('title') topic = request.form.get('topic') start_event = request.form.get('start_event') stop_event = request.form.get('stop_event') description = request.form.get('description') if title: post.title = title if topic: post.topic = topic if start_event: post.start = start_event if stop_event: post.end = stop_event if description: post.description = description db.session.add(post) db.session.commit() flash("Post {} successfully update.".format(title)) return redirect('/{}'.format(post.id)) return render_template('update_post.html', post=post, form=form)
def update_post(post_id): post = Post.query.get(post_id) if current_user.id != post.author.id: abort(401) form = UpdatePostForm() if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data db.session.commit() return redirect(url_for("show_post", post_id=post_id)) form.title.data = post.title form.body.data = post.body return render_template("posts/update.html", form=form)
def update_post(): """ function to update existing post """ form = UpdatePostForm() if form.validate_on_submit(): current_user.title = form.title.data current_user.content = form.content.data db.session.commit() flash('Your changes have been saved.', 'success') return redirect(url_for('index')) elif request.method == 'GET': form.title.data = current_user.title form.content.data = current_user.content return render_template('post.html', title='Edit Post', form=form)
def viewPost(post_id): postData = Posts.query.filter_by(id=post_id).first() form = UpdatePostForm() if form.validate_on_submit(): postData.title = form.title.data postData.content = form.content.data db.session.commit() return redirect(url_for('viewPost', post_id=post_id)) elif request.method == "GET": form.title.data = postData.title form.content.data = postData.content return render_template('postDetail.html', title='postDetail', post=postData, form=form, editPost=request.args.get('editPost'))
def edit(id): form = UpdatePostForm() post = Post.query.get(id) if post.user_id == current_user.id: if form.validate_on_submit(): Post.query.filter_by(id=id).update({ 'body': form.body.data, 'title': form.title.data }) db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('post', id=id)) elif request.method == 'GET': post = Post.query.get(id) form.title.data = post.title form.body.data = post.body else: flash('You try edit not your post!', 'danger') return redirect(url_for('posts', id=current_user.id)) return render_template('edit.html', title='Edit', form=form)
def post(post_id): post = Post.query.filter_by(id=post_id).first() form = UpdatePostForm() if current_user.username != post.author.username: flash("Its not your post", category="eror") return redirect(url_for('posts')) elif form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.updatetime = datetime.utcnow() db.session.commit() flash("Post was updated", category="info") return redirect(url_for('post', post_id=post.id)) elif "delete" in request.form: db.session.delete(post) db.session.commit() flash("Post deleted", category="eror") return redirect(url_for('posts')) return render_template('post.html', post=post, form=form)