Пример #1
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)
Пример #2
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)
Пример #3
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)
Пример #4
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)
Пример #5
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)
Пример #6
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)