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 add_product(name: str, price: float, quantity: int) -> Product: product = Product() product.name = name product.price = price product.quantity = quantity product.save() return product
def create_products(): if not flask.request.json: return flask.jsonify({'error': 'Empty request'}) elif not all(key in flask.request.json for key in ['name', 'seller', 'price', 'address']): return flask.jsonify({'error': 'Bad request'}) session = db_session.create_session() prod = Product() prod.name = flask.request.json['name'] prod.seller = flask.request.json['seller'] prod.price = flask.request.json['price'] prod.address = flask.request.json['address'] session.add(prod) session.commit() return flask.jsonify({'success': 'OK'})
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)