Ejemplo n.º 1
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)
Ejemplo n.º 2
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)