Esempio n. 1
0
def shopping(request):
    data = {}
    if request.method == 'GET':
        token = request.session.get('token')
        if token:
            user = User.objects.get(token=token)
            cart = Cart.objects.filter(user=user).filter(is_delete=0)
            data['cart_num'] = cart.count()
            data['user_name'] = user.name
            data['cart'] = cart
            return render(request, 'shopping.html', context=data)
        else:
            return render(request, 'shopping.html')

    else:
        token = request.session.get('token')
        if token:
            user = User.objects.get(token=token)

            name = request.POST.get('name')
            number = request.POST.get('number')
            price = request.POST.get('price')
            size = request.POST.get('size')
            smallimg = request.POST.get('smallimg')

            carts = Cart.objects.filter(user=user).filter(name=name).filter(
                smallimg=smallimg)
            if carts.filter(size=size).filter(is_delete=0).exists():
                cart = carts.first()
                cart.number += eval(number)
                cart.save()
            else:
                cart = Cart()
                cart.user = user
                cart.name = name
                cart.price = price
                cart.size = size
                cart.smallimg = smallimg
                cart.number = number
                cart.is_delete = 0
                cart.save()
            # 1、 第一次添加的商品是不存在的,要往数据库中添加一条新记录
            return JsonResponse({
                'msg': '{},添加购物车成功'.format(name),
                'number': cart.number,
                'status': 1
            })
        else:
            return JsonResponse({'msg': '添加购物车失败,请先登录', 'status': 0})
Esempio n. 2
0
def cart_add():
    """
    添加到购物车
    :return:
    """
    params = request.values or request.get_json()
    user = User.get_user_by_token(params.get('token'))
    cart_item_id = params.get("cart_item_id")
    cart_sku_id = params.get("cart_sku_id")
    cart_add_from = params.get("cart_add_from")
    cart_sku_num = params.get("cart_sku_num")
    requires = [
        "cart_item_id",
        "cart_sku_id",
        "cart_sku_num",
    ]
    for r in requires:
        if params.get(r) is None:
            return CommonError.args_miss(msg='{}_required'.format(r))
    cart_item: Cart
    try:
        cart_item = db.session.query(Cart).filter_by(cart_item_id=cart_item_id,
                                                     cart_sku_id=cart_sku_id,
                                                     is_delete=0).one()
        if cart_item is not None:
            cart_item = cart_item
            cart_item.cart_sku_num += int(cart_sku_num)
            db.session.add(cart_item)
            db.session.commit()
    except NoResultFound:
        cart_item = Cart()
        cart_item.cart_item_id = cart_item_id
        cart_item.cart_sku_id = cart_sku_id
        cart_item.cart_add_from = int(cart_add_from) or 1
        cart_item.cart_sku_num = cart_sku_num
        cart_item.cart_item_belong = user.user_id
        cart_item.is_delete = 0
        db.session.add(cart_item)
        db.session.commit()
    except:
        return CommonError.getError(errorCode=999)
    return responseSuccessHandler()