Example #1
0
def addtocart(request):
    # 判断用户是否登录
    username = request.session.get('username')
    # 用户未登录
    if username == None:
        # 跳转到登录页面
        return redirect(reverse('axf:login'))
    # 用户已登录,数据添加到登录车
    goods_id = request.GET.get('goodsid')
    # 通过ID将商品添加到商品的的表里
    goods = Goods.objects.get(pk=goods_id)
    # 获取用户登录信息
    user = User.objects.get(u_name=username)

    # 购物车数量增加
    c = Cart.objects.filter(c_user=user).filter(c_goods=goods).filter(
        c_belong=False)
    if len(c) == 0:
        c = Cart()
    else:
        c = c.first()
        num = c.c_goods_num
        c.c_goods_num = num + 1
    # 存储购物信息
    c.c_user = user
    c.c_goods = goods
    c.save()
    return JsonResponse({'msg': '添加成功'})
Example #2
0
def changecart(request, flag):
    num = 1
    if flag == "1":
        num = -1

    # 判断是否登录
    tokenValue = request.COOKIES.get("token")
    if not tokenValue:
        print("nonononono")
        # 说明没登录
        return JsonResponse({"error": 1})
    try:
        user = User.objects.get(tokenValue=tokenValue)
    except User.DoesNotExist as e:
        return JsonResponse({"error": 2})

    gid = request.POST.get("gid")
    cid = request.POST.get("cid")
    pid = request.POST.get("pid")

    product = Product.objects.filter(categoryId=gid,
                                     childId=cid).get(productId=pid)

    try:
        cart = Cart.objects.filter(user__tokenValue=tokenValue).filter(
            product__categoryId=gid).filter(product__childId=cid).get(
                product__productId=pid)
        if flag == "2":
            if product.storeNums == "0":
                return JsonResponse({"error": 0, "num": cart.num})
        #以买过该商品,得到了该购物车数据
        cart.num = cart.num + num
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
        if cart.num == 0:
            cart.delete()
        else:
            cart.save()

    except Cart.DoesNotExist as e:
        if flag == "1":
            return JsonResponse({"error": 0, "num": 0})
        #找一个可用的订单  isDelete为False,flag为0,在当前用户中的所有订单中最多只能有一个订单为0
        try:
            order = Order.orders2.filter(user__tokenValue=tokenValue).get(
                flag=0)
        except Order.DoesNotExist as e:
            #没有可用订单
            orderId = str(uuid.uuid4())
            address = Address.objects.get(pk=1)
            order = Order.create(orderId, user, address, 0)
            order.save()
        #没有购买过该商品,需要创建该条购物车数据
        cart = Cart.create(user, product, order, 1)
        cart.save()
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
    # 告诉客户端添加成功
    return JsonResponse({"error": 0, "num": cart.num})
Example #3
0
def changecart(request, flag):
    num = 1
    if flag == "1":
        num = -1
    tokenValue = request.COOKIES.get("token")
    if not tokenValue:
        return JsonResponse({"error": 1})
    try:
        # 找到用户
        user = User.objects.get(tokenValue=tokenValue)
    except User.DoesNotExist as e:
        return JsonResponse({"error": 2})

    gid = request.POST.get("gid")
    cid = request.POST.get("cid")
    pid = request.POST.get("pid")
    # 找到商品
    product = Product.objects.filter(categoryId=gid, childId=cid).get(productId=pid)
    # 找到该商品的购物车数据
    try:
        cart = Cart.objects.filter(user__tokenValue=tokenValue).filter(product__categoryId=gid).filter(
            product__childId=cid).get(product__productId=pid)
        # 添加商品时,如果商品库存为0,就不在添加,数据(num)返回
        if flag == "2":
            if product.storeNums == "0":
                return JsonResponse({"error": 0, "num": cart.num})
        # 买过商品,把购物车数据加上
        cart.num = cart.num + num
        # 减该商品库存
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
        # 当商品数量为0时,删除购物车数据。不为0保存数据
        if cart.num == 0:
            cart.delete()
        else:
            cart.save()
    except Cart.DoesNotExist as e:
        # 没有该商品购物车数据,点击减商品时,不会有变化。
        if flag == "1":
            return JsonResponse({"error": 0, "num": 0})
        # 数据不存在说明没有买过该商品,创建数据添加到购物车。
        # 找到一个可用的订单flag为0的
        try:
            order = Order.orders2.filter(user__tokenValue=tokenValue).get(flag=0)
        except Order.DoesNotExist as e:
            # 没有可用订单,创建订单
            orderId = str(uuid.uuid4())
            address = Address.objects.get(pk=1)
            order = Order.create(orderId, user, address, 0)
            order.save()
        # 没有购买过该商品,创建购物车数据
        cart = Cart.create(user, product, order, 1)
        cart.save()
        # 减该商品库存
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
        # 把数据返回
    return JsonResponse({"error": 0, "num": cart.num})
