def buy(): if not current_user.is_authenticated: return redirect('/') try: db_sess = db_session.create_session() basket = db_sess.query(Basket).filter( Basket.user_id == current_user.id).all() if len(basket) == 0: return redirect('/basket') order = Order() order.user_id = current_user.id order.bought_at = datetime.datetime.now() db_sess.add(order) for item in basket: product = db_sess.query(Product).filter( Product.product_id == item.product_id).first() product.quantity -= item.quantity if product.quantity < 0: return redirect( url_for('basket', msg=str('Недостаточно товара: ' + product.product_name))) order_detail = OrderDetails() order_detail.order_id = order.order_id order_detail.product_id = item.product_id order_detail.price = product.price order_detail.quantity = item.quantity db_sess.add(order_detail) db_sess.delete(item) db_sess.commit() return redirect('/orders') except Exception as e: print(e) return redirect('/basket')
def add_order(self, total_price, status, goods, user=None, address=None): if goods is None or len(goods) == 0: raise ValueError(f"Can`t create Order with 0 goods in it: {goods}") order = Order(total_price=float(total_price)) if status is int: order.status_id = status '''''' if address: order.address_id = address if user: order.user_id = user '''''' for good in goods: good = self.db_sess.query(Good).filter( Good.id == int(good)).first() order.goods_by_order.append(good) self.db_sess.add(order) self.db_sess.commit() return order
def create_order(products_in_cart): global count_products if current_user.get_id(): count_products = 0 db_sess = db_session.create_session() order = Order() total_cost = sum([ db_sess.query(Product).filter(Product.id == int(i)).first().price for i in products_in_cart.split(",") ]) order.user_id = current_user.get_id() order.order = products_in_cart order.total_price = total_cost order.quantity_of_goods = len(products_in_cart.split(",")) db_sess.add(order) db_sess.query(User).filter(User.id == current_user.get_id()).update( {User.cart: ""}) db_sess.commit() db_sess.close() else: if request.method == "POST": db_sess = db_session.create_session() req = request.form address = req.get("address") phone_number = req.get("phone_number") if address != "" and phone_number != "": if phone_number.isdigit() and len(phone_number) == 11: count_products = 0 unreg_order = UnregOrder() unreg_order.user_phone_number = phone_number unreg_order.address = address unreg_order.order = products_in_cart products = [ db_sess.query(Product).filter( Product.id == int(i)).first() for i in products_in_cart.split(",") ] total_cost = sum([product.price for product in products]) unreg_order.total_price = total_cost db_sess.add(unreg_order) db_sess.commit() db_sess.close() session["cart"] = [] return redirect("/") else: return render_template( "reg.html", products_in_cart=products_in_cart, create_order=True, error_text="Ошибка при вводе данных", count_product=count_products) else: return render_template("reg.html", products_in_cart=products_in_cart, create_order=True, error_text="Нужно заполнить все поля!", count_product=count_products) return render_template("reg.html", products_in_cart=products_in_cart, create_order=True, count_product=count_products) return redirect("/")