Пример #1
0
def addcart(request):
    goodsid = request.GET.get('goodsid')
    num = request.GET.get('num')
    # print(goodsid,num)
    token = request.COOKIES.get('token')
    responseData = {
        'msg': '添加购物车成功',
        'status': 1  # 1标识添加成功,0标识添加失败,-1标识未登陆
    }
    if token:  # 登陆[直接操作模型]
        # 获取用户
        user = User.objects.get(token=token)
        # 获取商品
        goods = Goods.objects.get(pk=goodsid)
        # 商品已经在购物车,只改商品个数
        # 商品不存在购物车,新建对象(加入一条新的数据)
        carts = Cart.objects.filter(user=user).filter(goods=goods)
        if carts.exists():  # 修改数量
            cart = carts.first()
            cart.number = int(cart.number) + int(num)
            cart.save()
            responseData['number'] = cart.number
        else:  # 添加一条新记录
            cart = Cart()
            cart.user = user
            cart.goods = goods
            cart.number = num
            cart.save()
            responseData['number'] = cart.number
        return JsonResponse(responseData)
    else:  # 未登录[跳转到登陆页面]
        responseData['msg'] = '未登录,请登录后操作'
        responseData['status'] = -1
        return JsonResponse(responseData)
Пример #2
0
def sub_to_cart(request):
    goodsid = request.GET.get('goodsid')
    # user_id = request.GET.get('user_id')
    # if user_id:
    #     return HttpResponse("Add Success")
    # else:
    #
    #     data = {
    #         'status': 302,
    #         'msg': 'not login'
    #     }
    #
    # return JsonResponse(data)
    # print(Cart.c_user)
    # print(Cart.c_goods_id)
    # print(request.user)
    # print(goodsid)
    carts=Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid)

    if carts.exists():
        cart_obj = carts.first()
        cart_obj.c_goods_num=cart_obj.c_goods_num-1
    else:
        cart_obj = Cart()
        cart_obj.c_goods_id = goodsid
        cart_obj.c_user = request.user
    cart_obj.save()

    data = {
            'status': 200,
            'msg': 'sub success',
            'c_goods_num': cart_obj.c_goods_num
        }
    return JsonResponse(data=data)
Пример #3
0
def cart(request, id):
    a = Product.objects.get(id=id)
    if request.method == "POST":
        c = Cart(user_id=request.user.id, product_id=a.id)
        c.save()
        return redirect("/crtcnt")
    return render(request, 'html/cart.html', {'crt': a})
Пример #4
0
def add_to_cart(request):
    user_id = request.session.get('user_id')
    user = get_user_by_id(user_id)
    data = {}
    if not user:
        # 重定向
        data['status'] = '302'
        data['msg'] = 'not login'
    else:
        goods_id = request.GET.get("goodsid")
        carts = Cart.objects.filter(c_user=user).filter(c_goods_id=goods_id)

        if carts:
            # 商品已存在时(数量不为0)
            cart_obj = carts[0]
            cart_obj.c_goods_num = cart_obj.c_goods_num + 1
            cart_obj.save()
        else:
            # 商品不存在
            cart_obj = Cart()
            cart_obj.c_goods_id = goods_id
            cart_obj.c_user_id = user_id
            cart_obj.save()

        data['msg'] = 'add success'
        data['status'] = '200'
        data['cart_goods_num'] = cart_obj.c_goods_num
    return JsonResponse(data)
Пример #5
0
def add_to_cart(request):
    # 获取前端传来的商品id
    goodsid = request.GET.get('goodsid')

    # 去购物车表里查看请求的用户 在筛选出当前添加的goodsid
    carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid)
    # print(carts)
    # print(type(carts))
    # print(carts.values())
    # 判断上一步筛选出来是否有数据  如果有取出来(上一步的筛选只能筛出一条数据) 并且将数量加1
    if carts.exists():
        cart_obj = carts.first()
        cart_obj.c_goods_num = cart_obj.c_goods_num + 1
    # 如果筛选出来没有数据 则把这条数据添加到购物车表  因为模型中c_goods_num默认是1 c_is_select默认是选中 所以这里存的时候不写也可以
    else:
        cart_obj = Cart()
        cart_obj.c_goods_id = goodsid
        cart_obj.c_user = request.user

    cart_obj.save()

    # print(request.user)
    data = {
        'status': 200,
        'msg': 'add success',
        'c_goods_num': cart_obj.c_goods_num
    }
    return JsonResponse(data=data)
