Ejemplo n.º 1
0
def cart(request):
    user = request.user
    carts = Cart.objects.filter(c_user=user)
    is_all_select = not carts.filter(c_is_selected=False).exists()
    context = {
        'title': '购物车',
        'carts': carts,
        'is_all_select': is_all_select,
        'total_price': total_price(user),
    }
    return render(request, 'main/cart.html', context=context)
Ejemplo n.º 2
0
def add_in_cart(request):
    cartid = request.GET.get('cartid')
    user = request.user
    cart = Cart.objects.get(pk=cartid)
    cart.c_goods_num = cart.c_goods_num + 1
    cart.save()
    data = {
        'status': 700,
        'num': cart.c_goods_num,
        'total_price': total_price(user),
    }
    return JsonResponse(data)
Ejemplo n.º 3
0
def delete_in_cart(request):
    cartid = request.GET.get('cartid')
    cart = Cart.objects.get(pk=cartid)
    user = request.user
    data = {
        'status': 700,
    }
    if cart.c_goods_num > 1:
        cart.c_goods_num = cart.c_goods_num - 1
        cart.save()
        data['num'] = cart.c_goods_num
    else:
        cart.delete()
        data['num'] = 0
    data['total_price'] = total_price(user)
    return JsonResponse(data)
Ejemplo n.º 4
0
def change_select_state(request):
    cartid = request.GET.get('cartid')
    carts = Cart.objects.filter(pk=cartid)
    user = request.user
    data = {
        'status': 700,
    }
    if carts.exists():
        cart = carts.first()
        cart.c_is_selected = not cart.c_is_selected
        cart.save()
        data['is_selected'] = cart.c_is_selected
        is_all_select = not Cart.objects.filter(c_user=user).filter(
            c_is_selected=False).exists()
        data['is_all_select'] = is_all_select
        data['total_price'] = total_price(user)
    return JsonResponse(data)
Ejemplo n.º 5
0
def make_order(request):
    user = request.user
    carts = Cart.objects.filter(c_user=user).filter(c_is_selected=True)
    # 生成订单
    order = Order()
    order.o_user = user
    order.o_price = total_price(user)
    order.save()
    # 订单和商品级联
    for cart in carts:
        ordergoods = OrderGoods()
        ordergoods.o_order = order
        ordergoods.o_goods = cart.c_good
        ordergoods.o_goods_num = cart.c_goods_num
        ordergoods.save()
        cart.delete()
    data = {
        'status': 700,
        'msg': 'ok',
        'order_id': order.id,
    }
    return JsonResponse(data)