def cart_page(): meals_list = db.session.query(Meal).all() cart = session.get("cart", []) meals = [] for i in cart: meals.append(i) form = CartForm() if request.method == 'POST' and form.validate_on_submit(): name = form.name.data address = form.address.data user_mail = form.user_mail.data phone = form.phone.data date = datetime.date.today().strftime("%d.%m.%Y") status = "Выполняется" order_form = Order(name=name, address=address, user_mail=user_mail, phone=phone, meals=meals, summ=summ(), date=date) db.session.add(order_form) db.session.commit() return redirect('/ordered/') output = render_template('cart.html', form=form, cart=cart, meals_list=meals_list) return output
def render_cart(): cart_form = CartForm() meals = [] trash = session.get('trash', None) if trash != None: trash_meal = db.session.query(Meal).filter( Meal.id == trash).one_or_none() else: trash_meal = None cart = session.get('cart', []) total_price = session.get('total_price', 0) user_cart = {'meals_numb': len(cart), 'total_price': total_price, 'trash_meal': trash_meal} if len(cart) > 0: for meal_id in cart: meal = db.session.query(Meal).filter( Meal.id == meal_id).one_or_none() meals.append(meal) if request.method == 'POST' and cart_form.validate_on_submit(): today = datetime.datetime.today() client_order = { "clientName": cart_form.clientName.data, "clientAddress": cart_form.clientAddress.data, "clientMail": cart_form.clientMail.data, "clientPhone": cart_form.clientPhone.data, "cartSumm": cart_form.cartSumm.data, "cartMeals": cart_form.cartMeals.data } client = User.query.filter_by(id=session['user']['id']).one_or_none() if client and int(client_order['cartSumm']) > 0: order_db = Order( date=today.strftime("%Y-%m-%d-%H:%M"), sum=total_price, status='Ready', user_id=client.id) for meal in meals: order_db.meals.append(meal) db.session.add(order_db) db.session.commit() session['trash'] = [] session['cart'] = [] session['total_price'] = 0 return redirect(url_for('render_ordered')) elif client_order['cartSumm'] == 0: cart_form.clientPhone.errors.append("Добавьте товары в корзину для оформления заказа") else: cart_form.clientMail.errors.append("Неверный адрес электронной почты") return render_template('cart.html', cart=user_cart, form=cart_form, meals=meals, trash=trash, user_access=session.get('is_auth'))
def single_product(product_id): form = CartForm() mycursor.execute( 'SELECT * FROM product WHERE product_id = {}'.format(product_id)) product = mycursor.fetchone() if product == None: abort(404) mycursor.execute('SELECT * FROM store WHERE id = {}'.format( product['store_id'])) store = mycursor.fetchone() product['store_name'] = store['store_name'] product['store_phone'] = store['phone'] image_file = url_for('static', filename='profile_pics/' + product['photo']) if 'whois' in session and session['whois'] == '1': if form.validate_on_submit(): if 'cart' in session: if not any( str(product['product_id']) in d for d in session['cart']): new_dict = {product['product_id']: form.quantity.data} keys_values = new_dict.items() new_d = {str(key): value for key, value in keys_values} new_dict_copy = new_d.copy() session['cart'].append(new_dict_copy) flash("New Product Added to Cart!", 'success') elif any( str(product['product_id']) in d for d in session['cart']): for d in session['cart']: for k, v in d.items(): if k == str(product['product_id']): d.update({k: form.quantity.data}) flash("Quantity updated!", 'success') else: session['cart'] = [{product['product_id']: form.quantity.data}] flash("New Product Added to Cart!", 'success') return redirect(url_for('shopping_cart')) return render_template('single_product.html', image_file=image_file, title=product['product_name'], product=product, form=form)
def render_cart(): form = CartForm() if request.method == "POST": if not session.get("user"): return redirect("/auth/") if not form.validate_on_submit(): return render_template("cart.html", form=form) if not session.get("cart", {}): form.submit.errors.append("Корзина не должна быть пустой") return render_template("cart.html", form=form) order = Order(user_id=session.get("user")["id"], status='Размещен', date=datetime.today(), email=form.email.data) order.items = json.dumps(session.get("cart")) db.session.add(order) db.session.commit() session.pop("cart") session.pop("total_cost") session.pop("total_count") return render_template("ordered.html") session["total_cost"] = sum([ item["price"] * item["count"] for item in session.get("cart", {}).values() ]) session["total_count"] = len(session.get("cart", {})) output = render_template("cart.html", form=form, deleted=session.get("deleted"), total_cost=session.get("total_cost", 0), total_count=session.get("total_count", 0)) session["deleted"] = False return output
def render_cart(): meals = [] if 'cart' not in session: session['cart'] = [] for i in session['cart']: meal = db.session.query(Meal).get(i) meals.append(meal) cart_form = CartForm() if not cart_form.is_submitted() and 'is_auth' in session: client = db.session.query(User).get(session['user_id']) cart_form.client_name.data = client.name cart_form.client_address.data = client.address cart_form.client_mail.data = client.mail cart_form.client_phone.data = client.phone if cart_form.validate_on_submit(): order = Order(order_date=date.today().isoformat(), order_sum=session['total'], phone=cart_form.client_phone.data, address=cart_form.client_address.data, mail=cart_form.client_mail.data, user_id=session['user_id']) for meal_id in session['cart']: meal = db.session.query(Meal).get(meal_id) order.meals.append(meal) db.session.add(order) db.session.commit() session['cart'] = [] session['total'] = 0 return redirect(url_for('store.render_ordered')) return render_template('cart.html', meals=meals, form=cart_form)