Пример #6
0
def add_to_cart(request):

    user_id = request.session.get('user_id')

    user = get_user_by_id(user_id)

    data = {}

    if not user:
        # 重定向
        data['status'] = "902"
        data['msg'] = "not login"
    else:
        goods_id = request.GET.get("goodsid")

        carts = Cart.objects.filter(c_user=user).filter(c_goods_id=goods_id)

        if carts.exists():
            cart_obj = carts.first()
            cart_obj.c_goods_num = cart_obj.c_goods_num + 1
            cart_obj.save()
        else:
            cart_obj = Cart()
            cart_obj.c_goods_id = goods_id
            cart_obj.c_user_id = user_id
            cart_obj.save()

        data['msg'] = 'add success'
        data['status'] = "201"
        data['cart_goods_num'] = cart_obj.c_goods_num

    return JsonResponse(data)
Пример #7
0
def addtocart(request):
    token = request.session.get('token')
    goodsid = request.GET.get('goodsid')

    response_data = {}
    if token:
        user = User.objects.get(token=token)
        goods = Goods.objects.get(pk=goodsid)

        carts = Cart.objects.filter(goods=goods).filter(user=user)
        if carts.exists():
            cart = carts.first()
            cart.number = cart.number + 1
            cart.save()
            response_data['msg'] = '添加购物车成功'
            response_data['status'] = 1
            response_data['number'] = cart.number
            return JsonResponse(response_data)
        else:
            cart = Cart()
            cart.user = user
            cart.goods = goods
            cart.number = 1
            cart.save()

            response_data['mag'] = '添加购物车成功'
            response_data['status'] = 1
            response_data['number'] = cart.number
            return JsonResponse(response_data)
    else:
        response_data['msg'] = '你还未登录,请登陆'
        response_data['status'] = '-1'
        return JsonResponse(response_data)
Пример #8
0
def shopping_cart(request):
    var_user = request.session.get('user')
    if var_user == None:
        return render(request, 'app/varification_login.html')

    commodity = Index.objects.all()
    title = request.GET.get('tit')
    for value in commodity:
        value_title = str(value.title)
        if title == value_title:
            cart_id = Cart.objects.filter(title=value.title)
            if not len(cart_id) == 0:  # 判断列表不为空
                cart = Cart.objects.get(title=value.title)
                cart.digital += 1
                cart.save()
            else:
                add_cart = Cart(image=value.image,
                                title=value.title,
                                price=value.price,
                                total_price=value.price)
                add_cart.save()
    cart = Cart.objects.all()
    if len(cart) == 0:
        return render(request, 'app/shopping_null.html')
    return render(request, 'app/shopping_cart.html', {
        'commodity': cart,
        'titles': title
    })
Пример #9
0
def addcart(request):
    num = request.GET.get('number')
    goodsid = request.GET.get('goodsid')
    goodssize = request.GET.get('goodssize')
    email = request.COOKIES.get('email')
    print(num)
    print(goodssize)
    responseData = {
        'msg': '添加购物车成功',
        'status': 1  # 1标识添加成功,0标识添加失败,-1标识未登录
    }
    if email:
        # 如果用户存在
        user = User.objects.get(email=email)
        goods = Hanfengshishang.objects.get(pk=goodsid)

        carts = Cart.objects.filter(user=user).filter(goods=goods)
        # 如果购物车有商品,原有的基础上加
        if carts.exists():
            cart = carts.first()
            cart.number = cart.number + int(num)
            cart.size = goodssize
            cart.save()
            responseData['number'] = cart.number
        else:
            # 如果没有,商品数量等于添加的数量
            cart = Cart()
            cart.user = user
            cart.goods = goods
            cart.number = num
            cart.size = goodssize
            cart.save()
            responseData['number'] = cart.number
        return JsonResponse(responseData)