Example #4
0
def addcart(request):
    goodsid = request.GET.get('goodsid')
    token = request.session.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 = 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)
Example #5
0
def addcart(request):
    token = request.session.get('token', '')

    response_data = {}
    if token:

        goodid = request.GET.get('goodid')
        good = Goods.objects.get(pk=goodid)

        response_data['statue'] = 1
        user = User.objects.get(token=token)
        carts = Cart.objects.filter(user=user).filter(goods=good)
        if carts.exists():
            cart = carts.first()
            cart.number += 1
            cart.save()
        else:
            cart = Cart()
            cart.user = user
            cart.goods = good
            cart.number = 1
            cart.save()

        response_data['statue'] = 1
        response_data['number'] = cart.number
        response_data['msg'] = '添加{}商品成功到购物车{}'.format(
            cart.goods.productlongname, cart.number)
        return JsonResponse(response_data)
    else:
        response_data['statue'] = -1
        return JsonResponse(response_data)
Example #6
0
File: views.py Project: buddysh/AXF
def addcart(request):
    token = request.session.get('token')
    response_data = {}

    if token:
        userid = cache.get(token)
        if userid:
            user = User.objects.get(pk=userid)
            goodsid = request.GET.get('goodsid')
            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()
            else:
                cart = Cart()
                cart.user = user
                cart.goods = goods
                cart.number = 1
                cart.save()

            response_data['status'] = 1
            response_data['number'] = cart.number
            response_data['msg'] = '添加 {} 购物车成功: {}'.format(
                cart.goods.productlongname, cart.number)

        return JsonResponse(response_data)
    response_data['status'] = -1
    response_data['msg'] = '请登录后操作'
    return JsonResponse(response_data)
Example #7
0
def add_to_cart(request):
    # 判断用户是否登录
    username = request.session.get('username')

    if username == None:
        return redirect(reverse('axf:login'))

    # 用户已登录
    goods_id = request.GET.get('goods_id')
    goods = Goods.objects.get(pk=goods_id)

    # 获取用户
    user = User.objects.get(u_name=username)

    c = Cart.objects.filter(c_user=user).filter(c_goods=goods).filter(
        c_belong=False)

    if len(c) == 0:
        # 存储购物信息
        c = Cart()
    else:
        c = c.first()
        c.c_goods_num += 1

    c.c_user = user
    c.c_goods = goods

    c.save()

    return JsonResponse({'msg': '添加成功'})
Example #8
0
def changecart(request,flag):
    num = 1
    if flag == '1':
        num = -1
    #判断是否登陆
    tokenValue=request.COOKIES.get('token')
    if not tokenValue:
        return JsonResponse({'error':1})
    try:
        user = User.objects.get(tokenValue=tokenValue)
    except User.DoesNotExist as e:
        return JsonResponse({'error':2})

    gid = request.POST.get('gid')
    cid = request.POST.get('cid')
    pid = request.POST.get('pid')
    product = Product.objects.filter(categoryId=gid,childId=cid).get(productId=pid)
    try:
        cart = Cart.objects.filter(user__tokenValue=tokenValue).filter(product__categoryId=gid).filter(product__childId=cid).get(product__productId=pid)
        if flag == '2':
            if product.storeNums == '0':
                return JsonResponse ({'error':0,'num':cart.num})
        cart.num = cart.num + num
        product.storeNums = str(int(product.storeNums) - num)
        product.save()
        if cart.num == 0:
            cart.delete()
        else:
            cart.save()

    except Cart.DoesNotExist as e:
        if flag == '1':
            return  JsonResponse({'error':0,'num':0})
        try:
            order = Order.orders2.filter(user__tokenValue=tokenValue).get(flag=0)
        except Order.DoesNotExist as e:
            orderId = str(uuid.uuid4())
            address = Address.objects.get(pk=3)
            order = Order.create(orderId,user,address,0)
            order.save()
        cart = Cart.create(user,product,order,1)
        cart.save()
        product.storeNums = str(int(product.storeNums) - num)
        product.save()

    return  JsonResponse({'error':0,'num':cart.num})
