コード例 #1
0
def cart(request):
    """ 我的购物车 """
    user = request.user
    # 购物车中的商品列表
    prod_list = user.carts.filter(status=constants.ORDER_STATUS_INIT)
    if request.method == 'POST':
        # 提交订单
        # 1.保存用户的地址快照
        default_addr = user.default_addr
        if not default_addr:
            # 消息通知
            messages.warning(request, '请选择地址信息')
            return redirect('accounts:address_list')
        # 计算订单总额
        cart_total = prod_list.aggregate(sum_amount=Sum('amount'),
                                         sum_count=Sum('count'))
        order = Order.objects.create(user=user,
                                     sn=tools.gen_trans_id(),
                                     buy_amount=cart_total['sum_amount'],
                                     buy_count=cart_total['sum_count'],
                                     to_user=default_addr.username,
                                     to_area=default_addr.get_region_format(),
                                     to_address=default_addr.address,
                                     to_phone=default_addr.phone)
        # 2.修改购物车的商品状态 已提交
        # 3.生成订单,关联到购物车
        prod_list.update(status=constants.ORDER_STATUS_SUBMIT, order=order)
        # 4.跳转到订单详情
        messages.success(request, '下单成功,请支付')
        return redirect('mine:order_detail', sn=order.sn)

    return render(request, 'cart.html', {'prod_list': prod_list})
コード例 #2
0
ファイル: views.py プロジェクト: 1182636308/mall
def cart(request):
    # 我的购物车
    user = request.user
    cart_list = user.cart.filter(status=constant.ORDER_STATUS_INIT)
    # 总金额
    sum_amunt = cart_list.aggregate(sum_amunt=Sum('amount'))

    if request.method == 'POST':
        # 保存用户地址
        # 修改购物车状态,生成订单
        default_addr = user.default_addr
        if not default_addr:
            messages.warning(request, '请添加地址信息')
            return redirect('accounts:user_address_list')
        # 计算商品总数量,与总价
        cart_total = cart_list.aggregate(sum_count=Sum('count'),
                                         sum_amount=Sum('amount'))
        order = Order.objects.create(sn=gen_trans_id(),
                                     user=user,
                                     buy_count=cart_total['sum_count'],
                                     buy_amount=cart_total['sum_amount'],
                                     to_user=default_addr.obj,
                                     to_area=default_addr.get_regin_format(),
                                     to_address=default_addr.address,
                                     to_phone=default_addr.phone)
        cart_list.update(status=constant.ORDER_STATUS_SUMBIT, order=order)
        # 跳转到订单详情,并消息通知
        messages.success(request, '下单成功,请支付')
        return redirect('mine:order_detail', order.sn)
    return render(request, 'shopcart.html', {
        'cart_list': cart_list,
        'sum_amunt': sum_amunt['sum_amunt']
    })
コード例 #3
0
 def save(self, user, commit=False):
     obj = super().save(commit=commit)
     obj.user = user
     # 生成订单号
     obj.sn = tools.gen_trans_id()
     # 计算价格
     buy_count = self.cleaned_data['buy_count']
     buy_amount = self.ticket.sell_price * buy_count
     obj.buy_amount = buy_amount
     obj.save()
     # 扣减库存
     self.ticket.remain_stock = F('remain_stock') - buy_count
     self.ticket.save()
     # 关联订单明细,保存快照
     ctype = ContentType.objects.get_for_model(Ticket)
     OrderItem.objects.create(
         user=user,
         order=obj,
         flash_name=self.ticket.name,
         flash_img=self.ticket.sight.main_img,
         flash_price=self.ticket.sell_price,
         flash_origin_price=self.ticket.price,
         flash_discount=self.ticket.discount,
         count=buy_count,
         amount=buy_amount,
         content_type=ctype,
         object_id=self.ticket.id,
         remark='出行时间:{}'.format(self.cleaned_data['play_date'])
     )
     return obj