Пример #10
0
def addGoods(request):

    data = {'status': '200', 'goods_num': '1'}
    username = request.session.get('username')
    goods_id = request.GET.get('goods_id')
    if not username:
        data['status'] = '901'
        return JsonResponse(data)

    users = User.objects.filter(u_username=username)
    goods_s = Axf_goods.objects.filter(pk=goods_id)

    if goods_s.exists():
        if users.exists():
            user = users.first()
            goods = goods_s.first()
            carts = Cart.objects.filter(cart_belong=False).filter(
                Q(cart_user=user) & Q(cart_goods=goods))
            if carts.exists():
                cart_my = carts.first()
                cart_my.cart_num += 1
                cart_my.save()
            else:
                cart_my = Cart()
                cart_my.cart_user = user
                cart_my.cart_goods = goods
                cart_my.save()
            data['status'] = '200'
            data['goods_num'] = str(cart_my.cart_num)
            return JsonResponse(data)
    data['status'] = '900'
    return JsonResponse(data)
Пример #11
0
def addcart(request, id):
    a = Product.objects.get(id=id)
    if request.method == "POST":
        c = Cart(user_id=request.user.id, product_id=a.id)
        c.save()
        return redirect('/store')
    return render(request, 'html/addcart.html')
Пример #12
0
def addcart(request):
    goodsid = request.GET.get('goodsid')
    token = request.session.get('token')

    responseData = {'msg': '登录购物车成功', 'status': 1}

    if token:
        user = User.objects.get(token=token)
        goods = Goods.objects.get(pk=goodsid)
        carts = Cart.objects.filter(user=user).filter(goods=goods)
        if carts.exists():
            cart = carts.first()
            cart.number = cart.number + 1
            cart.save()
            responseData['number'] = cart.number
        else:
            cart = Cart()
            cart.user = user
            cart.goods = goods
            cart.number = 1
            cart.save()

            responseData['number'] = cart.number

        return JsonResponse(responseData)
    else:
        responseData['msg'] = '未登录,清东路后操作'
        responseData['status'] = -1
        return JsonResponse(responseData)
Пример #13
0
def add_to_cart(request):
    goodsid = request.GET.get('goodsid')

    # 获取购物车里的数据
    carts = Cart.objects.filter(C_user=request.user).filter(C_goods_id=goodsid)
    print(request.user)
    # 有数据+1
    if carts.exists():
        c_obj = carts.first()
        c_obj.C_goods_num = c_obj.C_goods_num + 1

    # 没有数据创建新的
    else:
        c_obj = Cart()
        c_obj.C_goods_id = goodsid
        c_obj.C_user = request.user

    c_obj.save()

    data = {
        'msg': 1,
        'status': 200,
        "C_goods_num": c_obj.C_goods_num,
    }

    return JsonResponse(data=data)
Пример #14
0
def addcart(request):

    token = request.session.get('token')
    bookid = request.GET.get('bookid')
    number = int(request.GET.get('number'))  #ajax傳遞數據

    data = {}

    if token:
        user = User.objects.get(token=token)
        book = Book.objects.get(pk=bookid)
        carts = Cart.objects.filter(user=user).filter(book=book)

        # 進行邏輯判斷
        if carts.exists():
            cart = carts.first()
            cart.number = cart.number + number
            cart.price = cart.number * float(cart.book.newprice)
            cart.save()
        else:
            cart = Cart()
            cart.user = user
            cart.book = book
            cart.number = number
            cart.price = cart.number * float(cart.book.newprice)
            cart.save()

        data['status'] = 1
        return JsonResponse(data)
    else:
        data['status'] = -1
        data['msg'] = "請登錄後操作"

        return JsonResponse(data)
