def category_page_edit(category_slug): category = Category.query.filter_by(slug=category_slug).first() categories = db_tools.get_categories() features = db_tools.get_featured_items() new_category = Category() if request.method == "POST": if request.form["name"]: new_category.name = request.form["name"] if request.form["slug"]: new_category.slug = request.form["slug"] if not app_tools.valid_slug(new_category.slug): flash("Slug must be only lowercase characters or hyphens (-).") return render_template( "category/category_edit.html", category=new_category, categories=categories, featured_items=features ) category.name = new_category.name category.slug = new_category.slug try: db.session.add(category) db.session.commit() flash("Category %s updated." % category.name) return redirect(url_for("category.category_page", category_slug=category.slug)) except sqlalchemy_exceptions.IntegrityError as error: db.session.rollback() print(error) flash("The category and slug must be unique.") return render_template( "category/category_edit.html", category=new_category, categories=categories, featured_items=features ) else: return render_template( "category/category_edit.html", category=category, categories=categories, featured_items=features )
def category_page_new(): categories = db_tools.get_categories() features = db_tools.get_featured_items() if request.method == "POST": new_category = Category(name=request.form["name"], slug=request.form["slug"]) if not app_tools.valid_slug(new_category.slug): flash("Slug must be only lowercase characters or hyphens (-).") return render_template( "category/category_new.html", category=new_category, categories=categories, featured_items=features ) try: db.session.add(new_category) db.session.commit() flash("Category %s created successfully." % new_category.name) return redirect(url_for(".category_page", category_slug=new_category.slug)) except sqlalchemy_exceptions.IntegrityError as error: print(error) db.session.rollback() flash("The category and slug must be unique.") return render_template("category/category_new.html", categories=categories, featured_items=features) else: return render_template("category/category_new.html", categories=categories, featured_items=features)