Example #1
0
def orderadd(request):
    get = request.GET
    carts = get.get('cart_id')
    sumprice = get.get('price')
    codenum = get.get('code_num')
    try:
        if codenum == 0:
            order_point = transaction.savepoint()
            cartlist = carts.split(',')
            userid = request.session.get('u_id')
            u = UserInfo.user.get(pk=userid)
            order = OrderInfo()
            order.ouser = u
            order.oprice = sumprice
            order.save()
            for item in cartlist:
                if item:
                    cart = CartInfo.objects.get(pk=item)
                    orderdetail = OrderDetails()
                    orderdetail.count = cart.cCount
                    orderdetail.goods = cart.cGoods
                    orderdetail.price = cart.cGoods.gprice
                    orderdetail.order = order
                    orderdetail.save()
                    cart.delete()
            return JsonResponse({'code': 1, 'url': '/user/order/'})
        else:
            # 修改订单状态
            return JsonResponse({'code': 1, 'url': '/user/order/'})
        transaction.savepoint_commit(order_point)
    except Exception as res:
        print(res)
        transaction.savepoint_rollback(order_point)
        return JsonResponse({'code': 0, 'url': '/user/order/'})
Example #2
0
def create_orders(request):
    if request.method == 'GET':
        user_ids = ['user_1556023764', 'user_1556024764', 'user_1556025764']
        users = ['佩奇', '嘉哥', '珠珠']
        # for index in range(3):
        #     User.objects.create(
        #         username=users[index],
        #         password=make_password('123456'),
        #         user_id=user_ids[index],
        #     )
        provinces = ['四川', '广东', '浙江', '四川', '福建', '广东', '四川']
        cities = ['成都', '广州', '宁波', '泸州', '泉州', '珠海', '德阳']
        districts = ['龙泉驿', '海珠', '北仑', '江阳', '江阴', '哈口', '什邡']
        addresses = [
            '迎晖路1080号', '杏园大街2080号', '大碶街道6324号', '诸子街1001号', '留下街343号',
            '海天321号', '回澜路12号'
        ]
        signer_names = ['张三', '李四', '王五', '杨二', '黄流', '吴奇隆', '蔡徐坤']
        signer_mobiles = [
            '13258230120', '18628133324', '15289044194', '12342343412',
            '13423233551', '18623232123', '18913213132'
        ]

        for gd in range(200):
            signer_name = random.choice(signer_names)
            index = signer_names.index(signer_name)
            goods_values = ['on', '']
            # 获取用户信息
            user = User.objects.filter(user_id=random.choice(user_ids)).first()
            # 创建签收者对象,保存信息
            signer_address = SignerAddress()
            signer_address.user = user
            signer_address.province = provinces[index]
            signer_address.city = cities[index]
            signer_address.district = districts[index]
            signer_address.address = addresses[index]
            signer_address.signer_name = signer_names[index]
            signer_address.signer_mobile = signer_mobiles[index]
            signer_address.save()

            # 创建货物对象,保存信息
            goods = GoodsInfo()
            goods.user = user
            goods.goods_id = '15560247' + str(gd)
            goods.goods_weight = random.randint(1, 10)
            goods.goods_volume = random.randint(3, 7)
            goods.goods_value = random.choice(goods_values)
            goods.goods_fast = random.choice(goods_values)
            goods.save()

            # 创建订单对象
            order = OrderInfo()
            order.user = user
            order.signer = signer_address
            order.goods = goods

            order.save()
        return HttpResponseRedirect(reverse('ruler:login'))