Пример #15
0
def add_to_cart(request):
    goodsid = request.GET.get('goodsid')
    user_id = request.user.id
    carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid)

    if carts.exists():
        cart_obj = carts.first()
        cart_obj.c_goods_num = cart_obj.c_goods_num + 1
    else:
        cart_obj = Cart()
        cart_obj.c_goods_id = goodsid
        cart_obj.c_user = request.user

    cart_obj.save()

    print(request.user)

    data = {
        'status': 200,
        'mgs': 'add success',
        'c_goods_num': cart_obj.c_goods_num,
        'total_price': get_total_price(user_id),
    }

    return JsonResponse(data=data)
Пример #16
0
def add_to_cart(request):
    goodsid = request.GET.get('goodsid')
    carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid)
    if carts.exists():
        cart_obj = carts.first()
        cart_obj.c_goods_num = cart_obj.c_goods_num + 1
    else:
        cart_obj = Cart()
        cart_obj.c_user = request.user
        cart_obj.c_goods_id = goodsid

    cart_obj.save()

    data = {
        "status": 200,
        "msg": "add success",
        "c_goods_num": cart_obj.c_goods_num
    }
    return JsonResponse(data=data)
Пример #17
0
def add_to_cart(request):
    goodsid = request.GET.get('goodsid')
    user = request.user
    carts = Cart.objects.filter(c_good__productid=goodsid).filter(c_user=user)
    data = {
        'status': 700,
        'num': 1,
    }
    if carts.exists():
        cart = carts.first()
        cart.c_goods_num = cart.c_goods_num + 1
        cart.save()
        data['num'] = cart.c_goods_num
    else:
        cart = Cart()
        cart.c_good = Goods.objects.get(productid=goodsid)
        cart.c_user = user
        cart.save()

    return JsonResponse(data)
Пример #18
0
def add_to_cart(request):
    goodsid = request.GET.get('goodsid')
    user_id = request.session.get("user_id")
    carts = Cart.objects.filter(c_user_id=user_id).filter(c_goods_id=goodsid)
    if carts.exists():
        cart_obj = carts.first()
        cart_obj.c_goods_num = cart_obj.c_goods_num + 1
    else:
        cart_obj = Cart()
        cart_obj.c_user_id = user_id
        cart_obj.c_goods_id = goodsid

    cart_obj.save()

    data = {
        'status': 200,
        'msg': 'add succcess',
        'c_goods_num': cart_obj.c_goods_num,
    }
    return JsonResponse(data)
Пример #19
0
def sub_to_cart(request):
    goodsid = request.GET.get('goodsid')

    carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid)

    if carts.exists():
        cart_obj = carts.first()
        if cart_obj.c_goods_num > 0:
            cart_obj.c_goods_num = cart_obj.c_goods_num - 1
    else:
        cart_obj = Cart()
        cart_obj.c_goods_id = goodsid
        cart_obj.c_user = request.user
    cart_obj.save()
    data = {
        'status': 200,
        'msg': 'sub_success',
        'c_goods_num': cart_obj.c_goods_num
    }

    return JsonResponse(data=data)
Пример #20
0
def add_to_cart(request):
    goodsid = request.GET.get('goodsid')

    carts = Cart.objects.filter(Q(c_user=request.user) & Q(c_goods_id=goodsid))

    if carts.exists():
        cart_obj = carts.first()
        cart_obj.c_goods_num = cart_obj.c_goods_num + 1
        cart_obj.save()

    else:
        cart_obj = Cart(c_goods_id=goodsid, c_user=request.user)
        cart_obj.save()

    data = {
        'status': 200,
        'msg': 'add success',
        'c_goods_num': cart_obj.c_goods_num,
    }

    return JsonResponse(data)
