def get(self,request): user_id = request.session.get("ID") # >>>> 1. 收货地址处理 address = UserAddress.objects.filter(user_id=user_id).order_by("-isDefault").first() # 处理商品信息 sku_ids = request.GET.getlist("sku_ids") # 准备空列表 装商品 goods_skus = [] # 准备商品总计 goods_total_price = 0 # 准备redis r = get_redis_connection() # 准备键 cart_key = get_cart_key(user_id) # 遍历 for sku_id in sku_ids: # 商品信息 try: goods_sku = GoodsskuClassModel.objects.get(pk=sku_id) except GoodsskuClassModel.DoesNotExist: # 商品不存在就回到购物车 return redirect("cart:购物车页面") # 获取对应商品的数量 try: count = r.hget(cart_key, sku_id) count = int(count) except: # 商品不存在就回到购物车 return redirect("cart:购物车页面") # 保存到商品对象上 goods_sku.count = count # 装商品 goods_skus.append(goods_sku) # 统计商品总计 goods_total_price += goods_sku.goodsprice * count # 获取运输方式 transports = Transport.objects.filter(is_delete=False).order_by('price') # 渲染数据 context = { 'address': address, 'goods_skus': goods_skus, 'goods_total_price': goods_total_price, 'transports': transports, } return render(request, 'order/tureorder.html', context=context)
def post(self, request): # 1. 接收参数 user_id = request.session.get("ID") sku_id = request.POST.get("sku_id") count = request.POST.get("count") # a.判断为整数 try: sku_id = int(sku_id) count = int(count) except: return JsonResponse(json_msg(1, "参数错误!")) # b.要在数据库中存在商品 try: goods_sku = GoodsSKU.objects.get(pk=sku_id) except GoodsSKU.DoesNotExist: return JsonResponse(json_msg(2, "商品不存在!")) # 判断库存 if goods_sku.stock < count: return JsonResponse(json_msg(3, "库存不足!")) # 2. 操作数据 # 创建连接 r = get_redis_connection() # 处理购物车的 key cart_key = get_cart_key(user_id) # 添加 # 获取购物车中已经存在的数量 加 上 需要添加 与 库存进行比较 old_count = r.hget(cart_key, sku_id) # 二进制 if old_count is None: old_count = 0 else: old_count = int(old_count) if goods_sku.stock < old_count + count: return JsonResponse(json_msg(3, "库存不足!")) # 将商品添加到购物车 # r.hset(cart_key,sku_id,old_count + count) # ctrl + shift + u rs_count = r.hincrby(cart_key, sku_id, count) if rs_count <= 0: # 删除field r.hdel(cart_key, sku_id) # 获取购物车中的总数量 cart_count = get_cart_count(request) # 3. 合成响应 return JsonResponse(json_msg(0, "添加购物车成功", data=cart_count))
def get(self, request): """ 步骤 1. 从redis获取所有的购物车信息(sku_id,count) 2. 根据购物中的sku_id从 商品sku表中获取商品信息 3. 渲染的数据 将购物车中数量和商品信息合成一块儿 """ # 1. 接收参数 user_id = request.session.get("ID") # 2. 操作数据库 r = get_redis_connection() # 准备键 cart_key = get_cart_key(user_id) # 从redis获取所有的购物车信息 cart_datas = r.hgetall(cart_key) # 准备一个空列表,保存多个商品 goods_skus = [] # 遍历字典 for sku_id, count in cart_datas.items(): # 将二进制数据转成整型 sku_id = int(sku_id) count = int(count) # 2. 根据购物中的sku_id从 商品sku表中获取商品信息 try: goods_sku = GoodsSKU.objects.get(pk=sku_id, is_delete=False, is_on_sale=True) except GoodsSKU.DoesNotExist: # 删除redis中过期的数据 r.hdel(cart_key, sku_id) continue if count <= 0: r.hdel(cart_key, sku_id) continue # 3. 将购物车中数量和商品信息合成一块儿(给一个已经存在的对象添加属性) goods_sku.count = count # setattr(goods_sku,'count',count) # 保存商品到商品列表 goods_skus.append(goods_sku) # 渲染数据 context = {'goods_skus': goods_skus} return render(request, 'cart/shopcart.html', context=context)
def post(self,reqeust): """ 保存订单数据 1. 订单基本信息表 和 订单商品表 """ # 1. 接收参数 transport_id = request.POST.get('transport') sku_ids = request.POST.getlist('sku_ids') address_id = request.POST.get('address') # 接收用户的id user_id = request.session.get("ID") user = User.objects.get(pk=user_id) # 验证数据的合法性 try: transport_id = int(transport_id) address_id = int(address_id) sku_ids = [int(i) for i in sku_ids] except: return JsonResponse(json_msg(2, "参数错误")) # 验证收货地址和运输方式存在 try: address = UserAddress.objects.get(pk=address_id) except UserAddress.DoesNotExist: return JsonResponse(json_msg(3, "收货地址不存在!")) try: transport = Transport.objects.get(pk=transport_id) except Transport.DoesNotExist: return JsonResponse(json_msg(4, "运输方式不存在!")) # 2. 操作数据 # >>>1 . 操作订单基本信息表 order_sn = "{}{}{}".format(datetime.now().strftime("%Y%m%d%H%M%S"), user_id, random.randrange(10000, 99999)) address_info = "{}{}{}-{}".format(address.hcity, address.hproper, address.harea, address.brief) order = Order.objects.create( user=user, order_sn=order_sn, transport_price=transport.price, transport=transport.name, username=address.username, phone=address.phone, address=address_info ) # >>>2. 操作订单商品表 # 操作redis r = get_redis_connection() cart_key = get_cart_key(user_id) # 准备个变量保存商品总金额 goods_total_price = 0 for sku_id in sku_ids: # 获取商品对象 try: goods_sku = GoodsskuClassModel.objects.get(pk=sku_id, is_delete=False, is_on_sale=True) except GoodsskuClassModel.DoesNotExist: return JsonResponse(json_msg(5, "商品不存在")) # 获取购物车中商品的数量 # redis 基于内存的存储,有可能数据会丢失 try: count = r.hget(cart_key, sku_id) count = int(count) except: return JsonResponse(json_msg(6, "购物车中数量不存在!")) # 判断库存是否足够 if goods_sku.stock < count: return JsonResponse(json_msg(7, "库存不足!")) # 保存订单商品表 order_goods = OrderGoods.objects.create( order=order, goods_sku=goods_sku, price=goods_sku.price, count=count ) # 添加商品总金额 goods_total_price += goods_sku.price * count # 扣除库存, 销量增加 goods_sku.stock -= count goods_sku.sale_num += count goods_sku.save() # 3. 反过头来操作订单基本信息表 商品总金额 和 订单总金额 # 订单总金额 order_price = goods_total_price + transport.price order.goods_total_price = goods_total_price order.order_price = order_price order.save() # 4. 清空redis中的购物车数据(对应sku_id) r.hdel(cart_key, *sku_ids) # 3. 合成响应 return JsonResponse(json_msg(0, "创建订单成功!", data=order_sn))
def get(self, request): """ 1. 如果没有收货地址显示添加收货地址 如果有收货地址, 显示默认收货地址, 如果没有默认显示第一个 2. 展示商品信息 a. 获取商品sku_ids 如何获取多个参数的值 getlist() get()只能获取一个 b. 根据商品id获取商品信息 并且 获取购物车redis中的数量 """ user_id = request.session.get("ID") # >>>> 1. 收货地址处理 address = SpAddress.objects.filter( user_id=user_id).order_by("-isDefault").first() # address = None # 处理商品信息 sku_ids = request.GET.getlist("sku_ids") # 准备空列表 装商品 goods_skus = [] # 准备商品总计 goods_total_price = 0 # 准备redis r = get_redis_connection() # 准备键 cart_key = get_cart_key(user_id) # 遍历 for sku_id in sku_ids: # 商品信息 try: goods_sku = GoodsSKU.objects.get(pk=sku_id) except GoodsSKU.DoesNotExist: # 商品不存在就回到购物车 return redirect("cart:购物车展示") # 获取对应商品的数量 try: count = r.hget(cart_key, sku_id) count = int(count) except: # 商品不存在就回到购物车 return redirect("cart:购物车展示") # 保存到商品对象上 goods_sku.count = count # 装商品 goods_skus.append(goods_sku) # 统计商品总计 goods_total_price += goods_sku.price * count # 获取运输方式 transports = Transport.objects.filter( is_delete=False).order_by('price') # 渲染数据 context = { 'address': address, 'goods_skus': goods_skus, 'goods_total_price': goods_total_price, 'transports': transports, } return render(request, 'order/tureorder.html', context=context)
def post(self, request): """ 保存订单数据 1. 订单基本信息表 和 订单商品表 """ # 1. 接收参数 transport_id = request.POST.get("transport") address_id = request.POST.get("address") sku_ids = request.POST.getlist("sku_ids") user_id = request.session.get("ID") user = SpUser.objects.get(pk=user_id) # 验证数据合法性 try: transport_id = int(transport_id) address_id = int(address_id) sku_ids = [int(i) for i in sku_ids] except: return JsonResponse(json_msg(2, "数据不合法")) # 验证收货地址和运输方式存在 try: address = UserAddress.objects.get(pk=address_id) except UserAddress.DoesNotExist: return JsonResponse(json_msg(3, "收货地址不存在")) try: transport = Transport.objects.get(pk=transport_id) except Transport.DoesNotExist: return JsonResponse(json_msg(4, "运输方式不存在")) # 2. 操作数据 # 创建保存点 sid = transaction.savepoint() # 操作订单基本信息表 order_sn = "{}{}{}".format(datetime.now().strftime("%Y%m%d%H%M%S"), user_id, random.randrange(10000, 99999)) address_info = "{}{}{}-{}".format(address.hcity, address.hproper, address.harea, address.brief) try: order = Order.objects.create( user=user, order_sn=order_sn, transport_price=transport.price, transport=transport.name, username=address.username, phone=address.phone, address=address_info, ) except: return JsonResponse(json_msg(8, "创建订单基本数据失败")) # 操作订单商品表 # 操作redis r = get_redis_connection() cart_key = get_cart_key(user_id) goods_total_price = 0 for sku_id in sku_ids: try: goods_sku = GoodsSKU.objects.select_for_update().get( pk=sku_id, is_delete=False, is_on_sale=True) except GoodsSKU.DoesNotExist: # 回滚数据 transaction.savepoint_rollback(sid) return JsonResponse(json_msg(5, "商品不存在")) # 获取购物车中商品的数量 # redis 是基于内存的存储,有可能数据会丢失 try: count = r.hget(cart_key, sku_id) count = int(count) except: transaction.savepoint_rollback(sid) return JsonResponse(json_msg(6, "购物车数量不存在")) # 判断库存 是否足够 if goods_sku.stock < count: transaction.savepoint_rollback(sid) return JsonResponse(json_msg(7, "库存不足")) order_goods = OrderGoods.objects.create( order=order, goods_sku=goods_sku, price=goods_sku.price, count=count, ) goods_total_price += count * goods_sku.price # 扣除库存 销量增加 # GoodsSKU.objects.filter(pk=sku_id,stock=goods_sku.stock).update(stock=goods_sku.stock-count) goods_sku.stock -= count goods_sku.sale_num += count goods_sku.save() # 再次操作订单基本信息表 商品总金额 订单总金额 try: order_total_price = goods_total_price + transport.price order.goods_total_price = goods_total_price order.order_total_price = order_total_price order.save() except: # 回滚 transaction.savepoint_rollback(sid) return JsonResponse(json_msg(9, "更新订单失败")) # 清空redis中的购物车数据(对应sku_id的) r.hdel(cart_key, *sku_ids) # 下单成功,提交事务 transaction.savepoint_commit(sid) # 3. 合成响应 return JsonResponse(json_msg(0, "创建订单成功", data=order_sn))