def add_product(): if request.method == "POST": db_sess = db_session.create_session() req = request.form name = req.get("name") image = req.get("image") price = req.get("price") description = req.get("description") if name and price and image: if price.isdigit(): product = Product() product.name = name product.image = image product.price = price product.description = description db_sess.add(product) db_sess.commit() db_sess.close() return redirect('/admin') else: return render_template("admin_create_product.html", wrong_data=True) else: return render_template("admin_create_product.html", empty_form=True) return render_template("admin_create_product.html")
def create(): if current_user.is_admin: form = ProductForm() if form.validate_on_submit(): product = Product() product.name = form.name.data product.description = form.description.data product.price_cents = int(form.price.data * 100) product.picture_url = form.picture_url.data db_sess = db_session.create_session() db_sess.add(product) db_sess.commit() return redirect(url_for('.details', product_id=product.id)) return render_template('products/new.html', form=form) return redirect('/')
def change_product(product_id): if request.method == "POST": db_sess = db_session.create_session() req = request.form name = req.get("name") image = req.get("image") price = req.get("price") description = req.get("description") if name and image and price: if price.isdigit(): product = Product() product.name = name product.image = image product.price = price product.description = description db_sess.query(Product).filter(Product.id == product_id).update( { Product.name: product.name, Product.image: product.image, Product.price: product.price, Product.description: description }) db_sess.commit() db_sess.close() return redirect("/admin") else: return render_template("admin_change_product.html", wrong_data=True) else: return render_template("admin_change_product.html", empty_form=True) db_sess = db_session.create_session() product = db_sess.query(Product).filter(Product.id == product_id).first() return render_template("admin_change_product.html", product_id=product_id, name=product.name, image=product.image, price=product.price, description=product.description)