Пример #21
0
 def create(self, request, *args, **kwargs):
     goodid = request.POST.get('c_goods_id')
     carts = Cart.objects.filter(c_user_id=request.user.id).filter(
         c_goods_id=goodid)
     action = request.GET.get('action')
     if carts.exists():
         cart_obj = carts.first()
         data = {
             'status': 200,
             'msg': 'success',
             'goods_num': cart_obj.c_goods_num
         }
         if action == 'add':
             cart_obj.c_goods_num = cart_obj.c_goods_num + 1
         elif action == 'sub' and cart_obj.c_goods_num > 0:
             cart_obj.c_goods_num = cart_obj.c_goods_num - 1
         elif cart_obj.c_goods_num == 0:
             data['goods_num'] = 0
         else:
             raise exceptions.ParseError
         data['goods_num'] = cart_obj.c_goods_num
         cart_obj.save()
     else:
         data = {
             'status': 200,
             'msg': 'success',
         }
         if action == 'sub':
             data['msg'] = '购物车无该商品'
         elif action == 'add':
             cart_obj = Cart()
             cart_obj.c_user_id = request.user.id
             cart_obj.c_goods_id = goodid
             data['goods_num'] = 1
             cart_obj.save()
         else:
             raise exceptions.ParseError
     return JsonResponse(data=data)
Пример #22
0
def addcart(request):
    goodsid = request.GET.get('goodsid')
    # print(goodsid)
    tel = request.COOKIES.get('tel')

    responseData={
        'msg':'添加购物车成功',
        'status': 1
    }

    if tel:
        # 获取用户信息
        user = User.objects.get(tel=tel)
        # 获取商品信息
        goods = Goodsdetail.objects.get(goodsid=goodsid)

        # 商品已经在购物车,只修改商品个数
        # 商品不存在购物车,新建对象(加入一条新的数据)
        carts = Cart.objects.filter(user=user).filter(goods=goods)
        print(carts)
        if carts.exists(): # 修改数量
            cart = carts.first()
            cart.number = cart.number + 1
            cart.save()
            responseData['number'] = cart.number
        else: # 添加一条新购物车记录
            cart=Cart()
            cart.user= user
            cart.goods = goods
            cart.number = 1
            cart.save()

            responseData['number'] = cart.number
        return JsonResponse(responseData)
    else: # 未登录
        responseData['msg'] = '未登录,请登录后操作'
        responseData['status'] = -1
        return JsonResponse(responseData)
Пример #23
0
def addtocart(request):
    # from market.js
    goodsid = request.GET.get('goodsid')

    # AOP middleware
    # request.user saved in 'LoginMiddleware'
    carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid)
    if carts.exists():
        cart_obj = carts.first()
        cart_obj.c_goods_num = cart_obj.c_goods_num + 1
    else:
        cart_obj = Cart()
        cart_obj.c_user = request.user
        cart_obj.c_goods_id = goodsid

    cart_obj.save()

    data = {
        'status': 200,
        'msg': 'updated cart',
        'c_goods_num': cart_obj.c_goods_num,
    }
    return JsonResponse(data=data)
Пример #24
0
def add_to_cart(request):
    goodsid=request.GET.get('goodsid')
    user=request.user
    carts=Cart.objects.filter(c_goods_id=goodsid).filter(c_user=request.user)

    if carts:
        cart=carts.first()
        cart.c_goods_num=cart.c_goods_num+1
    else:
        cart=Cart()
        cart.c_user=request.user
        # good=Goods.objects.get(goodsid=goodsid)
        # cart.c_goods=good
        cart.c_goods_id=goodsid
        cart.c_goods_num=1
    cart.save()
    data={
        'status':200,
        'msg':'添加成功',
        'c_good_num':cart.c_goods_num,
        'total_price': get_total_price(request.user),
    }
    return JsonResponse(data)