Example #9
0
def carts(request):
    num = int(request.GET.get("num"))

    #已知用户
    # request.user和request.token 已在中间件check..定义其为当前用户和用户的tokenValue值
    user = request.user

    #获取要添加到购物车的商品
    gid = request.GET.get('gid')
    pid = request.GET.get('pid')
    product = Product.objects.filter(category_id=gid).get(product_id=pid)
    if product.store_nums == 0 and num == 1:
        return JsonResponse({'error': 2})

    #当前用户的所有没进入订单的购物车信息
    usercarts = Cart.objects.filter(user__tokenValue=request.token)
    try:
        productCart = usercarts.get(product__product_id=pid)
        #添加过
        productCart.num += num
        #库存
        product.store_nums -= num
        product.save()
        if productCart.num == 0:
            productCart.delete()
        else:
            productCart.isChose = not productCart.isChose
            productCart.save()
    except Cart.DoesNotExist as e:
        #没添加过
        if num == 1:
            productCart = Cart.create(user, None, product, 1)
            productCart.save()
            # 库存
            product.store_nums -= num
            product.save()
        elif num == -1:
            return JsonResponse({'error': 0, "num": 0})

    return JsonResponse({
        'error': 0,
        "num": productCart.num,
        "isChose": productCart.isChose
    })
Example #10
0
def addcart(request):
    goodsid = request.GET.get('goodsid')
    token = request.session.get('token')

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

    if token:  # 登录 [直接操作模型]
        # 获取用户
        user = User.objects.get(token=token)
        print('#######################')

        # 获取商品
        goods = Goods.objects.get(pk=goodsid)
        print(goods)

        # 商品已经在购物车,只修改商品个数
        # 商品不存在购物车,新建对象(加入一条新的数据)
        carts = Cart.objects.filter(user=user).filter(goods=goods)
        print(carts)

        if carts.exists():  # 修改数量
            cart = carts.first()
            print(cart)

            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:  # 未登录 [跳转到登录页面]
        # 由于addcart这个是 用于 ajax操作, 所以这里是不能进行重定向!!
        # return redirect('axf:login')
        responseData['msg'] = '未登录,请登录后操作'
        responseData['status'] = -1
        return JsonResponse(responseData)
Example #11
0
def addcart(request):
    token = request.session.get('token', '')

    userid = cache.get(token)
    # print(userid) #获取登录用户信息userid

    if userid:  #true是已经登录状态
        user = User.objects.get(pk=userid)
        goodsid = request.GET.get('goodsid')
        # print('收到ajax数据产品id',goodsid)
        goods = Goods.objects.get(pk=goodsid)

        carts = Cart.objects.filter(user=user).filter(goods=goods)
        if carts.exists():
            cart = carts.first()
            cart.goodsnumber += 1
            cart.save()

        else:
            cart = Cart()
            cart.user = user
            cart.goods = goods
            cart.goodsnumber = 1
            cart.save()
        response_data = {
            'status':
            1,
            'mig':
            '添加{}成功添加总数量{}'.format(cart.goods.productlongname,
                                   cart.goodsnumber),
            'goodsnumber':
            cart.goodsnumber
        }
        return JsonResponse(response_data)

    response_data = {'status': 0, 'msg': '请先进行登录'}
    return JsonResponse(response_data)
Example #12
0
def changecart(request, flag):
    # 首先判断用户是否登录
    token = request.session.get('token')
    if token == None:
        return JsonResponse({'data': -1, 'status': 'error'})
    productid = request.POST.get('productid')
    user = User.objects.get(userToken=token)
    product = ProductGoods.objects.get(productid=productid)
    if flag == '0':
        if product.storenums == 0:
            return JsonResponse({'data': -2, 'status': 'error'})
        carts = Cart.obj1.filter(userAccount=user.userAccount)
        c = None
        if carts.count() == 0:
            c = Cart.createuser(user.userAccount, productid, 1, product.price,
                                True, product.productimg,
                                product.productlongname, False)
            c.save()
        else:
            try:
                c = carts.get(productid=productid)
                # print(c.productid)
                c.productnum += 1
                c.productprice = '%.2f' % (float(product.price) * c.productnum)
                c.save()
            except Cart.DoesNotExist as e:
                c = Cart.createuser(user.userAccount, productid, 1,
                                    product.price, True, product.productimg,
                                    product.productlongname, False)
                # print(c.productid)
                c.save()
        product.storenums -= 1
        product.save()
        return JsonResponse({
            'data': c.productnum,
            'price': c.productprice,
            'status': 'success'
        })
    elif flag == '1':
        carts = Cart.obj1.filter(userAccount=user.userAccount)
        # print(len(carts))
        # print(carts.count())
        # print(productid)
        c = None
        if carts.count() == 0:
            return JsonResponse({'data': -2, 'status': 'error'})
        else:
            try:
                # print('productid',productid)
                c = carts.get(productid=productid)
                c.productnum -= 1
                # print(c.productnum)
                c.productprice = '%.2f' % (float(product.price) * c.productnum)
                if c.productnum == 0:
                    c.delete()
                else:
                    # print(c.productnum)
                    c.save()
            except Cart.DoesNotExist as e:
                return JsonResponse({'data': -2, 'status': 'error'})
        product.storenums += 1
        product.save()
        return JsonResponse({
            'data': c.productnum,
            'price': c.productprice,
            'status': 'success'
        })
    elif flag == '2':
        carts = Cart.obj1.filter(userAccount=user.userAccount)
        c = carts.get(productid=productid)
        c.isChose = not c.isChose
        c.save()
        str = ""
        color = "white"
        if c.isChose:
            str = '√'
            color = 'yellow'
        return JsonResponse({'data': str, 'status': 'success', 'color': color})
    elif flag == '3':
        text = request.POST.get('text')
        if text:
            carts = Cart.obj1.filter(userAccount=user.userAccount)
            for c in carts:
                c.isChose = True
                c.save()
            str = "√"
            color = "yellow"
            return JsonResponse({
                'data': str,
                'status': 'success',
                'color': color
            })
        else:
            carts = Cart.obj1.filter(userAccount=user.userAccount)
            for c in carts:
                c.isChose = False
                c.save()
            str = ""
            color = "white"
            return JsonResponse({
                'data': str,
                'status': 'success',
                'color': color
            })
    elif flag == '4':
        try:
            carts = Cart.obj1.filter(userAccount=user.userAccount)
            c = carts.get(productid=productid)
            str = ""
            color = "white"
            # print(c.productname)
            # print(c.isChose)
            if c.isChose:
                str = '√'
                color = 'yellow'
            return JsonResponse({
                'data': str,
                'status': 'success',
                'color': color,
                'productid': productid
            })
        except Cart.DoesNotExist as e:
            return JsonResponse({'data': -2, 'status': 'error'})