def create_orders():
    user_ids = ['user_1556023764', 'user_1556024764', 'user_1556025764']
    users = ['佩奇', '嘉哥', '珠珠']
    for index in range(3):
        User.objects.create(
            username=users[index],
            password=make_password('123456'),
            user_id=user_ids[index],
        )
    provinces = ['四川', '广东', '浙江']
    cities = ['成都', '广州', '宁波']
    districts = ['龙泉驿', '海珠', '北仑']
    addresses = ['迎晖路1080号', '杏园大街2080号', '大碶街道6324号']
    signer_names = ['张三', '李四', '王五']
    signer_mobiles = ['13258230120', '18628133324', '15289044194']

    for _ in range(20):
        province = random.choice(provinces)
        index = provinces.index(province)
        goods_values = ['on', '']
        # 获取用户信息
        user = User.objects.filter(user_id=random.choice(user_ids)).first()
        # 创建签收者对象,保存信息
        signer_address = SignerAddress()
        signer_address.user = user
        signer_address.province = province
        signer_address.city = cities[index]
        signer_address.district = districts[index]
        signer_address.address = addresses[index]
        signer_address.signer_name = signer_names[index]
        signer_address.signer_mobile = signer_mobiles[index]
        signer_address.save()

        # 创建货物对象,保存信息
        goods = GoodsInfo()
        goods.user = user
        goods.goods_id = int(time.time())
        goods.goods_weight = random.randint(10)
        goods.goods_volume = random.randint(7)
        goods.goods_value = random.choice(goods_values)
        goods.goods_fast = random.choice(goods_values)
        goods.save()

        # 创建订单对象
        order = OrderInfo()
        order.user = user
        order.signer = signer_address
        order.goods = goods

        order.save()
    return
Example #4
0
def add_order(request):
    post = request.POST
    if post:
        address = Address.objects.get(pk=int(post.get('address')))
        address = '%s (%s收) %s' % (address.add, address.recipients,
                                   address.cel)
        cart_ids = post.get('cart_ids').split(',')
        cart_ids = [int(i) for i in cart_ids]
        carts = CartInfo.objects.filter(pk__in=cart_ids)

        sid = transaction.savepoint()  # 事务起点

        try:
            # 创建订单
            order = OrderInfo()
            now = datetime.now()
            order.pk = int(
                now.strftime('%s'))  # 订单编号:当前时间now.strftime('%Y%m%d%H%M%S')
            order.user_id = int(request.session.get('userid'))  # 用户编号
            order.address = address  # 地址
            order.total = 0  # 总金额
            order.save()
            # 1/0 异常模拟

            # 创建订单明细
            total = 0
            for cart in carts:
                if cart.nums > cart.cart_good.stock:  # 如果数量大于库存
                    transaction.savepoint_rollback(sid)  # 回滚到起点
                    return redirect(reverse('cart:carts'))  # 重定向到购物车
                else:
                    order_detail = OrderDetailInfo()
                    order_detail.good = cart.cart_good
                    order_detail.order = order
                    order_detail.price = cart.cart_good.price
                    order_detail.count = cart.nums
                    order_detail.save()

                    total += float(cart.money())
                    cart.delete()
            # 修改总价
            order.total = total + 10
            order.save()
            transaction.savepoint_commit(sid)  # 提交事务
            return redirect(reverse('order:play', args=(order.pk, )))
        except Exception as e:
            print('异常:%s' % e)
            transaction.savepoint_rollback(sid)  # 回滚到起点
            raise e  # 抛出异常,用于调试
            return redirect(reverse('order:user_order'))  # 重定向到订单页面
Example #5
0
def submit_order(request):
    if request.method == 'GET':
        username = request.session.get('username')
        user = User.objects.filter(username=username).first()
        carts = ShoppingCart.objects.filter(user=user, is_select=1).all()
        final_price = 0
        if carts:
            for cart in carts:
                final_price += cart.nums * cart.goods.shop_price
            order = OrderInfo()
            order.user = user
            order.order_mount = final_price
            order.save()
            return HttpResponseRedirect(reverse('goods:index'))
Example #6
0
def user_order_create(request):
    if request.method == 'GET':
        return render(request, 'userOrderCreate.html')
    if request.method == 'POST':
        # 获取用户信息
        user = User.objects.filter(user_id=request.session['user_id']).first()
        # 创建签收者对象,保存信息
        signer_address = SignerAddress()
        signer_address.user = user
        signer_address.province = request.POST.get('province')
        signer_address.city = request.POST.get('city')
        signer_address.district = request.POST.get('district')
        signer_address.address = request.POST.get('address')
        signer_address.signer_name = request.POST.get('signerName')
        signer_address.signer_mobile = request.POST.get('signerMobile')
        signer_address.save()

        # 创建货物对象,保存信息
        goods = GoodsInfo()
        goods.user = user
        goods.goods_id = int(time.time())
        goods.goods_weight = request.POST.get('goodsWeight')
        goods.goods_volume = request.POST.get('goodsVolume')
        goods.goods_value = request.POST.get('goodsValue')
        goods.goods_fast = request.POST.get('goodsFast')
        goods.save()

        # 创建订单对象
        order = OrderInfo()
        order.user = user
        order.signer = signer_address
        order.goods = goods

        order.save()

        return HttpResponseRedirect(reverse('user:user_order_create'))
