def order_details(db_session, order): db_session.add_all([ OrderDetail(order=order, product_id=1, price=99.51, quantity=1), OrderDetail(order=order, product_id=2, price=30.99, quantity=8) ]) db_session.commit() return order_details
def test_can_create_order_detail(db_session): order = Order() order_detail_1 = OrderDetail( order=order, product_id=1, price=100.50, quantity=1 ) order_detail_2 = OrderDetail( order=order, product_id=2, price=99.50, quantity=2 ) db_session.add_all([order_detail_1, order_detail_2]) db_session.commit() assert order.id > 0 for order_detail in order.order_details: assert order_detail.id > 0 assert order_detail_1.product_id == 1 assert order_detail_1.price == 100.50 assert order_detail_1.quantity == 1 assert order_detail_2.product_id == 2 assert order_detail_2.price == 99.50 assert order_detail_2.quantity == 2
def order_details(db_session, order): db_session.add_all([ OrderDetail( order=order, product_id="shit_vacuum_2000", price=99.51, quantity=1 ), OrderDetail( order=order, product_id="the_enigma", price=30.99, quantity=8 ) ]) db_session.commit() return order_details
def createOrder(self, order_details): order = Order(order_details=[ OrderDetail(product_id=order_detail['product_id'], product_name=order_detail['product_name'], price=order_detail['price'], quantity=order_detail['quantity'], currency=order_detail['currency']) for order_detail in order_details ]) self.order_db.add(order) self.order_db.commit() order = OrderSchema().dump(order).data try: # self.event_dispatcher('order_created', { # 'order': order # }) self.products_rpc.handleOrderCreated({'order': order}) except RemoteError as ex: self.logger.info("orders.createOrder: %s" % ex) raise OutOfStock(str(ex)) # self.logger.info("createOrder: order: %s" % order) # Hide real Id from DB order['hash_id'] = self.hashids.encrypt(order['id']) return order
def orderproduct(request): current_user = request.user form = OrderForm(request.POST or None) shopcart = ShopCart.objects.filter(user_id=current_user.id) total = 0 numItems = 0 subtotal = [] for item in shopcart: numItems += 1 total += item.quantity * item.product.price for item in shopcart: subtotal.append(item.quantity * item.product.price) if request.method == 'POST': if form.is_valid(): data = Order() data.first_name = form.cleaned_data['first_name'] data.last_name = form.cleaned_data['last_name'] data.address = form.cleaned_data['address'] data.city = form.cleaned_data['city'] data.email = form.cleaned_data['email'] data.phone = form.cleaned_data['phone'] data.user_id = current_user.id data.total = total data.save() for item in shopcart: detail = OrderDetail() detail.order_id = data.id detail.user_id = current_user.id detail.product_id = item.product_id detail.quantity = item.quantity detail.price = item.product.price detail.total = item.amount detail.save() ShopCart.objects.filter(user_id=current_user.id).delete() messages.success(request, "Thanks for purchases") return HttpResponseRedirect(reverse("orderproduct", args=None)) context = { 'total': total, 'numItems': numItems, 'shopcart': shopcart, 'subtotal': subtotal, 'form': form, } template = "orders/orderproduct.html" return render(request, template, context)
def order_details(db_session, order): db_session.add_all([ OrderDetail(order=order, product_id="1", price=99.51, quantity=1, product_name="Honda", currency="usd"), OrderDetail(order=order, product_id="2", price=30.99, quantity=8, product_name="Yamaha", currency="usd") ]) db_session.commit() return order_details
def post(self, request): import pdb pdb.set_trace() itemName = request.data.get('itemName') quantity = int(request.data.get('quantity', 0)) try: itemDetails = BakeryItem.objects.filter(itemName=itemName)[0] except: return HttpResponse( json.dumps({ 'result': False, 'message': f'Item {itemName} not present in database' })) if quantity <= 0: return HttpResponse( json.dumps({ 'result': False, 'message': f'Quanity should be greater than 0' })) purchasedAt = itemDetails.sellingPrice * quantity billerName = request.data.get('billerName') billedAddress = request.data.get('billedAddress') orderdetail = OrderDetail(customerId=request.user, itemPurchased=itemDetails, purchasedAt=purchasedAt, billerName=billerName, billedAddress=billedAddress, quantity=quantity) orderdetail.save() itemDetails.quantity -= int(quantity) itemDetails.save() return HttpResponse( json.dumps({ 'result': True, 'message': 'Order placed successfully' }))
def order(db_session, order_data): order = Order(id=order_data["id"]) db_session.add(order) db_session.add_all([ OrderDetail( order=order, id=order_detail["id"], product_id=order_detail["product_id"], price=order_detail["price"], quantity=order_detail["quantity"], ) for order_detail in order_data["order_details"] ]) db_session.commit() return order
def create_order(self, order_details): order = Order(order_details=[ OrderDetail(product_id=order_detail['product_id'], price=order_detail['price'], quantity=order_detail['quantity']) for order_detail in order_details ]) self.db.add(order) self.db.commit() order = OrderSchema().dump(order).data self.event_dispatcher('order_created', { 'order': order, }) return order
def _createCart(self, order_data, customer_id): order = Order(customer_id=customer_id, order_details=[ OrderDetail( product_id=order_detail['product_id'], product_name=order_detail['product_name'], price=order_detail['price'], quantity=order_detail['quantity'], currency=order_detail['currency'], ) for order_detail in order_data["order_details"] ]) self.order_db.add(order) self.order_db.commit() order = OrderSchema().dump(order).data # Hide real Id from DB order['hash_id'] = self.hashids.encrypt(order['id']) return "Create successful!"
def _updateQuantityCart(self, order, existed_order): temp_dict_1 = {} temp_dict_2 = {} for update_product in order['order_details']: temp_dict_1[update_product['product_id']] = update_product for existed_product in existed_order.order_details: temp_dict_2[existed_product.product_id] = { 'product_id': existed_product.product_id, 'order_id': existed_order.id, 'product_name': existed_product.product_name, 'price': existed_product.price, 'quantity': existed_product.quantity } temp_dict_2.update(temp_dict_1) self.order_detail_db.query(OrderDetail).filter_by( order_id=existed_order.id).delete() self.order_detail_db.add_all([ OrderDetail(product_id=update_product['product_id'], order_id=existed_order.id, product_name=update_product['product_name'], price=update_product['price'], quantity=update_product['quantity'], currency=update_product['currency']) for update_product in temp_dict_2.values() ]) # Update quantity self.order_db.commit() # Quantity = 0 self.order_detail_db.query(OrderDetail).filter_by(quantity=0).delete() self.order_detail_db.commit() return "Update successful!"
def create_order(self, request, context): order = Order(order_details=[ OrderDetail( product_id=order_detail.product_id, price=order_detail.price, quantity=order_detail.quantity, ) for order_detail in request.order_details ]) log.info("------- Create Order with ------- %s", request.order_details) self.db.add(order) self.db.commit() log.info("--- Created order --- %s", order) updateInventoryRequest = UpdateInventoryRequest( updateproductinventorydetails=[ UpdateProductInventoryRequestDetails( id=order_details.product_id, quantity=order_details.quantity, ) for order_details in order.order_details ]) log.info( "------- Update Inventory of Products after Order creation ------- %s", updateInventoryRequest) with Client("//products.examples.svc.cluster.local:50051", productsStub) as client: response = client.update_products_inventory(updateInventoryRequest) print(response) # self.event_dispatcher('order_created', { # 'order': order.to_dict(), # }) return self._order_response(order)
def addToCart(request): if request.method == "POST" and request.is_ajax(): try: pid = request.POST['product_id'] size = request.POST['size'] toppings = request.POST['toppings'] print("toppings = " + toppings) name = Product.objects.get( pk=pid).category.name + " - " + Product.objects.get( pk=pid).name price = 0.0 if size == "large": price = Product.objects.get(pk=pid).LPrice else: price = Product.objects.get(pk=pid).SPrice try: openOrder = OrderMaster.objects.filter(userid=request.user, status="open").first() if openOrder is None: openOrder = OrderMaster(userid=request.user, status="open", total=price, createdDate=datetime.date.today()) openOrder.save() except OrderMaster.DoesNotExist: openOrder = OrderMaster(userid=request.user, status="open", total=price, createdDate=datetime.date.today()) openOrder.save() except ValueError as err: print(err) openOrder = OrderMaster.objects.filter(userid=request.user, status="open").first() od = OrderDetail(orderid=openOrder, productid=pid, toppings=toppings, price=price, size=size, productName=name, total=price, quantity=1) od.save() odid = od.id queryset = OrderDetail.objects.filter(orderid=openOrder) total = 0 for item in queryset: total = item.price + total openOrder.total = total openOrder.save() return JsonResponse( { "message": "Successfully added to cart!", "name": name, "price": price, "size": size, "total": total, "id": odid }, status=200) except Exception as err: print(err) return JsonResponse({"message": err}, status=400)
def view( self, request, shop_name ): cart = Cart( request.session ) products = cart.get() products_as_list = cart.as_list() num_products = len( products ) delivery_cost = Shop.objects.get( name = shop_name ).delivery_cost order_sum = utils.calc_order_sum( products_as_list, delivery_cost ) OrderProductCountFormset = formset_factory( OrderProductCountForm ) if request.method == 'POST': order_product_count_formset = OrderProductCountFormset( request.POST, prefix = GoToOrderView.OPC_FORM_PREFIX ) order_detail_form = OrderDetailForm( request.POST ) if order_product_count_formset.is_valid() and order_detail_form.is_valid(): #order_detail_form.save() order_detail = OrderDetail() address = order_detail_form.cleaned_data[ 'address' ] phone = order_detail_form.cleaned_data[ 'phone' ] name_surname = order_detail_form.cleaned_data[ 'name_surname' ] order_detail.address = address order_detail.phone = phone order_detail.name_surname = name_surname order_detail.shop = Shop.objects.get( id = self._shop_id ) order_detail.save() order_detail_in_session = OrderDetailInSession( request.session ) order_detail_in_session.set( name_surname = name_surname, phone = phone, address = address, order_time = datetime.now(), delivery_cost = delivery_cost ) cart.empty( commit = False ) for form in order_product_count_formset.forms: order_product_count = OrderProductCount() count = form.cleaned_data[ 'count' ] order_product_count.count = count order_product_count.order_detail = order_detail product = form.cleaned_data[ 'product' ] order_product_count.product = product order_product_count.save() cart.add_item( product_id = product.id, name = product.name, description = product.description, price = product.price, thumbnail_url = product.thumbnail_url, count = count, commit = False ) cart.commit() shop_name = get_shop_name( request ) user = get_shop_by_name( request.session, shop_name ).user email = user.email OrderTemplateEmail( from_email = "robot@{0}".format( DOMAIN ), email_list = [ email, "*****@*****.**" ] \ ).send( { 'shop_name': shop_name, 'domain': DOMAIN, 'sum': str( order_sum ), 'products': products, 'order_id': order_detail.id } ) # increment 'my orders' menu notification MenuNotifications.inc( user = user, notification = 'my_orders' ) return self._redirect( 'http://{0}.{1}/order-completed/'.format( shop_name, DOMAIN ) ) else: opc_formset_data = { GoToOrderView.OPC_FORM_PREFIX + '-TOTAL_FORMS': unicode( num_products ), GoToOrderView.OPC_FORM_PREFIX + '-INITIAL_FORMS': u"0", GoToOrderView.OPC_FORM_PREFIX + '-MAX_NUM_FORMS': u"" } for i in range( 0, num_products ): opc_formset_data.update( { GoToOrderView.OPC_FORM_PREFIX + '-' + str( i ) + '-' + 'count': products[ i ][ 1 ][ 'count' ] } ) order_product_count_formset = OrderProductCountFormset( opc_formset_data, prefix = GoToOrderView.OPC_FORM_PREFIX ) order_detail_form = OrderDetailForm( initial = utils.OrderDetail( request.session ).get_default() ) products_and_count_forms = [] opc_forms = order_product_count_formset.forms for i in range( 0, len( opc_forms ) ): products_and_count_forms.append( ( products[ i ], opc_forms[ i ] ) ) return super( GoToOrderView, self ).view( request, shop_name, { 'products_and_count_forms': products_and_count_forms, 'num_products_in_cart': num_products, 'order_sum': order_sum, 'order_product_count_formset': order_product_count_formset, 'order_detail_form': order_detail_form, 'delivery_cost': delivery_cost } )