def get(self, request): user = request.user redis_conn = get_redis_connection('cart') redis_cart = redis_conn.hgetall('cart_%s' % user.id) print('redis_cart:%s' % redis_cart) redis_selected = redis_conn.smembers('cart_selected_%s' % user.id) print('redis_selected: %s' % redis_selected) cart = {} for sku_id in redis_selected: cart[int(sku_id)] = int(redis_cart[sku_id]) print('cart: %s' % cart) skus = SKU.objects.filter(id__in=cart.keys()) print('skus:%s' % skus) for sku in skus: sku.count = cart[sku.id] freight = Decimal('10.00') serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': skus }) return Response(serializer.data)
def get(self, request): """ 获取 """ user = request.user # 从购物车中获取用户勾选要结算的商品信息 redis_conn = get_redis_connection('cart') redis_cart = redis_conn.hgetall('cart_%s' % user.id) cart_selected = redis_conn.smembers('cart_selected_%s' % user.id) cart = {} for sku_id in cart_selected: cart[int(sku_id)] = int(redis_cart[sku_id]) # 查询商品信息 skus = SKU.objects.filter(id__in=cart.keys()) for sku in skus: sku.count = cart[sku.id] # 运费 freight = Decimal('10.00') serializer = OrderSettlementSerializer({'freight': freight, 'skus': skus}) print(serializer.data) return Response(serializer.data)
def get(self, request): # 获取用户对象 user user = request.user # 从redis中查询购物车 sku_id count selected # 1.创建redis连接对象 redis_conn = get_redis_connection('cart') # 2. hash 商品数量 redis_cart_dict = redis_conn.hgetall('cart_%s' % user.id) # 3.set 勾选商品 redis_cart_selected = redis_conn.smembers('cart_selected_%s' % user.id) cart = {} for sku_id in redis_cart_selected: cart[int(sku_id)] = int(redis_cart_dict[sku_id]) # 查询数据库 sku_id_list = cart.keys() sku_obj_list = SKU.objects.filter(id__in=sku_id_list) for sku in sku_obj_list: sku.count = cart[sku.id] # 运费 freight = Decimal('10.00') # 序列化返回 # serializer = CartSKUSerializer(sku_obj_list, many=True) # return Response({'freight': freight, 'skus': serializer.data}) # 利用序列化器嵌套完成返回 serializer = OrderSettlementSerializer({'freight': freight, 'skus': sku_obj_list}) return Response(serializer.data)
def get(self, request): """获取订单""" user = request.user # 从订单中获取用户勾选的商品 redis_conn = get_redis_connection("cart") redis_cart = redis_conn.hgetall("cart_%s" % user.id) cart_selected = redis_conn.smembers("cart_selected_%s" % user.id) # 定义一个字典.用户勾选的商品 cart = {} for sku_id in cart_selected: cart[int(sku_id)] = int(redis_cart[sku_id]) # 查询商品信息 skus = SKU.objects.filter(id__in=cart.keys()) for sku in skus: sku.count = cart[sku.id] # 运费 freight = Decimal("10.00") serializer = OrderSettlementSerializer({ "freight": freight, "skus": skus }) return Response(serializer.data)
def get(self, request): # 链接redis conn = get_redis_connection('carts') user = request.user # 获取数据 sku_id_count_dict = conn.hgetall('cart_%s' % user.id) sku_selected_list = conn.smembers('cart_selected_%s' % user.id) # 构建数据 cart = {} for sku_id in sku_selected_list: cart[int(sku_id)] = int(sku_id_count_dict[sku_id]) # 查询出对应的对象 sku_list = SKU.objects.filter(id__in=cart.keys()) # 添加运费属性 freight = Decimal('10.00') for sku in sku_list: sku.count = cart[sku.id] # 进行序列化 serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': sku_list }) # 返回结果 return Response(serializer.data)
def get(self, request): # 获取 user = request.user # 从购物车中获取用户勾选的要结算的商品信息 redis_conn = get_redis_connection('cart') redis_cart = redis_conn.hgetall('cart_%s' % user.id) # 返回一个列表,包含所有的域和值 # print('redis_cart', redis_cart) # list {b'10': b'1', b'15': b'1'} list?? sku_id count cart_selected = redis_conn.smembers('cart_selected_%s' % user.id ) # 集合中的所有成员。 # print('redis_selected', cart_selected) # {b'10', b'15'} # 勾选的商品sku_id 保存到变量cart cart = {} for sku_id in cart_selected: cart[int(sku_id)] = int(redis_cart[sku_id]) # 查询商品信息 skus = SKU.objects.filter(id__in=cart.keys()) # print(skus) # 商品信息 for sku in skus: sku.count = cart[sku.id] # 运费 freight = Decimal('10.00') serializer = OrderSettlementSerializer({'freight':freight, 'skus':skus}) return Response(serializer.data)
def get(self, request): # 获取用户对象 user user = request.user # 从redis中查询购物车 sku_id count selected redis_conn = get_redis_connection('cart') # hash 商品数量 sku_id count redis_cart_dict = redis_conn.hgetall('cart_%s' % user.id) # set 勾选商品 redis_cart_selected = redis_conn.smembers('cart_selected_%s' % user.id) cart = {} # cart = { # 勾选商品信息 # sku_id: count # } for sku_id in redis_cart_selected: cart[int(sku_id)] = int(redis_cart_dict[sku_id]) sku_id_list = cart.keys() sku_object_list = SKU.objects.filter(id__in=sku_id_list) for sku in sku_object_list: sku.count = cart[sku.id] freight = Decimal('10.00') serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': sku_object_list }) return Response(serializer.data)
def get(self, request): redis_conn = get_redis_connection("cart") # redis_cart = { # 商品的sku_id(bytes类型): count数量(bytes类型) # 1: 1 # 16: 2 # ... # } redis_cart_dict = redis_conn.hgetall( "cart_%s" % request.user.id) # 购物车中所有商品的sku_id,字典 # set(勾选的商品sku_id(bytes类型), 1, 3, 16,....) redis_cart_selected = redis_conn.smembers( "cart_selected_%s" % request.user.id) # 购物车中被选中商品的sku_id,集合 cart = {} # {sku_id: count} for sku_id in redis_cart_selected: # 把某个商品的数量和cart字典的id组成键值对 cart[int(sku_id)] = int(redis_cart_dict[sku_id]) redis_id_list = cart.keys() sku_obj_list = SKU.objects.filter(id__in=redis_id_list) for sku in sku_obj_list: sku.count = cart[sku.id] # 把商品的数量添加到sku的属性上 freight = Decimal("10.00") # 运费 serializer = OrderSettlementSerializer({ "freight": freight, "skus": sku_obj_list }) return Response(serializer.data)
def get(self, request): user = request.user #redis中获取购物车信息 redis_conn = get_redis_connection('cart') # 获取当前用户购物车信息 redis_carts = redis_conn.hgetall('cart_%s' % user.id) carts_selected = redis_conn.smembers('cart_selected_%s' % user.id) # 获取选中的商品信息 # 方式一 # order_cart={} # for sku_id,count in redis_carts.items(): # if sku_id in carts_selected: # order_cart[int(sku_id)]=int(count) # skus=SKU.objects.filter(id__in=[int(skuid) for skuid in carts_selected]) # for sku in skus: # sku.count=order_cart[sku.id] # 方式二 order_cart = {} for sku_id in carts_selected: order_cart[int(sku_id)] = int(redis_carts[sku_id]) skus = SKU.objects.filter(id__in=order_cart.keys()) for sku in skus: sku.count = order_cart[sku.id] freight = Decimal('10.00') serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': skus }) return Response(serializer.data)
def get(self, request): """订单生成get请求""" user = request.user # 1. 前端需要提供: PayMethod, 收货地址等数据 cur = get_redis_connection("cart") cart_redis = cur.hgetall("cart_%s" % user.id) cart_selected = cur.smembers("cart_selected_%s" % user.id) # 2. 获取出选中的redis中所需的商品和数量 cart = { int(sku_id): int(cart_redis[sku_id]) for sku_id in cart_selected } # 3. 查询出商品信息: skus = SKU.objects.filter(id__in=cart.keys()) # 4. 向sku中添加count准备序列化 for sku in skus: setattr(sku, "count", cart[sku.id]) # 5. 获取运费数据: freight = Decimal("10.00") # 6. 序列化数据: serializer = OrderSettlementSerializer({ "freight": freight, "skus": skus }) return Response(serializer.data)
def get(self, request): """ 获取 """ user = request.user # 从购物车中获取用户勾选要结算的商品信息 redis_conn = get_redis_connection('cart') redis_cart = redis_conn.hgetall('cart_%s' % user.id) cart_selected = redis_conn.smembers('cart_selected_%s' % user.id) cart = {} for sku_id in cart_selected: # 每一个勾选的商品id作为key, 勾选商品的id在查询出数量count作为value 构建新的字典 cart[int(sku_id)] = int(redis_cart[sku_id]) # 查询商品信息 skus = SKU.objects.filter(id__in=cart.keys()) for sku in skus: # 利用模型类来序列化输出,只要把count重新赋值,并且每一个商品在sku_id中的都是勾选 sku.count = cart[sku.id] sku.selected = True # 运费 freight = Decimal('10.00') serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': skus }) return Response(serializer.data)
def get(self, request): """ 获取 """ user = request.user # 从购物车中获取用户勾选要结算的商品 redis_con = get_redis_connection("cart") redis_cart = redis_con.hgetall("cart_%s" % user.id) redis_selectetd = redis_con.smembers("cart_selected_%s" % user.id) cart = {} # 遍历勾选列表 被勾选的商品加入到cart里 for sku_id in redis_selectetd: cart[int(sku_id)] = int(redis_cart[sku_id]) # 查询数据库商品信息 skus = SKU.objects.filter(id__in=cart.keys()) for sku in skus: sku.count = cart[sku.id] # 运费 freight = Decimal("10.00") seriliazer = OrderSettlementSerializer({'freight': freight, 'skus': skus}) return Response(seriliazer.data)
def get(self, request, id): try: dinnertable = DinnerTable.objects.get(tableKey=id) except DinnerTable.DoesNotExist: return self.response_data(code=RET.NODATA, msg="本餐桌不存在") # 从购物车中获取用户勾选要结算的商品信息 redis_conn = get_redis_connection('carts') # 获取hash数据 redis_cart = redis_conn.hgetall('carts_%s' % dinnertable.tableKey) # 获取set数据{b'1', b'2'} cart_selected = redis_conn.smembers('selected_%s' % dinnertable.tableKey) cart = {} for food_id in cart_selected: cart[int(food_id)] = int(redis_cart[food_id]) # 查询商品信息 foods = Foods.objects.filter(id__in=cart.keys()) for food in foods: food.count = cart[food.id] print(foods) # 运费 # freight = Decimal('10.00') serializer = OrderSettlementSerializer(foods, many=True) return self.response_data(code=RET.OK, msg="成功", data=serializer.data, pagination=False)
def get(self, request): """ 订单结算 GET /orders/settlement/ :param request: request.user当前用户 :return: { "freight":"运费", "skus":[ 结算的商品列表 { "id": 商品id, "name": 商品名称, "default_image_url": 商品默认图片路径, "price": 商品单价, "count": 商品数量 }, ...... ] } """ # 获取登陆的用户数据 user = request.user # 获取购物车中被勾选的要结算的商品信息 redis_conn = get_redis_connection('cart') # redis_cart_id = redis_conn.hkeys('cart_%s' % user.id) # 拿到当前用户所有购物车商品息 redis_cart_selected = redis_conn.smembers('cart_selected_%s' % user.id) redis_cart_selected = [int(sku_id) for sku_id in redis_cart_selected ] # 拿到被勾选的商品sku_id # 拿到商品对应数量 cart_count = {} """ { sku_id1 : count1, sku_id2 : count2, ... } """ for sku_id in redis_cart_selected: cart_count[sku_id] = int( redis_conn.hget('cart_%s' % user.id, sku_id)) # 查询商品,添加数量属性 skus = SKU.objects.filter(pk__in=redis_cart_selected) for sku in skus: sku.count = cart_count[sku.id] # 运费 freight = Decimal('10.00') # 定义序列化数据格式 context = {'freight': freight, 'skus': skus} serializer = OrderSettlementSerializer(context) return Response(serializer.data)
def get(self, request): """ 获取订单结算的数据: 1. 从redis获取用户购物车中被勾选的商品id和对应数量count 2. 根据商品的id获取对应商品的数据 & 订单运费 3. 将订单结算数据序列化并返回 """ # 获取登录user user = request.user # 1. 从redis获取用户购物车中被勾选的商品id和对应数量count redis_conn = get_redis_connection('cart') # 从redis set中获取用户购物车被勾选的商品的id cart_selected_key = 'cart_selected_%s' % user.id # (b'<sku_id>', b'<sku_id>', ...) sku_ids = redis_conn.smembers(cart_selected_key) # 从redis hash中获取用户购物车中所有商品的id和对应数量count cart_key = 'cart_%s' % user.id # { # b'<sku_id>': b'<count>', # ... # } cart_dict = redis_conn.hgetall(cart_key) # 组织数据 # { # '<sku_id>': '<count>', # ... # } cart = {} for sku_id, count in cart_dict.items(): cart[int(sku_id)] = int(count) # 2. 根据商品的id获取对应商品的数据 & 订单运费 skus = SKU.objects.filter(id__in=sku_ids) for sku in skus: # 给sku对象增加属性count,保存该商品所要结算的数量 sku.count = cart[sku.id] # 组织运费 freight = Decimal(10) # 3. 将订单结算数据序列化并返回 res_dict = {'freight': freight, 'skus': skus} serializer = OrderSettlementSerializer(res_dict) return Response(serializer.data)
def get(self, request): """ 订单结算商品信息获取: 1. 从redis中获取用户要结算的商品sku_id和数量count 2. 根据商品sku_id获取对应商品的信息,组织运费 3. 将数据序列化并返回 """ user = request.user # 1. 从redis中获取用户要结算的商品sku_id和数量count redis_conn = get_redis_connection('cart') # 从redis购物车记录中获取勾选商品sku_id set cart_selected_key = 'cart_selected_%s' % user.id # (b'<sku_id>, b'<sku_id>', ...) sku_ids = redis_conn.smembers(cart_selected_key) # 从redis购物车记录中获取用户购物车中商品的sku_id和对应数量count hash cart_key = 'cart_%s' % user.id # { # b'<sku_id>': b'<count>', # ... # } cart_dict = redis_conn.hgetall(cart_key) # 组织数据 # { # '<sku_id>': '<count>', # ... # } cart = {} for sku_id, count in cart_dict.items(): cart[int(sku_id)] = int(count) # 2. 根据商品sku_id获取对应商品的信息,组织运费 skus = SKU.objects.filter(id__in=sku_ids) for sku in skus: # 给sku对象增加属性count,保存要结算商品数量count sku.count = cart[sku.id] # 运费 freight = Decimal(10) # 3. 将数据序列化并返回 resp = {'freight': freight, 'skus': skus} serializer = OrderSettlementSerializer(resp) return Response(serializer.data)
def get(self, request): """ 获取 """ user = request.user # data = {} # 查询redis数据 # redis_conn = get_redis_connection('cart') # type: redis.StrictRedis # select_list = redis_conn.smembers('cart_selected_%s' % user.id) # sku_count_list = redis_conn.hgetall('cart_%s' % user.id) # 只展示勾选状态的商品 # sku_list = [] # # for select_id in select_list: # count = sku_count_list[select_id] # sku = SKU.objects.get(id=int(select_id)) # sku.count = count # sku_list.append(sku) # 从购物车中获取用户勾选要结算的商品信息 redis_conn = get_redis_connection('cart') redis_cart = redis_conn.hgetall('cart_%s' % user.id) cart_selected = redis_conn.smembers('cart_selected_%s' % user.id) cart = {} # tips--构造一个 { sku_id: count } 的数据格式 for sku_id in cart_selected: cart[int(sku_id)] = int(redis_cart[sku_id]) # 查询商品信息, 并添加额外的字段 # note--python中字典和类的实例对象是不一样的! # note--类的实例对象是可以通过点操作熟悉的, 也是可变类型 skus = SKU.objects.filter(id__in=cart.keys()) for sku in skus: sku.count = cart[sku.id] # 运费, Decimal运算速度慢, 但是精度高 freight = Decimal('10.00') # 直接只序列化商品也可以 # 构造一个对象即可, 单独序列化skus serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': skus }) return Response(serializer.data)
def get(self, request): """获取商品信息""" # 获取当前用户 user = request.user # 链接数据库 redis_conn = get_redis_connection('cart') # 拼接键 cart_key = 'cart_%s' % user.id # 从数据库中获取商品id和数量 cart_count = redis_conn.hgetall(cart_key) # 拼接键 cart_selected_key = 'cart_selected_%s' % user.id # 从数据库中获取已经选中的商品 cart_selected = redis_conn.smembers(cart_selected_key) cart = {} for sku_id in cart_selected: cart[int(sku_id)] = int(cart_count[sku_id]) # 查询已勾选的商品的数据 skus = SKU.objects.filter(id__in=cart.keys()) for sku in skus: # 给商品模型对象添加属性count。 sku.count = cart[sku.id] # 运费 freight = Decimal('10') # 将数据进行序列化 serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': skus }) # 返回序列化之后的数据 return Response(serializer.data)
def get(self, request): user = request.user redis_conn = get_redis_connection('cart') # {sku_id:count,...} # [sku_id,...] redis_cart_dict = redis_conn.hgetall('cart_%s' % user.id) redis_cart_set = redis_conn.smembers('cart_selected_%s' % user.id) # 只需要获取已勾选的sku的数据 cart = {} for sku_id in redis_cart_set: cart[int(sku_id)] = int(redis_cart_dict[sku_id]) # 查询已勾选商品的具体信息 sku_query_set = SKU.objects.filter(id__in=redis_cart_set) for sku_obj in sku_query_set: sku_obj.count = cart[sku_obj.id] # 运费 freight = Decimal('10.00') data_dict = {'freight': freight, 'skus': sku_query_set} serializer = OrderSettlementSerializer(data_dict) return Response(serializer.data)
def get(self, request): """获取""" user = request.user # 从购物车中获取用户勾选要结算的商品信息 redis_conn = get_redis_connection('carts') sku_ids = redis_conn.smembers('cart_selected_%s' % user.id) cart_dict = redis_conn.hgetall('cart_%s' % user.id) # 组织数据 cart = {} for sku_id, count in cart_dict.items(): cart[int(sku_id)] = int(count) # 2. 根据商品id获取对应商品的信息, 组织运费 skus = SKU.objects.filter(id__in=sku_ids) for sku in skus: # 给sku对象增加属性count, 保存用户索要结算的该商品的数量count sku.count = cart[sku.id] # 组织运费 freight = Decimal(10) # # serializer = OrderSKUSerializer(skus, many=True) # resp_data = { # 'freight': freight, # 'skus': serializer.data # } # return Response(resp_data) # 组织数据 res_dict = { 'freight': freight, 'skus': skus } serializer = OrderSettlementSerializer(res_dict) return Response(serializer.data)
def get(self, request): """获取""" # 获取用户对象 user = request.user # 查询redis redis_conn = get_redis_connection('cart') # 获取 hash 数据 redis_cart = redis_conn.hgetall('cart_%s' % user.id) # 获取 set 数据 redis_selected_cart = redis_conn.smembers('cart_selected_%s' % user.id) cart = {} # 进行遍历 for sku_id in redis_selected_cart: cart[int(sku_id)] = int(redis_cart[sku_id]) # 查询商品信息(mysql数据库) sku_id_list = cart.keys() sku_obj_list = SKU.objects.filter(id__in=sku_id_list) # 给商品添加属性 for sku in sku_obj_list: sku.count = cart[sku.id] sku.selected = True # 运费 freight = Decimal('10.00') # 序列化返回 # serializer = CartSKUSerializer(sku_obj_list, many=True) # return Response({'freight': freight, 'skus': serializer.data}) serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': sku_obj_list }) return Response(serializer.data)
def get(self, request): """获取购物车信息""" user = request.user # 从购物车中获取用户勾选要结算的商品信息 # 创建redis连接对象 redis_conn = get_redis_connection('cart') # 获取hash值 cart_dict = redis_conn.hgetall('cart_%d' % user.id) # 获取是否勾选的值 cart_selected = redis_conn.smembers('selected_%s' % user.id) cart = {} # 遍历选中的商品 for sku_id in cart_selected: cart[int(sku_id)] = int(cart_dict[sku_id]) # 获取商品信息 skus = SKU.objects.filter(id__in=cart.keys()) # 对商品信息进行遍历 for sku in skus: # sku_id 是商品id,通过sku.id 在cart 中找到所对应的count sku.count = cart[sku.id] # 运费 freight = Decimal('10.00') # data = {'freight': freight, 'skus': skus} # serializer = OrderSettlementSerializer(data) # 创建序列化器时 给instance参数可以传递(模型/查询集(many=True) /字典) serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': skus }) return Response(serializer.data)
def get(self, request): """ 获取 """ # 从购物车中获取用户勾选要结算的商品信息 # redis string {cart_user_id : {sku_id1:[ count, seleted ],sku_id2:[ count, seleted ] } } cart = request.cart # 查询商品信息 sku_id_list = [] for sku_id in cart.keys(): if cart[sku_id][1] == True: sku_id_list.append(int(sku_id)) skus = SKU.objects.filter(id__in=sku_id_list) for sku in skus: sku.count = cart[str(sku.id)][0] # 运费 freight = Decimal('10.00') serializer = OrderSettlementSerializer({ 'freight': freight, 'skus': skus }) return Response(serializer.data)