示例#1
0
    def get(self, request):
        """点击用户中心展示个人的历史浏览"""
        """
        获取用户id 查询redis 用户历史浏览商品
        获取商品信息 返回
        """
        #获取登录用户信息
        user_id = request.user.id
        #连接redis
        conn = get_redis_connection('history')
        #查询
        sku_id = conn.lrange('history_%s' % user_id, 0, -1)
        print(sku_id)

        sku_list = []
        #获取商品信息
        for id in sku_id:
            sku = SKU.objects.get(id=id)
            sku_list.append(sku)

        #序列化商品信息
        ser = SKUSerializer(instance=sku_list, many=True)

        data = ser.data

        return Response(data)
示例#2
0
文件: views.py 项目: lmcxy/md_mall
    def get(self, request):
        """查询用户浏览记录
        1.获取用户id
        2.从redis获取浏览过的商品id
        3.根据商品id到数据库中查询对应商品
        4.序列化返回

        """

        # 获取用户user_id
        user_id = request.user.id

        # 获取redis对象
        redis_conn = get_redis_connection('history')

        # 查询出用户存储的浏览记录
        sku_ids = redis_conn.lrange('history_%s' % user_id, 0, -1)

        # 查询sku列表数据
        sku_list = []
        for sku_id in sku_ids:
            sku = SKU.objects.get(id=sku_id)
            sku_list.append(sku)

        # 调用序列化器返回参数
        serializer = SKUSerializer(sku_list, many=True)
        return Response(serializer.data)
示例#3
0
    def get(self, request):
        """
        用户浏览记录获取:
        1. 从redis中获取登录用户浏览的商品sku_id
        2. 根据商品的id获取对应商品的数据
        3. 将商品的数据进行序列化并返回
        """
        # 获取登录user
        user = request.user

        # 1. 从redis中获取登录用户浏览的商品sku_id
        redis_conn = get_redis_connection('history')

        history_key = 'history_%s' % user.id

        # lrange(key, start, end): 获取redis列表指定区间内的元素
        # [b'<sku_id>', b'<sku_id>', ...]
        sku_ids = redis_conn.lrange(history_key, 0, constants.USER_BROWSING_HISTORY_COUNTS_LIMIT - 1)

        # 2. 根据商品的id获取对应商品的数据
        skus = []
        for sku_id in sku_ids:
            # 根据商品id获取商品信息
            sku = SKU.objects.get(id=sku_id)
            skus.append(sku)

        # 3. 将商品的数据进行序列化并返回
        serializer = SKUSerializer(skus, many=True)
        return Response(serializer.data)
示例#4
0
文件: views.py 项目: sentrywang/root
    def get(self, request):
        '''
        获取历史浏览记录
        :param request:
        :return:
        '''
        # 获取当前用户的id
        user_id = request.user.id

        # 获取redis数据
        conn = get_redis_connection('history')
        # 取出redis数据
        history = conn.lrange('history_%s' % user_id, 0, -1)
        skus = []
        # 为了保持查询出的顺序与用户的浏览历史保存顺序一致
        for sku_id in history:
            # sku_id 是byte字节类型
            # byte 是由redis 和python 链接的中间插件来转化的
            # id 是integer
            sku = SKU.objects.get(id=sku_id.decode())
            skus.append(sku)

        s = SKUSerializer(skus, many=True)

        # 返回序列化后的数据
        return Response(s.data)
示例#5
0
    def get(self, request):
        """
        获取用户浏览记录:
        1. 从redis中获取用户浏览的商品的id
        2. 根据商品id获取对应商品的信息
        3. 将商品的信息序列化并返回
        """
        # 获取登录user
        user = request.user

        # 1. 从redis中获取用户浏览的商品的id
        redis_conn = get_redis_connection('history')
        history_key = 'history_%s' % user.id

        # lrange(key, start, end): 获取redis列表指定区间内的元素
        # [b'<sku_id>', b'<sku_id>', ...]
        sku_ids = redis_conn.lrange(history_key, 0, 4)

        # 2. 根据商品id获取对应商品的信息
        skus = []
        for sku_id in sku_ids:
            sku = SKU.objects.get(id=sku_id)
            skus.append(sku)

        # 3. 将商品的信息序列化并返回
        serializer = SKUSerializer(skus, many=True)
        return Response(serializer.data)