Пример #25
0
def add_to_cart(request):
    user_id = request.session.get('user_id')

    user = get_user_by_id(user_id)

    # ##
    cartid = request.GET.get("cartid")

    data = {}

    if not user:
        # 重定向
        data['status'] = "902"
        data['msg'] = "not login"
    else:
        # goods_id = request.GET.get("goodsid")
        goods_id = request.GET.get("goodsid") or Cart.objects.get(
            pk=cartid).c_goods_id

        carts = Cart.objects.filter(c_user=user).filter(c_goods_id=goods_id)

        if carts.exists():
            cart_obj = carts.first()
            cart_obj.c_goods_num = cart_obj.c_goods_num + 1
            cart_obj.save()
        else:
            cart_obj = Cart()
            cart_obj.c_goods_id = goods_id
            cart_obj.c_user_id = user_id
            cart_obj.save()

        data['msg'] = 'add success'
        data['status'] = "200"
        data['c_goods_num'] = cart_obj.c_goods_num
        data["total_price"] = get_total_price(request.session.get("user_id"))

    return JsonResponse(data)
Пример #26
0
def addcart(request):
    goodsid = request.GET.get('goodsid')
    # print(goodsid)
    goodsnum = request.GET.get('goodsnum')
    goodsnum1 = int(goodsnum)
    print(type(goodsnum1))
    token = request.session.get('token')

    responseDate = {
        'msg': '添加商品到购物车成功',
        'status': 1,
    }
    if token:
        user = User.objects.get(token=token)
        goods = Goods.objects.get(pk=goodsid)

        carts = Cart.objects.filter(user=user).filter(goods=goods)
        if carts.exists():
            cart = carts.first()
            cart.number = cart.number + goodsnum1
            cart.save()
            responseDate['number'] = cart.number

        else:
            cart = Cart()
            cart.user = user
            cart.goods = goods
            cart.number = 1
            cart.save()
            responseDate['number'] = cart.number

        return JsonResponse(responseDate)
    else:
        responseDate['msg'] = '未登录 请登陆后操作'
        responseDate['status'] = -1
        return JsonResponse(responseDate)
Пример #27
0
def add_to_cart(request):
    user = request.user
    good_id = request.POST.get('g_id')
    good = Goods.objects.get(id=good_id)

    opreate_type = request.POST.get('opreate_type')

    carts = Cart.objects.filter(c_user=user).filter(c_goods=good)

    if carts.exists():
        cart = carts.first()
        if opreate_type == 'add':
            cart.c_goods_nums = cart.c_goods_nums + 1

        elif opreate_type == 'sub':
            cart.c_goods_nums = cart.c_goods_nums - 1
    else:
        cart = Cart()
        cart.c_user = user
        cart.c_goods = good
    cart.save()

    data = {'status': 200, 'msg': 'ok', 'num': cart.c_goods_nums}
    return JsonResponse(data=data)
Пример #28
0
 def get(self, request):
     if request.method == 'GET':
         token = request.GET.get('token')
         try:
             u_id = cache.get(token)
             user = User.objects.get(pk=u_id)
             request.user = user
         except Exception as e:
             print("错误提示:", e)
             data = {'status': 901}
             return JsonResponse(data=data)
         foodid = request.GET.get("foodid")
         is_add = int(request.GET.get("is_add"))
         carts = Cart.objects.filter(c_user_id=request.user).filter(
             c_foods_id=foodid)
         # 加
         if is_add == 1:
             if carts.exists():
                 c_obj = carts.first()
                 c_obj.c_foods_num = c_obj.c_foods_num + 1
             else:
                 c_obj = Cart()
                 c_obj.c_foods_id = foodid
                 c_obj.c_user_id = u_id
             c_obj.save()
             data = {
                 'status': 200,
                 "c_foods_num": c_obj.c_foods_num,
             }
             return JsonResponse(data=data)
         # 减
         elif is_add == 0:
             if carts.exists():
                 c_obj = carts.first()
                 if c_obj.c_foods_num >= 1:
                     c_obj.c_foods_num = c_obj.c_foods_num - 1
                     c_obj.save()
                 data = {
                     'status': 200,
                     "c_foods_num": c_obj.c_foods_num,
                 }
                 if c_obj.c_foods_num == 0:
                     Cart.objects.filter(pk=c_obj.id).delete()
                 return JsonResponse(data=data)
             data = {
                 'status': 200,
                 "c_foods_num": 0,
             }
             return JsonResponse(data=data)
         else:
             if carts.exists():
                 c_obj = carts.first()
                 data = {
                     'status': 200,
                     "c_foods_num": c_obj.c_foods_num,
                 }
             else:
                 data = {
                     'status': 200,
                     "c_foods_num": 0,
                 }
             return JsonResponse(data=data)