Example #13
0
def changecart(request,flag):
    tokenValue = request.COOKIES.get('token')
    print('----------------购物车页面用户token',tokenValue)

    try:
        user_motto,phone = find_user_data(request.session.get(str(tokenValue)))
    except:
        print('------------通过token获取用户信息出错')
        return JsonResponse({'login':True})
    if user_motto and phone:
        print('--------------第一阶段认证通过')
        try:
            user = User.objects.get(token=tokenValue)
            print('--------------第二阶段认证通过')
            print('获取当前请浏览器的token---------------',tokenValue)
            print('获取当前请求的用户phone------------------', phone)
        except:
            print('--------------------用户Token异常',tokenValue)
            return JsonResponse({'Error': True})

        pid = request.POST.get("pid")
        gid = request.POST.get("gid")
        ppid = request.POST.get("ppid")
        product = Product.objects.filter(categoryId=gid).get(productId=pid)
        try:
            # 根据用户手机号码,试图找到收货地址
            user_motto, phone = find_user_data(request.session.get(str(tokenValue)))
            Address.objects.get(phoneNum=phone)
            print('用户有收货地址')
        except:
            # 如果没有找到用户地址,提醒用户去添加地址
            print('-------------用户没有收货地址')
            return JsonResponse({"None": 3})
        try:
            # 如果没报错,证明用户购买过商品   定位用户
            user_motto, phone = find_user_data(request.session.get(str(request.COOKIES.get('token'))))
            cart = Cart.objects.filter(user__phoneNum=phone).get(product__id=ppid)
            # yibu(flag, cart, product, ppid)
            if flag == '1':
                print('--------------用户增加商品')
                if int(product.storeNums) == 0:
                    print('------------商品库存为空')
                    return JsonResponse({'Null': True})
                else:
                    cart.num = str(int(cart.num) + 1)
                    pdt = Product.objects.get(id=ppid)
                    pdt.storeNums = str(int(pdt.storeNums) - 1)
                    pdt.save()
                    cart.save()
                    print('------------增加购物车的数量')
                    return JsonResponse({'Succeed': cart.num})
            else:
                if cart.num == 0:
                    # cart.delete()

                    return JsonResponse({'reload': True})
                cart.num = str(int(cart.num) - 1)
                pdt = Product.objects.get(pk=ppid)
                pdt.storeNums = str(int(pdt.storeNums) + 1)
                pdt.save()
                cart.save()
                print('------------去掉购物车的数量')
                return JsonResponse({'Succeed': cart.num})
        except:
            print('--------------没有找到用户的购物车订单')
            orderId = str(uuid.uuid4())
            # 获取用户的           收货地址
            address = Address.objects.get(user__phoneNum=phone)
            # 创建                订单数据
            order = Order.create(orderId, user, address, product.price)
            # 保存数据
            order.save()
            # 再次找到            订单对象
            oder = Order.orders2.get(pk=orderId)
            # 创建                 购物车数据
            cart = Cart.create(user=user, product=product, order=oder, num = 1)
            cart.save()
            product.storeNums = str(int(product.storeNums) - 1)
            product.save()
            # 告诉客户端添加成功
            print('----------------新用户/新商品数据增加完毕')
            return JsonResponse({'Succeed': cart.num})
    else:
        print('用户身份认证失败')
        return render(request, 'mine/login.html')