示例#6
0
    def get(self, request):
        """
        登录用户浏览记录获取:
        1. 从redis中获取用户的浏览的商品sku_id
        2. 根据sku_id获取对应商品的数据
        3. 将商品数据序列化并返回
        """
        # 获取登录用户
        user = request.user

        # 1. 从redis中获取用户的浏览的商品sku_id
        redis_conn = get_redis_connection('histories')
        history_key = 'history_%s' % user.id

        # [b'<sku_id>', b'<sku_id>', ...]
        sku_ids = redis_conn.lrange(history_key, 0, -1)

        # 2. 根据sku_id获取对应商品的数据
        skus = []

        for sku_id in sku_ids:
            sku = SKU.objects.get(id=sku_id)
            skus.append(sku)

        # 3. 将商品数据序列化并返回
        serializer = SKUSerializer(skus, many=True)
        return Response(serializer.data)
示例#7
0
    def get(self, request):
        '''查询用户浏览记录
        '''

        # history_1 =  [2, 1, 3]
        # 获取用户id

        user_id = request.user.id

        # 获取商品id
        strict_redis = get_redis_connection('history')  # type: StrictRedis
        sku_ids = strict_redis.lrange('history_%s' % user_id, 0, -1)

        # 从mysql中查询商品详情信息 (QuerySet)
        # SKU.objects.filter(id__in=[2,1,3])
        # SKU.objects.filter(id__in=sku_ids)   # 元素: bytes

        skus = []
        for sku_id in sku_ids:
            sku = SKU.objects.get(id=int(sku_id))  # bytes -> int
            skus.append(sku)

            # 序列化商品数据, 返回商品数据,instance指定字段
        s = SKUSerializer(instance=skus, many=True)
        # s.data  # 列表数据

        # 响应数据
        return Response(s.data)
示例#8
0
class OrderGoodsSerializer(serializers.ModelSerializer):
    """
    订单商品数据序列化器
    """
    sku = SKUSerializer()

    class Meta:
        model = OrderGoods
        fields = ('id', 'sku', 'count', 'price')
示例#9
0
 def get(self, request):
     redis_conn = get_redis_connection('history')
     sku_ids = redis_conn.lrange('history_%s' % request.user.id, 0, -1)
     # 查询商品对象
     skus = []
     for sku_id in sku_ids:
         skus.append(SKU.objects.get(id=sku_id))
     sku_serializer = SKUSerializer(skus, many=True)
     return Response(sku_serializer.data)
示例#10
0
	def get(self, request):
		# 根据用户取出用户的浏览记录
		user_id = request.user.id
		redis_conn = get_redis_connection('history')
		skus_list = redis_conn.lrange('history_%s' % user_id, 0, 5)
		skus = []
		for sku_id in skus_list:
			sku_obj = SKU.objects.get(id=sku_id)
			skus.append(sku_obj)
		s = SKUSerializer(skus, many=True)
		return Response(s.data)
示例#11
0
 def get(self, request):
     # 查询Redis,读取最近浏览记录
     redis_cli = get_redis_connection('history')
     # 获取当前用户所有浏览记录,返回列表[1,2,3,4]
     sku_ids = redis_cli.lrange('history%d' % request.user.id, 0, -1)
     # 查询商品对象
     skus = []
     for sku_id in sku_ids:
         skus.append(SKU.objects.get(pk=sku_id))
     # 输出json
     sku_serializer = SKUSerializer(skus, many=True)
     return Response(sku_serializer.data)
示例#12
0
文件: views.py 项目: goddess5321/mall
 def get(self, request):
     user_id = request.user.id
     redis_conn = get_redis_connection('history')
     history = redis_conn.lrange(
         'history_%s' % user_id, 0,
         constants.USER_BROWSING_HISTORY_COUNTS_LIMIT)
     skus = []
     for sku_id in history:
         sku = SKU.objects.get(id=sku_id)
         skus.append(sku)
     s = SKUSerializer(skus, many=True)
     return Response(s.data)
