Esempio n. 1
0
def order_handle(request):
    tran_id = transaction.savepoint()
    # 接收购物车编号
    cart_ids = request.POST.get('cart_ids')
    try:
        # 创建订单对象
        order = OrderInfo()
        now = datetime.now()
        uid = request.session['user_id']
        order.oid = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid)
        order.user_id = uid
        order.odate = now
        order.oaddress = request.POST.get('address')
        order.ototal = 0
        order.save()

        # 创建详单对象
        cart_ids1 = [int(item) for item in cart_ids.split(',')]
        total = 0
        for id1 in cart_ids1:
            detail = OrderDetailInfo()
            detail.order = order
            # 查询购物车信息
            cart = CartInfo.objects.get(id=id1)
            # 判断商品库存
            goods = cart.goods
            # 如果库存大于购买量
            if goods.gkuncun >= cart.count:
                # 减少商品数量
                goods.gkuncun = cart.goods.gkuncun - cart.count
                goods.save()
                # 完善订单信息
                detail.goods_id = goods.id
                price = goods.gprice
                detail.price = price
                count = cart.count
                detail.count = count
                detail.save()
                total = total + price * count
                # 删除购物车数据
                cart.delete()
            # 如果库存小于购买量
            else:
                transaction.savepoint_rollback(tran_id)
                return redirect('/cart/')
        # 保存总价
        order.ototal = total + 10
        order.save()
        transaction.savepoint_commit(tran_id)

    except Exception as e:
        print "========================%s" % e
        transaction.savepoint_rollback(tran_id)
    return redirect('/user_center_order/')
Esempio n. 2
0
def order_handle(request):
    """提交订单"""
    # 保存一个点(django中的事务)
    tran_id = transaction.savepoint()

    cart_ids = str(request.POST.get('cart_ids'))
    total = request.POST.get('total')
    print('cart_ids:%s'%cart_ids)
    print('total:%s' % total)
    try:
        order = OrderInfo()
        now = datetime.now()
        uid = request.session['id']
        # 采用订单创建时间+用户id来表示订单编号
        order.oid = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid)
        order.user_id = uid
        order.odate = now
        order.ototal = Decimal(request.POST.get('total'))
        order.save()

        cart_ids1 = [int(item) for item in cart_ids.split(' ')]
        for id1 in cart_ids1:
            detail = OrderDetailInfo()
            # 表示外键order是当前的订单对象
            detail.order = order

            cart = CarInfo.objects.get(id=id1)
            goods = cart.goods
            # 判断库存
            print goods.gkucun
            if goods.gkucun >= cart.count:
                goods.gkucun = cart.goods.gkucun - cart.count

                print cart.count
                print goods.gkucun
                goods.save()

                detail.goods_id = goods.id
                detail.price = goods.gprice
                detail.count = cart.count
                detail.save()

                cart.delete()
            else:

                transaction.savepoint_rollback(tran_id)
                return JsonResponse({'url':'/carts/'})

        transaction.savepoint_commit(tran_id)
    except Exception as e:
        print '===========%s' % e
        transaction.savepoint_rollback(tran_id)

    return JsonResponse({'url':'/user/order_1/'})
Esempio n. 3
0
def order_handle(request):
    tran_id = transaction.savepoint()
    # 接收购物车编号
    cart_ids = request.POST.get('cart_ids')  # 5,6
    try:
        # 创建订单对象
        order = OrderInfo()
        now = datetime.now()
        uid = request.session['user_id']
        order.oid = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid)
        order.user_id = uid
        # print order.oid
        order.odate = now
        order.ototal = Decimal(request.POST.get('total'))
        order.oaddress = request.POST.get('address')
        order.save()
        # 创建详单对象
        cart_ids1 = [int(item) for item in cart_ids.split(',')]
        for id1 in cart_ids1:
            detail = OrderDetailInfo()
            detail.order = order
            # 查询购物车信息
            cart = CartInfo.objects.get(id=id1)
            # 判断商品库存
            goods = cart.goods
            if goods.gkucun >= cart.count:  # 如果库存大于购买数量
                # 减少商品库存
                goods.gkucun = cart.goods.gkucun - cart.count
                goods.save()
                # 完善详单信息
                detail.goods_id = goods.id
                detail.price = goods.gprice
                detail.count = cart.count
                detail.save()
                # 删除购物车数据
                cart.delete()
            else:  # 如果库存小于购买数量
                transaction.savepoint_rollback(tran_id)
                return redirect('/cart/')
            # return HttpResponse('no')
        transaction.savepoint_commit(tran_id)
    except Exception as e:
        print
        '================%s' % e
        transaction.savepoint_rollback(tran_id)
    return redirect('/order/pay/')
Esempio n. 4
0
def do_order(request):
    # {'cid': cids,'pay_style': pay_style},
    pay_style = request.GET.get('pay_style')  # cash  zfb
    cids = request.GET.getlist('cid')  # 购物车的id
    uid = request.session.get('user_id')  # 用户id
    user = UserInfo.objects.get(id=uid)  # 用户

    # 开启事务
    sid = transaction.savepoint()  #
    # 创建订单
    order = OrderInfo()
    order.oid = str(int(time.time() * 1000)) + str(uid)  # 订单编号
    order.user_id = uid
    order.ototal = 0  # 订单金额

    order.oaddr = UserAddressInfo.objects.get(user=user).uaddress  # 订单地址
    #address = order.oaddr.objects.get("uaddress")
    order.save()
    cart_list = CartInfo.objects.filter(id__in=cids)  # 查找到所有购物车对象
    total_price = 0
    total_count = 0
    isOk = True
    for cart in cart_list:
        #
        if cart.count <= cart.goods.gkucun:
            # 库存充足
            detail = OrderDetailInfo()
            detail.order = order
            detail.goods = cart.goods
            detail.price = cart.goods.gprice
            detail.count = cart.count
            detail.save()

            # 更改库存
            cart.goods.gkucun -= cart.count
            cart.goods.save()

            # 计算总价
            total_price += detail.count * detail.price
            total_count += detail.count

            # 删除购物车对象
            cart.delete()
        else:
            # 库存不够
            isOk = False
            break

    if isOk:
        # 最终提交订单
        order.ototal = total_price
        # 订单成功
        if pay_style == 'cash':
            order.oIsPay = 1
            order.save()
            transaction.savepoint_commit(sid)  # 提交事务
            return JsonResponse({'res': 1})

        else:
            order_id = order.oid  # 订单编号
            # total_pay = order.ototal
            total_pay = order.ototal
            order_string = alipay.api_alipay_trade_page_pay(
                out_trade_no=order_id,
                total_amount=str(total_pay),
                subject=f'粤嵌科技{order_id}',
                return_url='http://127.0.0.1:8000/order/check_pay/?order_id=' +
                order_id,
                notify_url='http://127.0.0.1:8000/order/check_pay/?order_id=' +
                order_id,
            )
            alipay_url = settings.ALIPAY_URL + '?' + order_string
            return JsonResponse({'res': 3, 'pay_url': alipay_url})
    else:
        # 订单失败
        transaction.savepoint_rollback(sid)
        return JsonResponse({'res': 0})