コード例 #4
0
def cart(request):
    """ my shopping cart """
    user = request.user
    # List of items in my shopping cart
    prod_list = user.carts.filter(status=constants.ORDER_STATUS_INIT)
    # Aggregate query
    if request.method == 'POST':
        # Submit Order
        # 1.Save a snapshot of the user's address
        default_addr = user.default_addr
        if not default_addr:
            # notification
            messages.warning(request, 'Please select address information')
            return redirect('accounts:address_list')
        # Order total calculation
        cart_total = prod_list.aggregate(sum_amount=Sum('amount'),
                                         sum_count=Sum('count'))
        order = Order.objects.create(user=user,
                                     sn=tools.gen_trans_id(),
                                     buy_amount=cart_total['sum_amount'],
                                     buy_count=cart_total['sum_count'],
                                     to_user=default_addr.username,
                                     to_area=default_addr.get_region_format(),
                                     to_address=default_addr.address,
                                     to_phone=default_addr.phone)
        # 2.Modify the status in the cart
        # 3.Generate order, connect to shopping cart
        prod_list.update(status=constants.ORDER_STATUS_SUBMIT, order=order)
        # 4.Jump to order details
        messages.success(request, 'The order is successful, please pay')
        return redirect('mine:order_detail', order.sn)

    return render(request, 'cart.html', {'prod_list': prod_list})
コード例 #5
0
ファイル: views.py プロジェクト: gzy-hub/django-py
def cart(request):
    """我的购物车"""
    user = request.user
    # 我购物车中的商品列表,user.carts是Cart模型中的user的related_name为carts
    prod_list = user.carts.filter(status=constants.ORDER_STATUS_INIT)
    if request.method == 'POST':
        # 提交订单
        # 1.保存用户的地址快照
        default_addr = user.default_addr
        if not default_addr:
            # 消息通知
            messages.warning(request, '请选择地址信息')
            # 如果没有默认地址,跳转到另外一个页面,
            return redirect('accounts:address_list')
        # 订单总额计算
        # 聚合查询
        cart_total = prod_list.aggregate(sum_amount=Sum('amount'),
                                         sum_count=Sum('count'))
        # 创建一个订单
        order = Order.objects.create(user=user,
                                     sn=tools.gen_trans_id(),
                                     buy_amount=cart_total['sum_amount'],
                                     buy_count=cart_total['sum_count'],
                                     to_user=default_addr.username,
                                     to_area=default_addr.get_regin_format(),
                                     to_address=default_addr.address,
                                     to_phone=default_addr.phone)
        prod_list.update(
            # 2.修改购物车中的状态  已提交
            status=constants.ORDER_STATUS_SUBMIT,
            # 3.生成订单,关联到购物车
            # 在mine模块下的购物车模型是关联到订单的
            order=order)
        # 4.跳转到订单详情
        messages.success(request, '下单成功,请支付')
        return redirect('mine:order_detail', order.sn)

    return render(request, 'cart.html', {'prod_list': prod_list})
コード例 #6
0
ファイル: views.py プロジェクト: chydream/djangoDemo
def cart(request):
    user = request.user
    prod_list = user.carts.filter(status=constants.ORDER_STATUS_INIT)
    if request.method == 'POST':
        default_addr = user.default_addr
        if not default_addr:
            messages.warning(request, '请选择地址信息')
            return redirect('accounts:address_list')
        cart_total = prod_list.aggregate(sum_amount=Sum('amount'),
                                         sum_count=Sum('count'))
        order = Order.objects.create(
            user=user,
            sn=tools.gen_trans_id(),
            buy_amount=cart_total['sum_amount'],
            buy_count=cart_total['sum_count'],
            to_user=default_addr.username,
            to_area=default_addr.get_region_format(),
            to_address=default_addr.address,
            to_phone=default_addr.phone,
        )
        prod_list.update(status=constants.ORDER_STATUS_SUBMIT, order=order)
        messages.success(request, '下单成功,请支付!')
        return redirect('mine:order_detail', order.sn)
    return render(request, 'cart.html', {'prod_list': prod_list})