示例#13
0
 def get(self, request):
     """获取"""
     # 获取用户信息
     user_id = request.user.id
     # 连接redis
     redis_conn = get_redis_connection('history')
     # 获取数据
     history_sku_ids = redis_conn.lrange('history_%s' % user_id, 0, 5)
     skus = []
     for sku_id in history_sku_ids:
         sku = SKU.objects.get(pk=sku_id)
         skus.append(sku)
     # 序列化
     serializer = SKUSerializer(skus, many=True)
     return Response(serializer.data)
示例#14
0
    def get(self, request):
        user_id = request.user.id
        # 查询redis数据库
        redis_conn = get_redis_connection('history')
        sku_id_list = redis_conn.lrange('history_%s' % user_id, 0, constants.USER_BROWSING_HISTORY_COUNTS_LIMIT)

        # 根据redis返回的sku id 查询数据
        # SKU.objects.filter(id__in=sku_id_list)
        sku_list = []
        for sku_id in sku_id_list:
            sku = SKU.objects.get(id=sku_id)
            sku_list.append(sku)

        # 使用序列化器序列化
        serializer = SKUSerializer(sku_list, many=True)
        return Response(serializer.data)
示例#15
0
    def get(self, request):
        """
        获取
        """
        user_id = request.user.id

        redis_conn = get_redis_connection("history")
        history = redis_conn.lrange("history_%s" % user_id, 0, constants.USER_BROWSE_HISTORY_MAX_LIMIT - 1)
        skus = []
        # 为了保持查询出的顺序与用户的浏览历史保存顺序一致
        for sku_id in history:
            sku = SKU.objects.get(id=sku_id)
            skus.append(sku)

        s = SKUSerializer(skus, many=True)
        return Response(s.data)
示例#16
0
    def get(self,request):
        """查询用户记录"""
        # 获取user_id
        user_id = request.user.id
        # 获取可以操作redis服务器的对象
        redis_conn = get_redis_connection('history')
        # 查询出redis中用户存储的浏览记录
        sku_ids = redis_conn.lrange('history_%s' % user_id,0,-1)

        # 查询sku列表数据
        sku_list = []
        for sku_id in sku_ids:
            sku = SKU.objects.get(id=sku_id)
            sku_list.append(sku)
        # 调用序列化器实现输出:序列化器序列化操作
        serializer = SKUSerializer(sku_list,many=True)
        return Response(serializer.data)
示例#17
0
    def get(self, request):
        """获取浏览历史记录"""
        print(self)
        user = request.user

        # 链接数据库,取出sku_id
        conn = get_redis_connection('history')

        sku_id_list = conn.lrange('history_%s' % user.id, 0, 5)

        # 获取所有的商品对象
        sku = []
        for sku_id in sku_id_list:
            sku.append(SKU.objects.get(id=sku_id))
        # 序列化
        serializer = SKUSerializer(sku, many=True)
        # 返回
        return Response(serializer.data)
示例#18
0
    def get(self, request):
        """读取用户浏览记录"""

        # 创建redis连接对象
        redis_conn = get_redis_connection('history')
        # 查询出redis中当前登录用户的浏览记录[b'1', b'2', b'3']
        sku_ids = redis_conn.lrange('history_%d' % request.user.id, 0, -1)
        # 把sku_id对应的模型取出来
        sku_list = []
        for sku_id in sku_ids:
            sku = SKU.objects.get(id=sku_id)
            sku_list.append(sku)

        # 序列化器
        serializer = SKUSerializer(sku_list, many=True)

        # 返回
        return Response(serializer.data)
示例#19
0
    def get(self, request):
        """查看用户历史浏览记录"""
        # 创建数据库连接对象
        redis_conn = get_redis_connection('history')
        # 拼接key
        history_key = 'history_%s' % request.user.id
        # 获取redis列表指定区间的元素
        sku_id_list = redis_conn.lrange(
            history_key, 0, constants.USER_BROWSING_HISTORY_COUNTS_LIMIT - 1)

        sku_list = []
        for sku_id in sku_id_list:
            sku = SKU.objects.get(id=sku_id)
            # 将商品对象添加到模型类对象中
            sku_list.append(sku)
        # 将数据进行序列化,并返回
        serializer = SKUSerializer(sku_list, many=True)

        return Response(serializer.data)
