예제 #1
0
def cartitem_api(req):
    user = req.user
    params = req.POST
    c_id = int(params.get('c_id'))
    op_type = params.get('op_type')
    # 先拿cid对应的数据
    cart_data = Cart.objects.get(pk=c_id)
    sum_price, item_num = 0, 0
    is_select_all = True
    if op_type == 'sub':
        #减商品数量
        cart_data.num -= 1
        if cart_data.num == 0:
            cart_data.delete()
            if Cart.objects.filter(user=user, is_selected=False).exists():
                is_select_all = False
            else:
                is_select_all = True
        else:
            cart_data.save()
            #     需要拿到所有该用户的选中商品的数据集
            item_num = cart_data.num
        cart_items = Cart.objects.filter(user=user, is_selected=True)
        sum_price = get_cart_money(cart_items)
        data = {
            'code': SUCCESS,
            'msg': 'ok',
            'data': {
                'sum_price': sum_price,
                'item_num': item_num,
                'is_select_all': is_select_all
            }
        }
        return JsonResponse(data)
    # 算钱 和 记录商品数量
    else:
        # 查库存
        if cart_data.item.storenums < 1:
            data = {"code": INVENTORY, "msg": "库存不足", "data": None}
            return JsonResponse(data)
        # 加操作
        cart_data.num += 1
        cart_data.save()
        cart_items = Cart.objects.filter(user=user, is_selected=True)
        sum_price = get_cart_money(cart_items)
        # 保存 返回数据
        data = {
            "code": SUCCESS,
            "msg": "ok",
            "data": {
                "sum_price": sum_price,
                "item_num": cart_data.num,
            }
        }
        return JsonResponse(data)
예제 #2
0
def select_all_api(req):
    user = req.user
    # 查询到所有的用户对应的购物车数据
    cart_items = Cart.objects.filter(user=user)
    is_all_select, sum_price = True, 0
    # 判断是否存在 未选中的商品
    if cart_items.filter(is_selected=False).exists():
        # 全选操作
        cart_items.filter(is_selected=False).update(is_selected=True)
        # is_all_select = True
        sum_price = get_cart_money(cart_items)
    else:
        cart_items.update(is_selected=False)
        is_all_select = False
        # sum_money = 0
    #返回结果
    data = {
        "code": SUCCESS,
        "msg": 'ok',
        'data': {
            "is_select_all": is_all_select,
            "sum_price": sum_price
        }
    }
    return JsonResponse(data)
예제 #3
0
def cart(req):
    user = req.user
    cart_items = Cart.objects.filter(user_id=user.id)
    is_selected_all = True
    if Cart.objects.filter(user=user, is_selected=False).exists():
        is_selected_all = False
    data = {
        'title': '购物车',
        'uname': user.username,
        'phone': user.phone,
        'address': user.address,
        'cart_items': cart_items,
        'is_selected_all': is_selected_all,
        'sum_price': get_cart_money(cart_items)
    }
    return render(req, "cart/cart.html", data)
예제 #4
0
def order_api(req):
    user = req.user
    # 找购物车内被用户选中的数据
    cart_items = Cart.objects.filter(user=user, is_selected=True)
    if not cart_items.exists():
        return JsonResponse({'code': 4, 'msg': '无商品可下单'})
    #创建订单
    order = Order.objects.create(user=user)
    #创建订单详情
    for i in cart_items:
        OrderItem.objects.create(order=order, goods_id=i.item_id, num=i.num)
    # 算钱
    sum_price = get_cart_money(cart_items)
    cart_items.delete()
    return render(req, 'order/order.html', {
        'order': order,
        'sum_price': sum_price
    })
예제 #5
0
def cart_status_api(req):
    params = QueryDict(req.body)
    user = req.user
    cart_item = Cart.objects.get(pk=int(params.get('c_id')))
    cart_item.is_selected = not cart_item.is_selected
    cart_item.save()
    cart_items = Cart.objects.filter(user=user, is_selected=True)
    sum_price = get_cart_money(cart_items)
    is_select_all = True
    if Cart.objects.filter(user=user, is_selected=False).exists():
        is_select_all = False
    data = {
        'code': SUCCESS,
        'msg': 'ok',
        'sum_price': sum_price,
        'is_select_all': is_select_all,
        'current_item_status': cart_item.is_selected
    }
    return JsonResponse(data)