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 add_product(): form = ProductsAddForm() if form.validate_on_submit(): db_sess = db_session.create_session() product = Product() product.product = form.product.data product.price = form.price.data current_user.products.append(product) db_sess.merge(current_user) db_sess.commit() return redirect('/index') return render_template('add_product.html', title='Добавление товара', form=form)
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 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)