示例#20
0
文件: views.py 项目: fengnaxin/aaa
    def get(self, request):
        """获取用户的浏览记录"""

        # 获取用户id
        user_id = request.user.id

        # 创建redis连接
        redis_conn = get_redis_connection('history')

        # 取出当前用户redis浏览数据
        sku_ids = redis_conn.lrange('history_%s' % user_id, 0, -1)
        sku_list = []
        # 遍历sku_ids列表
        for sku_id in sku_ids:
            sku_model = SKU.objects.get(id=sku_id)
            sku_list.append(sku_model)
        serializer = SKUSerializer(sku_list, many=True)

        Response(serializer.data)
示例#21
0
    def get(self, request):
        """读取用户的浏览记录"""
        # 创建redis连接对象
        redis_conn = get_redis_connection('history')
        # 查询出redis中当前登录用户的浏览记录[b'1', b'2', b'3']
        sku_ids = redis_conn.lrange('hisrory_%d' % request.user.id, 0, -1)

        # 把sku_id对应的sku模型取出来
        # skus = SKU.objects.filter(id__in=sku_ids)  # 此查询它会对数据进行排序处理
        # 查询sku列表数据
        sku_list = []
        for sku_id in sku_ids:
            sku = SKU.objects.get(id=sku_id)
            sku_list.append(sku)

        # 序列化器
        serializer = SKUSerializer(sku_list, many=True)

        return Response(serializer.data)
示例#22
0
文件: views.py 项目: lsybzb/meiduo
    def get(self, request):
        """获取浏览记录"""

        # 1.获取用户id
        user_id = request.user.id
        # 2.建立redis连接
        redis_conn = get_redis_connection('history')
        # 3.获取sku_id列表
        history = redis_conn.lrange(
            'history_%s' % user_id, 0,
            constants.USER_BROWSING_HISTORY_COUNTS_LIMIT - 1)
        # 4.通过sku_id列表获取到所有对应的sku对象,放入列表中
        skus = []
        for sku_id in history:
            sku = SKU.objects.get(id=sku_id)
            skus.append(sku)
        # 5.序列化列表
        skus_dict = SKUSerializer(skus, many=True)
        # 6.返回序列化后的数据
        return Response(skus_dict.data)
示例#23
0
文件: views.py 项目: PyCN/book
    def get(self, request):
        """
        获取用户浏览历史商品
        :param request:
        :return:
        """
        # 获取用户id
        user_id = self.request.user.id

        # 获取连接
        redis_conn = get_redis_connection('history')
        sku_ids = redis_conn.lrange(
            'history_%s' % user_id, 0,
            constants.USER_BROWSING_HISTORY_COUNTS_LIMIT)
        skus = []
        # 与用户浏览记录保持一致,需要遍历sku_ids
        for sku_id in sku_ids:
            sku = SKU.objects.get(id=sku_id)
            skus.append(sku)

        serializer = SKUSerializer(skus, many=True)
        return Response(serializer.data)
示例#24
0
文件: views.py 项目: Zhusea/duomei
    def get(self, request):
        """获取历史记录"""

        user_id = request.user.id

        redis_conn = get_redis_connection('history')
        sku_id_list = redis_conn.lrange(
            'history_{}'.format(user_id), 0,
            constants.USER_BROWSING_HISTORY_COUNTS_LIMIT)

        sku_obj = []
        for sku_id in sku_id_list:
            try:
                sku = SKU.objects.get(id=sku_id)
            except SKU.DoesNotExist:
                pass
            else:
                sku_obj.append(sku)

        serializer = SKUSerializer(sku_obj, many=True)

        return Response(serializer.data)
示例#25
0
class SKUCommentSerializer(serializers.ModelSerializer):
    """商品评论序展示列化器"""
    sku = SKUSerializer()
    class Meta:
        model = OrderGoods
        fields = ['sku', 'price']