def edit_page(post_id): post = Post.query.filter_by(post_id=post_id).first_or_404() form = PostForm(obj=post) if form.validate_on_submit(): original_image = post.image form.populate_obj(post) if form.image.has_file(): image = request.files.get("image") try: filename = uploaded_images.save(image) except: flash("The image was not uploaded") if filename: post.image = filename else: post.image = original_image if form.new_category.data: new_category = Category(form.new_category.data) db.session.add(new_category) db.session.flush() post.category = new_category db.session.commit() return redirect(url_for("article_page", slug=post.slug)) return render_template("blog/post.html", form=form, action="edit", post=post)
def post_page(): form = PostForm() if form.validate_on_submit(): # import pdb; pdb.set_trace() image = request.files.get("image") filename = None try: filename = uploaded_images.save(image) except Exception as e: flash("The image was not uploaded! ") print(e) if form.new_category.data: new_category = Category(form.new_category.data) db.session.add(new_category) db.session.flush() category = new_category else: category = form.category.data try: blog = Blog.query.first() blog_id = blog.blog_id user = User.query.filter_by(username=session["username"]).first() author = user.user_id print(str(author)) title = form.title.data body = form.body.data slug = slugify(form.title.data) post = Post(blog_id, author, title, body, category, slug, filename) db.session.add(post) db.session.commit() return redirect(url_for("article_page", slug=slug)) except Exception as e: print(e) error = "error found while committing to database" return render_template("blog/post.html", form=form, error=error) return render_template("blog/post.html", form=form, action="new")