Пример #1
0
def checkout(request):

    order = Order()
    i = 0
    total_qty = 0
    total_price = 0.0

    while request.POST.get('productId_' + str(i), None) is not None:
        product_id = request.POST.get('productId_' + str(i))
        qty = int(request.POST.get('qty_' + str(i)))
        total_qty = total_qty + qty
        product = Product.objects.get(pk=product_id)
        total_price = total_price + product.price_sale
        product.qty -= qty
        product.save()
        # Handle database
        # 1- Decrease product count (qty) - read from database [[product = Product.objects.get(pk=product_id)]]
        # 2-  Add Entry (row) or (save) in order-item table and order (Create order )

        i += 1
    order.total_qty = total_qty
    order.total_price = total_price
    order.save()
    del request.session[CART_KEY]
    return home(request)
 def post(self, request):
     order = Order()
     total_price = 0.0
     total_qty = 0
     while request.data.get('product_id'):
         product = request.data.get('product_id')
         qty = request.data.get('qty')
         product_id = Product.objects.get(pk=product)
         total_price = total_price + product_id.sale_price
         total_qty = total_qty + qty
         product_id.qty -= qty
         product.save()
     order.total_qty = total_qty
     order.total_price = total_price
     order.save()
     return Response(status=status.HTTP_201_CREATED)