Пример #29
0
    def get(self, request, *args, **kwargs):

        action = request.query_params.get("action")

        data = {}

        if action == "add_to_cart":
            goodsid = request.query_params.get("goodsid")
            carts = Cart.objects.filter(c_user=request.user).filter(
                c_goods_id=goodsid)

            if carts.exists():
                cart_obj = carts.first()
                cart_obj.c_goods_num = cart_obj.c_goods_num + 1
            else:
                cart_obj = Cart()
                cart_obj.c_goods_id = goodsid
                cart_obj.c_user = request.user

            cart_obj.save()

            data = {
                'status': 200,
                'msg': 'add success',
                'c_goods_num': cart_obj.c_goods_num
            }

        elif action == "change_cart_status":
            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()

            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(self.request)
            }
            return Response(data)
        elif action == "add_shopping":
            cartid = request.GET.get("cartid")

            cart_obj = Cart.objects.get(pk=cartid)

            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['total_price'] = get_total_price(self.request)
            return Response(data)
        elif action == "sub_shopping":
            cartid = request.GET.get("cartid")

            cart_obj = Cart.objects.get(pk=cartid)

            data = {
                'status': 200,
                'msg': 'ok',
            }

            if cart_obj.c_goods_num > 1:
                cart_obj.c_goods_num = cart_obj.c_goods_num - 1
                cart_obj.save()
                data['c_goods_num'] = cart_obj.c_goods_num
            else:
                cart_obj.delete()
                data['c_goods_num'] = 0

            data['total_price'] = get_total_price(self.request)
            return Response(data)
        elif action == "all_select":
            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()

            data = {
                'status': 200,
                'msg': 'ok',
                'total_price': get_total_price(self.request)
            }
        elif action == "all":
            carts = Cart.objects.filter(c_user=request.user)

            is_all_select = not carts.filter(c_is_select=False).exists()

            data = {
                'status': 200,
                'msg': 'ok',
                "data": {
                    'title': '购物车',
                    'is_all_select': is_all_select,
                    'total_price': get_total_price(self.request),
                    'carts': CartSerializer(carts, many=True).data,
                }
            }
            return Response(data)
        else:
            data["status"] = status.HTTP_400_BAD_REQUEST
            data["msg"] = "错误的参数"

        return Response(data)
Пример #30
0
def addcart(request):
    shopid = request.GET.get('shopid')

    token = request.session.get('token')

    responseData = {
        'msg': '添加购物车成功',
        'status': 1  # 1标识添加成功,0标识添加失败,-1标识未登录
    }

    if token:  # 登录 [直接操作模型]

        # 获取用户

        user = User.objects.get(token=token)

        # 获取商品

        shop = Shop.objects.get(id=shopid)

        # 商品已经在购物车,只修改商品个数

        # 商品不存在购物车,新建对象(加入一条新的数据)

        carts = Cart.objects.filter(user=user).filter(shop=shop)

        if carts.exists():  # 修改数量

            cart = carts.first()
            number = request.GET.get('number')
            print(type(number))

            cart.isselect = 0

            cart.number = str(int(number) + 1)

            cart.save()

            responseData['number'] = cart.number

        else:  # 添加一条新记录

            cart = Cart()

            cart.user = user

            cart.shop = shop

            cart.number = 1

            cart.isselect = 0

            cart.save()

            responseData['number'] = cart.number

        return JsonResponse(responseData)

    else:  # 未登录 [跳转到登录页面]

        # 由于addcart这个是 用于 ajax操作, 所以这里是不能进行重定向!!

        responseData['msg'] = '未登录,请登录后操作'

        responseData['status'] = -1

        return JsonResponse(responseData)