Example #7
0
def place_order(request):
    if request.method == 'GET':

        # 获取商品信息
        carts = []

        # 商品总数量
        goods_count = 0

        # 获取登录用户
        token = request.COOKIES.get('token')
        user = UserToken.objects.filter(token=token).first().user

        # 用户地址信息
        user_addresses = UserAddress.objects.filter(user=user).all()


        try:
            # 获取token,如果是商品详情点了立即购买
            cart_id = request.GET.get('goods_id')
            cart_num = int(request.GET.get('goods_num'))

            # 从立即购买跳转来的
            if cart_id:
                goods = Goods.objects.get(id=cart_id)
                goods_ = dict(model_to_dict(goods))
                goods_['num'] = cart_num
                goods_['sum'] = cart_num * goods_['shop_price']
                carts.append(goods_)

                goods_count = cart_num

                # 加入购物车
                shop_carts = Cart()
                shop_carts.nums = cart_num
                shop_carts.goods = Goods.objects.filter(id=cart_id).first()
                shop_carts.user = User.objects.filter(username=user.username).first()
                shop_carts.is_select = 1
                shop_carts.save()


        # 如果空的话 按购物车选中商品创建订单
        except:
            cart_is = Cart.objects.filter(is_select=1).all()

            for cart in cart_is:
                goods = Goods.objects.get(id=cart.goods_id)
                goods_ = dict(model_to_dict(goods))
                # 转字典添加数量key
                goods_['num'] = cart.nums
                goods_['sum'] = cart.nums * goods_['shop_price']
                carts.append(goods_)

            goods_count = sum([i.nums for i in cart_is])


        # 商品总价格
        goods_sum = sum([i['num'] * i['shop_price'] for i in carts])

        return render(request, 'web/place_order.html', {'user_addresses': user_addresses, 'carts': carts,
                                                    'goods_count': goods_count, 'goods_sum': goods_sum})



    if request.method == 'POST':
        site_id = int(request.POST.get('site'))
        pay = int(request.POST.get('pay'))

        # 获取登录用户
        token = request.COOKIES.get('token')
        user = UserToken.objects.filter(token=token).first().user

        # 商品信息
        carts = []
        cart_is = Cart.objects.filter(is_select=1).all()

        for cart in cart_is:
            goods = Goods.objects.get(id=cart.goods_id)
            goods_ = dict(model_to_dict(goods))
            # 转字典添加数量key
            goods_['num'] = cart.nums
            carts.append(goods_)

        # 总价格和 +10 邮费   全不包邮
        goods_sum = sum([i['num'] * i['shop_price'] for i in carts]) + 10

        # 地址表
        address = UserAddress.objects.filter(id=site_id).first()

        # 获取订单号 交易号
        order_sn = get_order_sn()
        order = OrderInfo()

        # 保存信息
        order.user = user
        order.order_sn = order_sn
        order.trade_no = get_trade_no()
        order.order_mount = goods_sum
        order.address = address.address
        order.signer_name = address.signer_name
        order.signer_mobile = address.signer_mobile
        order.save()

        # 保存关联的详情表
        for i in cart_is:
            or_goods = OrderGoods()
            or_goods.order = OrderInfo.objects.filter(user=user).first()
            or_goods.order_sn = order_sn
            or_goods.goods = Goods.objects.filter(id=i.goods_id).first()
            or_goods.goods_nums = i.nums
            or_goods.save()

            # 修改商品库存
            i.goods.goods_nums -= i.nums
            i.save()

        # 删除已下单的商品
        cart_is.delete()

        return JsonResponse({'code': 200, 'msg': '请求成功'})