Esempio n. 1
0
def cart(request):
    carts = Cart.objects.filter(c_user=request.user)
    is_all_select = not carts.filter(c_is_select=False).exists()
    is_select = carts.filter(c_is_select=True)
    all_num = 0
    for num in is_select:
        all_num = num.c_goods_num + all_num
    list_carts = {}
    for cart in carts:
        if cart.c_goods.g_project not in list_carts:
            list_carts[cart.c_goods.g_project] = []
            list_carts[cart.c_goods.g_project].append(cart)
        else:
            list_carts[cart.c_goods.g_project].append(cart)

    data = {
        'title': '购物车',
        'carts': list_carts,
        'is_all_select': is_all_select,
        'total_price': get_total_price(request.user.id),
        'MEDIA_KEY_PREFIX': MEDIA_KEY_PREFIX,
        'all_num': all_num,
        'message': '购物车'
    }

    # 查询用户名
    user_id = request.user.id
    user = Users.objects.get(pk=user_id)
    data['username'] = user.u_username
    data['is_login'] = True
    # 查询购物车数量
    data['cart_num'] = get_cart_num(user_id)

    return render(request, 'orders/cart.html', context=data)
Esempio n. 2
0
def add_subshopping(request):
    cart_id = request.GET.get('cartid')
    cart_obj = Cart.objects.get(pk=cart_id)
    data = {'status': 200, 'msg': 'ok'}
    cart_obj.c_goods_num = cart_obj.c_goods_num + 1
    cart_obj.save()
    data['c_goods_num'] = cart_obj.c_goods_num
    data['price_sum'] = cart_obj.c_goods_num * cart_obj.c_goods.g_price
    is_select = Cart.objects.filter(c_user=request.user).filter(
        c_is_select=True)
    all_num = 0
    for num in is_select:
        all_num = num.c_goods_num + all_num
    data['all_num'] = all_num
    data['total_price'] = get_total_price(request.user.id)
    return JsonResponse(data=data)
Esempio n. 3
0
def all_select(request):
    cart_list = request.GET.get('cart_list')
    cart_list = cart_list.split('#')
    carts = Cart.objects.filter(id__in=cart_list)
    for cart_obj in carts:
        cart_obj.c_is_select = not cart_obj.c_is_select
        cart_obj.save()

    is_select = Cart.objects.filter(c_user=request.user).filter(
        c_is_select=True)
    all_num = 0
    for num in is_select:
        all_num = num.c_goods_num + all_num

    data = {
        'status': 200,
        'msg': 'ok',
        'total_price': get_total_price(request.user.id),
        'all_num': all_num,
    }
    return JsonResponse(data=data)
Esempio n. 4
0
def ordering(request):
    user_id = request.user.id
    address = Address.objects.filter(
        a_user_id=user_id).order_by('-a_is_default')
    cart_obj = Cart.objects.filter(c_user_id=user_id).filter(c_is_select=True)

    list_carts = {}
    for cart in cart_obj:
        if cart.c_goods.g_project not in list_carts:
            list_carts[cart.c_goods.g_project] = []
            list_carts[cart.c_goods.g_project].append(cart)
        else:
            list_carts[cart.c_goods.g_project].append(cart)

    is_select = Cart.objects.filter(c_user=request.user).filter(
        c_is_select=True)
    all_num = 0
    for num in is_select:
        all_num = num.c_goods_num + all_num

    data = {
        'title': '订单结算页',
        'address': address,
        'total_price': get_total_price(user_id),
        'MEDIA_KEY_PREFIX': MEDIA_KEY_PREFIX,
        'carts': list_carts,
        'all_num': all_num
    }

    # 查询用户名
    user_id = request.user.id
    user = Users.objects.get(pk=user_id)
    data['username'] = user.u_username
    data['is_login'] = True

    return render(request, 'orders/ordering.html', context=data)
Esempio n. 5
0
def change_cart_state(request):
    cart_id = request.GET.get('cartid')
    cart_obj = Cart.objects.get(pk=cart_id)
    cart_obj.c_is_select = not cart_obj.c_is_select
    cart_obj.save()
    is_all_select = not Cart.objects.filter(c_user=request.user).filter(
        c_is_select=False).exists()

    is_select = Cart.objects.filter(c_user=request.user).filter(
        c_is_select=True)
    all_num = 0
    for num in is_select:
        all_num = num.c_goods_num + all_num

    data = {
        'status': 200,
        'msg': 'change ok',
        'c_is_select': cart_obj.c_is_select,
        'is_all_select': is_all_select,
        'total_price': get_total_price(request.user.id),
        'all_num': all_num,
    }

    return JsonResponse(data=data)