def new_order(request): client_json = simplejson.loads(request.data) name = client_json.get('name', '') telephone = client_json.get('telephone', '') email = client_json.get('email', '') if not client_json \ or not name \ or not telephone: return render_json_response({ 'api_success': False, 'api_msg': \ u'Заказ не сформирован, т.к. не заполнена контактная информация.'}, status=401) cart = request.session.get('cart', {}) order_items = get_cart_items(cart) total_price = 0.0 for item in order_items: total_price += item.get('price', 0.0) * item.get('count', 0) pass if not order_items or not total_price: return render_json_response({ 'api_success': False, 'api_msg': u'Корзина пуста или при передаче данных произошла ошибка.\n\ Поробуйте еще раз. %s' % order_items}, status=409) client = Client( name=name, telephone=telephone, email=email, address=client_json.get('address'), is_delivery=client_json.get('delivery', False), ip=request.remote_addr) order = Order(client=client, total_price=total_price) for item in order_items: oi = OrderItem( item_id=item.get('id', ''), item_title=item.get('title', ''), item_url=item.get('url', ''), item_img_url=item.get('img_url', ''), item_price=item.get('price', 0.0), item_count=item.get('count', 0) ) order.items.append(oi) order.put() def txn(): taskqueue.add(url=url_for('order/send_to_manager', order_id=str(order.key.id())), transactional=True) db.run_in_transaction(txn) request.session['cart'] = {} return render_json_response({ 'api_success': True, 'api_msg': u'Заказ успешно сформирован!\n\ В ближайшее время с вами свяжется наш менеджер и уточнит детали заказа.' })
def cart_view(): if request.method == 'POST' and 'order_delete' in request.form: clear_cart() return redirect(url_for('order.cart_view')) cart = session.get('cart', {}) price = cart.get('price', 0) products_count = cart.get('products_count', 0) un_products_count = cart.get('un_products_count', 0) cart_products = cart.get('products', {}) products = [] for product_key in cart_products.keys(): cart_product = cart_products.get(product_key, {}) product = Product.retrieve_by_id(product_key) if product: product.order = cart_product.get('count', 0) product.order_price = cart_product.get('price', 0) products.append(product) if request.method == 'POST' and 'order_accept': order = Order(customer=current_user.user_db.key) price = 0 for product in products: order_product = OrderProduct( name=product.name, img_url='%s=s100' % product.images[0] if product.images else '', product_key=product.key, count=product.order, price=product.order_price, ) price += product.order_price order.products.append(order_product) order.price = price order.put() clear_cart() return render_template( 'order/order_success.html', title=u'Предзаказ оформлен', order=order ) return render_template( 'order/cart.html', title=u'Оформление предзаказа', price=price, products_count=products_count, un_products_count=un_products_count, products=products )