示例#1
0
    def get(self, request, order_id):  #提供评论的页面
        '''提供评论页面'''
        user = request.user

        # 校验数据
        if not order_id:
            return redirect(reverse('user:order'))
        try:
            order = OrderInfo.objects.get(id=order_id, user=user)
        except OrderInfo.DoesNotExist:
            return redirect(reverse('user:order'))

        # 根据订单的状态获取订单订单的状态信息 动态给order添加属性
        order.status_name = OrderInfo.ORDER_STATUS(order.order_status)

        # 获取订单商品的信息
        order_skus = OrderGoods.objects.filter(order_id=order_id)

        for order_sku in order_skus:
            # 计算商品小计
            amount = order_sku.count * order_sku.prcie
            # 动态给order_sku添加属性amount,保存商品小计
            order_sku.amount = amount
        # 动态给order添加属性order_skus,保存订单商品信息
        order.order_skus = order_skus

        # 使用模板
        return render(request, 'order_comment.html', {'order': order})
示例#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'))
示例#3
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/'})
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
示例#5
0
文件: views.py 项目: Deaseyy/Django
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'))
示例#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'))
示例#7
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'))  # 重定向到订单页面
示例#8
0
    def post(self, request):
        #接收参数
        user = request.user
        if not user.is_authenticated:
            return JsonResponse({'ret': 0, 'errmsg': '请先登录'})
        addr_id = request.POST.get('addr_id')
        pay_method = request.POST.get('pay_method')
        sku_ids = request.POST.get('sku_ids')
        #校验参数
        if not all([addr_id, pay_method, sku_ids]):
            return JsonResponse({'ret': 1, 'errmsg': '数据不全'})
        if pay_method not in OrderInfo.PAY_METHOD_CHOICES.keys():
            return JsonResponse({'ret': 2, 'errmsg': '支付方式有误'})
        try:
            addr = Address.objects.get(id=addr_id)
        except Address.DoesNotExist:
            return JsonResponse({'ret': 3, 'errmsg': '地址有误'})

        total_price = 0
        total_count = 0

        transit_price = 10
        #设置保存点
        save_id = transaction.savepoint
        # 生成订单号
        order_id = datetime.now().strftime('%Y%m%d%H%M%S') + str(user.id)
        try:

            order = OrderInfo.create(order=order_id,
                                     user=user,
                                     address=addr,
                                     total_count=total_count,
                                     total_price=total_price,
                                     transit_price=transit_price,
                                     pay_method=pay_method)

            #生成订单商品
            sku_ids = sku_ids.split(',')
            conn = get_redis_connection('default')
            cart_key = 'cart_%d' % user.id

            for sku_id in sku_ids:
                for i in range(3):
                    try:

                        sku = GoodsSKU.objects.get(id=sku_id)
                    except GoodsSKU.DoesNotExist:
                        transaction.savepoint_rollback(save_id)
                        return JsonResponse({'ret': 4, 'errmsg': '商品不存在'})
                    count = conn.hget(cart_key, sku_id)
                    if sku.store < count:
                        transaction.savepoint_rollback(save_id)
                        return JsonResponse({'ret': 5, 'errmsg': '商品库存不足'})
                    # 更新商品销量和库存
                    orgin_store = sku.store
                    new_store = orgin_store - int(count)
                    new_sales = sku.sales + int(count)
                    res = GoodsSKU.objects.filter(
                        id=sku_id, store=new_store).update(store=new_store,
                                                           sales=new_sales)
                    if res == 0:
                        if i == 2:
                            transaction.savepoint_rollback(save_id)
                            return JsonResponse({'ret': 6, 'errmsg': '订单创建失败'})
                        continue
                    goods = OrderGoods.objects.create(order=order,
                                                      sku=sku,
                                                      count=count,
                                                      price=sku.price)

                    #累计计算订单总价格和总数量
                    total_price += int(count) * sku.price
                    total_count += count
                    break
            #更新订单总价和总数量
            order.total_count = total_count
            order.total_price = total_price
            conn.save()
        except Exception as e:
            transaction.savepoint_rollback(save_id)
            return JsonResponse({'ret': 6, 'msg': '订单创建失败'})
        return JsonResponse({'ret': 7, 'msg': '订单创建成功'})
示例#9
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': '请求成功'})