def add_post(): form = AddPost(request.form) if request.method == "POST" and form.validate(): user = User.query.filter_by(username=session.get("username")).first_or_404() post = Post(user.username, form.title.data, markdown.markdown(form.body.data)) db.session.add(post) db.session.commit() for tag in form.tags.data.split(", "): tag = Tag.get_or_create(tag) tag.posts.append(post) db.session.commit() flash("Successfully added post") return redirect(url_for("show_post", slug=post.slug)) return render_template("add_post.html", form=form)
def edit_post(slug): post = Post.query.filter_by(slug=slug).first_or_404() form = EditPost(request.form, obj=post) if request.method == "POST" and form.validate(): form.populate_obj(post) user = User.query.filter_by(username=session.get("username")).first_or_404() post.author = user.username post.title = form.title.data post.body = markdown.markdown(form.body.data) db.session.add(post) db.session.commit() if form.tags.data: for tag in form.tags.data.split(", "): tag = Tag.get_or_create(tag) tag.posts.append(post) db.session.commit() flash("Successfully updated post") return redirect(url_for("show_post", slug=post.slug)) return render_template("add_post.html", form=form)