def addOrder(request): orderids = request.session.get('orderids') order = OrderInfo() oOder = OrderInfo.objects.all().order_by('-oid')[0:1] if len(oOder) == 0: order.oid = 1 else: order.oid = int(oOder[0].oid) + 1 order.odate = datetime.now() order.oIsPay = 0 order.ototal = request.POST.get('total') order.oaddress = request.POST.get('address') order.user_id = request.session.get('user_id') order.zhifu = request.POST.get('zhifu') order.save() for oid in orderids: cartInfo = CartInfo.objects.get(id=oid) good = GoodsInfo.objects.get(id=cartInfo.goods_id) if cartInfo.count > good.gkucun: return JsonResponse({'status': 2}) detail = OrderDetailInfo() detail.price = good.gprice detail.count = cartInfo.count detail.goods_id = good.id detail.order_id = order.oid detail.save() return JsonResponse({'status': 1})
def order_handle(request): #保存一个事物点 tran_id = transaction.savepoint() #接收购物车编号 # 根据POST和session获取信息 # cart_ids=post.get('cart_ids') try: post = request.POST orderlist = post.getlist('id[]') total = post.get('total') address = post.get('address') order=OrderInfo() now=datetime.now() uid = request.session.get('user_id') order.oid='%s%d'%(now.strftime('%Y%m%d%H%M%S'),uid) order.user_id=uid order.odate=now order.ototal=Decimal(total) order.oaddress = address order.save() # 遍历购物车中提交信息,创建订单详情表 for orderid in orderlist: cartinfo = CartInfo.objects.get(id=orderid) # good = GoodsInfo.objects.get(cartinfo__id=cartinfo.id) good = GoodsInfo.objects.get(pk=cartinfo.goods_id) # print '*'*10 # print cartinfo.goods_id # 判断库存是否够 if int(good.gkucun) >= int(cartinfo.count): # 库存够,移除购买数量并保存 good.gkucun -= int(cartinfo.count) good.save() goodinfo = GoodsInfo.objects.get(cartinfo__id=orderid) # 创建订单详情表 detailinfo = OrderDetailInfo() detailinfo.goods_id = int(goodinfo.id) detailinfo.order_id = int(order.oid) detailinfo.price = Decimal(int(goodinfo.gprice)) detailinfo.count = int(cartinfo.count) detailinfo.save() # 循环删除购物车对象 cartinfo.delete() else: # 库存不够出发事务回滚 transaction.savepoint_rollback(tran_id) # 返回json供前台提示失败 return JsonResponse({'status': 2}) except Exception as e: # print '==================%s'%e transaction.savepoint_rollback(tran_id) # 返回json供前台提示成功 return JsonResponse({'status': 1})
def order_handle(request): #保存一个事物点 tran_id = transaction.savepoint() #接收购物车编号 # 根据POST和session获取信息 # cart_ids=post.get('cart_ids') try: post = request.POST orderlist = post.getlist('id[]') total = post.get('total') address = post.get('address') order=OrderInfo() now=datetime.now() uid = request.session.get('user_id') order.oid='%s%d'%(now.strftime('%Y%m%d%H%M%S'),uid) order.user_id=uid order.odate=now order.ototal=Decimal(total) order.oaddress = address order.save() # 遍历购物车中提交信息,创建订单详情表 for orderid in orderlist: cartinfo = CartInfo.objects.get(id=orderid) # good = GoodsInfo.objects.get(cartinfo__id=cartinfo.id) good = GoodsInfo.objects.get(pk=cartinfo.goods_id) # print '*'*10 # print cartinfo.goods_id # 判断库存是否够 if int(good.gkucun) >= int(cartinfo.count): # 库存够,移除购买数量并保存 good.gkucun -= int(cartinfo.count) good.save() goodinfo = GoodsInfo.objects.get(cartinfo__id=orderid) # 创建订单详情表 detailinfo = OrderDetailInfo() detailinfo.goods_id = int(goodinfo.id) detailinfo.order_id = int(order.oid) detailinfo.price = Decimal(int(goodinfo.gprice)) detailinfo.count = int(cartinfo.count) detailinfo.save() # 循环删除购物车对象 cartinfo.delete() else: # 库存不够出发事务回滚 transaction.savepoint_rollback(tran_id) # 返回json供前台提示失败 return JsonResponse({'status': 2}) except Exception as e: print('==================%s'%e) transaction.savepoint_rollback(tran_id) # 返回json供前台提示成功 return JsonResponse({'status': 1})
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/'})
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/')
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/')
def addOrder(request):#先获取下单的所有购物车的id orderids = request.session.get('orderids') #构建一个订单对象 order = OrderInfo() #查询订单最大的id oOder = OrderInfo.objects.all().order_by('oid')[0:1] if len(oOder) == 0: order.oid = 1 else: print(int(oOder[0].oid)) order.oid = int(oOder[0].oid)+1 #增加订单时间 order.odate = datetime.now() #是否付款 order.oIsPay =0 order.ototal=request.POST.get('totle') order.oaddress= request.POST.get('address') order.user_id= request.session.get('user_id') order.zhifu= request.POST.get('zhifu') order.save() #增加订单明细的商品洗信息 for oid in orderids:#获取购物车(会有多条数据) cartInfo = CartInfo.objects.get(id=oid) good = GoodInfo.objects.get(id=cartInfo.goods_id) #如果返回2表示库存不够 if cartInfo.count>good.gkucun: return JsonResponse({'status':2}) #定义一个明细订单 detail = OrderDetailInfo() detail.price = good.gprice detail.count = cartInfo.count detail.goods_id = good.id detail.price = order.oid detail.save() #如果返回1表示增加成功 return JsonResponse({'status':1})
def order_handle(request): tran_id = transaction.savepoint() try: post = request.POST orderlist = post.getlist('id[]') total = post.get('total') address = post.get('address') order = OrderInfo() now = datetime.now() uid = request.session.get('user_id') order.oid = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid) order.user_id = uid order.odate = now order.ototal = Decimal(total) order.oaddress = address order.save() for orderid in orderlist: cartinfo = CartInfo.objects.get(id=orderid) good = GoodsInfo.objects.get(pk=cartinfo.goods_id) if int(good.gkucun) >= int(cartinfo.count): good.gkucun -= int(cartinfo.count) good.save() goodinfo = GoodsInfo.objects.get(cartinfo__id=orderid) detailinfo = OrderDetailInfo() detailinfo.goods_id = int(goodinfo.id) detailinfo.order_id = int(order.oid) detailinfo.price = Decimal(int(goodinfo.gprice)) detailinfo.count = int(cartinfo.count) detailinfo.save() cartinfo.delete() else: transaction.savepoint_rollback(tran_id) return JsonResponse({'status': 2}) except Exception as e: print '==================%s' % e transaction.savepoint_rollback(tran_id) return JsonResponse({'status': 1})