예제 #1
0
    def delete(self, request):
        """删除购物车数据"""
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        sku_id = serializer.validated_data['sku_id']

        user = request.user
        if user.is_authenticated():
            # 用户已登录,在redis中保存
            redis_conn = get_redis_connection('cart')
            pl = redis_conn.pipeline()
            pl.hdel('cart_%s' % user.id, sku_id)
            pl.srem('cart_selected_%s' % user.id, sku_id)
            pl.execute()
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            # 用户未登录,在cookie中保存
            response = Response(status=status.HTTP_204_NO_CONTENT)
            # 1. 从cookie中获取购物车信息
            cart = request.COOKIES.get('cart')
            if cart is not None:
                # 2. base64字符串 -> 字典
                cart = pickle.loads(base64.b64decode(cart.encode()))
                # 3. 删除字典中对应的商品
                if sku_id in cart:
                    del cart[sku_id]
                    # 4. 字典 --> base64字符串
                    cookie_cart = base64.b64encode(pickle.dumps(cart)).decode()
                    # 5. 通过cookie保存购物车数据(base64字符串)
                    response.set_cookie('cart', cookie_cart,
                                        constants.CART_COOKIE_EXPIRES)
                    return response
예제 #2
0
    def delete(self, request):
        """删除购物车商品"""
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        sku_id = serializer.validated_data["sku_id"]

        try:
            user = request.user
        except:
            user = None

        if user and user.is_authenticated:
            # 用户已经登录,在redis中删除
            redis_conn = get_redis_connection("cart")
            pl = redis_conn.pipeline()
            pl.hdel("cart_%s" % user.id, sku_id)
            pl.srem("cart_selected_%s" % user.id, sku_id)
            pl.execute()

            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            # 用户未登录,在cookie中删除
            cart = request.COOKIES.get("cart")
            response = Response(status=status.HTTP_204_NO_CONTENT)
            if cart:
                cart = pickle.loads(base64.b64decode(cart.encode()))
                if sku_id in cart:
                    del cart[sku_id]
                    cookie_cart = base64.b64encode(pickle.dumps(cart)).decode()
                    # 保存cookie
                    response.set_cookie("cart", cookie_cart, max_age=constants.CART_COOKIE_EXPIRES)

            return response
예제 #3
0
    def delete(self, request):
        data = request.data
        serializer = CartDeleteSerializer(data=data)
        serializer.is_valid()
        sku_id = serializer.data.get('sku_id')
        try:
            user = request.user
        except Exception:
            user = None
        if user is not None and user.is_authenticated:
            redis_conn = get_redis_connection('cart')
            redis_conn.hdel('cart_%s' % user.id, sku_id)
            # 当点击删除的时候,到redis_cart中把相应的商品id 给删了
            redis_conn.srem('cart_selected_%s' % user.id, sku_id)
            return Response(serializer.data)

        else:
            cart_str = request.COOKIES.get('cart')
            if cart_str is not None:

                cart = pickle.loads(base64.b64decode(cart_str.encode()))
            else:
                cart = {}

            response = Response(serializer.data)

            if sku_id in cart:
                del cart[sku_id]
                cookie_str = base64.b64encode(pickle.dumps(cart)).decode()
                response.set_cookie('cart', cookie_str, 3600)

            return response
예제 #4
0
    def delete(self, request):
        """购物车记录删除"""
        # 获取sku_id并进行校验
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        sku_id = serializer.validated_data['sku_id']

        # 获取user
        try:
            user = request.user
        except Exception:
            user = None

        # 删除用户的购物车记录
        if user and user.is_authenticated:
            # 登录,删除redis的记录
            connection = get_redis_connection('carts')
            pl = connection.pipeline()

            # 删除购物车记录中对应的商品id和count hash
            cart_key = 'cart_%s' % user.id
            # hdel(key, *fields): 删除hash中的指定属性和值,如果属性field不存在,直接忽略
            pl.hdel(cart_key, sku_id)

            # 删除购物车记录对应商品勾选状态
            cart_selected_key = 'cart_selected_%s' % user.id

            # srem(key, *values): 删除set中指定元素,如果元素不存在,直接忽略
            pl.srem(cart_selected_key, sku_id)

            pl.execute()
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            # 未登录删除cookie中的记录
            response = Response(status=status.HTTP_204_NO_CONTENT)
            cookie_cart = request.COOKIES.get('cart')
            if cookie_cart is None:
                return response
            # 解析购物车中的数据
            cart_dict = pickle.loads(base64.b64decode(cookie_cart))

            if not cart_dict:
                return response

            # 删除购物车对应商品记录
            if sku_id in cart_dict:
                del cart_dict[sku_id]
                cookie_data = base64.b64encode(
                    pickle.dumps(cart_dict)).decode()

                # 设置购物车cookie信息
                response.set_cookie('cart',
                                    cookie_data,
                                    expires=constants.CART_COOKIE_EXPIRES)

            return response
예제 #5
0
    def delete(self,request):

        """删除购物车中的商品"""

        # 创建序列化器,校验sku_id是否合法
        s = CartDeleteSerializer(data=request.data)
        s.is_valid(raise_exception=True)

        # 获取校验后的sku_id,
        sku_id = s.validated_data.get('sku_id')

        # 获取用户对象
        user = request.user

        #判断是否已经登陆
        if user.is_authenticated():
        # cart_1 = {1: 2, 2: 2}
        # cart_selected_1 = {1, 2}
        # 用户已登录,获取操作Redis的StrictRedis对象
            strict_redis = get_redis_connection('carts')  # type: StrictRedis
            # 删除商品  hdel cart_1 1
            strict_redis.hdel('cat_%s' % user.id, sku_id)
            # 删除商品勾选状态 srem cart_selected_1 1
            strict_redis.srem('cart_selected_%s' % user.id, sku_id)
            # 响应数据
            return Response(status=204)


        else:  # 未登录
            # 从cookie中获取购物车信息(base64字符串)
            cart = request.COOKIES.get('cart')
            # base64字符串 -> 字典
            # {1: {'count':2, 'selected':False}, 2: {'count':2, 'selected':False}}

            if cart is None:
                cart = {}

            else:
                cart = pickle.loads(base64.b64decode(cart.encode()))

            print('cookie', cart)

            response = Response(status=204)

            # del cart_data[3]  删除字典中的键值时,需要判断key是否存在
            if sku_id in cart:
                # 如果商品在字典中,删除字典中对应的商品
                del cart[sku_id]
                # 字典  -> base64字符串
                cart_cookie = base64.b64encode(pickle.dumps(cart)).decode()
                # 5. 通过cookie保存购物车数据(base64字符串): response.set_cookie()
                response.set_cookie('cart', cart_cookie, 60 * 60 * 24 * 365)  # 有效期: 1年

            # 6. 响应序列化数据
            return response
예제 #6
0
    def delete(self, request):
        """删除购物车商品sku_id"""
        # 校验:使用方法内部的序列化器
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        # 取出校验后的数据
        sku_id = serializer.validated_data["sku_id"]

        # 尝试获取用户登录状态
        try:
            user = request.user
        except Exception:
            user = None

        # 已登录,则从redis中删除
        if user and user.is_authenticated:
            redis_conn = get_redis_connection("cart")
            # 通过管道实例对象进行对此操作
            pl = redis_conn.pipeline()
            # 删除hash
            pl.hdel("cart_%s" % user.id, sku_id)
            # 删除set的勾选状态
            pl.srem("cart_selected_%s" % user.id, sku_id)

            pl.execute()
            # 删除成功进返回204状态码即可
            return Response(status=status.HTTP_204_NO_CONTENT)

        # 未登录,则从cookie中删除
        else:
            cookie_cart = request.COOKIES.get('cart')

            if cookie_cart:
                # 表示cookie中有购物车数据
                # 解析
                cart_dict = pickle.loads(base64.b64decode(
                    cookie_cart.encode()))
            else:
                # 表示cookie中没有购物车数据
                cart_dict = {}

            # 先构造响应体
            response = Response(status=status.HTTP_204_NO_CONTENT)

            # 在此id是cart_dict内的请求下在进行删除,不存在则过掉
            if sku_id in cart_dict:
                del cart_dict[sku_id]  # 删除字典中一个键值对

                # 删除成功后通过响应体重新构造cookie
                response.set_cookie("cart",
                                    cart_dict,
                                    max_age=constants.CART_COOKIE_EXPIRES)

            return response
예제 #7
0
    def delete(self, request):
        """删除购物车商品"""
        # 校验参数
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        # 获取校验之后的参数
        sku_id = serializer.validated_data.get('sku_id')

        # 链接redis数据库
        redis_conn = get_redis_connection("cart")

        # 获取登陆用户
        try:
            user = request.user
        except Exception:
            user = None

        if user and user.is_authenticated:
            # 登陆状态
            cart_key = 'cart_%s' % user.id
            # 删除hash中指定的属性和值,有则删除,无则忽略
            redis_conn.hdel(cart_key, sku_id)

            cart_selected_key = "cart_selected_%s" % user.id
            # 删除set集合中的元素,有则删除,无则忽略
            redis_conn.srem(cart_selected_key, sku_id)

            # 返回响应
            return Response(status=status.HTTP_204_NO_CONTENT)

        else:
            # 用户未登陆状态
            response = Response(status=status.HTTP_204_NO_CONTENT)
            cookie_cart = request.COOKIES.get('cart')

            if not cookie_cart:
                return response

            # 解析cookie数据
            cart_dict = pickle.loads(base64.b64decode(cookie_cart))

            if not cart_dict:
                return response

            if sku_id in cart_dict:
                del cart_dict[sku_id]

            cart_data = base64.b64encode(pickle.dumps(cart_dict))

            response.set_cookie('cart',
                                cart_data,
                                expires=constants.CART_COOKIE_EXPIRES)

            return response
예제 #8
0
파일: views.py 프로젝트: py-king/123
    def delete(self, request):
        '''
        1.接收前端商品id
        2.校验数据
        3.视图接收校验后的数据
        4.判断用户登录状态
        5.登录用户
            5.1.链接redis
            5.2.删除数据
            5.3.返回数据
        6.未登录用户
            6.1.获取cookie
            6.2.删除数据
            6.3.判断cart是否存在
            6.4.返回数据
        :param request:
        :return:
        '''
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        sku_id = serializer.validated_data['sku_id']
        try:
            user = request.user
        except Exception:
            user = None
        if user is not None and user.is_authenticated:
            '''redis 中删除'''
            redis_conn = get_redis_connection('cart')
            pl = redis_conn.pipeline()
            pl.hdel('cart_%s' % user.id, sku_id)
            pl.srem('cart_selected_%s' % user.id, sku_id)
            pl.execute()
            return Response(status=status.HTTP_204_NO_CONTENT)

        else:
            '''cookie 中删除'''
            cart_str = request.COOKIES.get('cart')
            if cart_str is not None:
                cart_dict = pickle.loads(base64.b64decode(cart_str.encode()))
            else:
                cart_dict = {}
            response = Response(serializer.data)
            if sku_id in cart_dict:
                del cart_dict[sku_id]
                cookie_str = base64.b64encode(pickle.dumps(cart_dict)).decode()
                response.set_cookie('cart', cookie_str)
            return response
예제 #9
0
    def delete(self, request):

        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        # 获取sku_id
        sku_id = serializer.validated_data.get('sku_id')
        user = request.user
        if user.is_authenticated:
            # 获取用户要删除的商品id
            # 用户已登录,在redis中保存
            redis_conn = get_redis_connection('cart')
            pl = redis_conn.pipeline()
            pl.hdel('cart_%s' % user.id, sku_id)
            pl.srem('cart_select_%s' % user.id, sku_id)
            pl.execute()
            return Response(status=status.HTTP_204_NO_CONTENT)
        return Response(status=301)
예제 #10
0
파일: views.py 프로젝트: xiewenquan/meiduo
    def delete(self,request):

        # 1.后端接收数据
        data=request.data
        # 2.校验数据
        serializer=CartDeleteSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        # 3.获取校验之后的数据
        sku_id=serializer.validated_data.get('sku_id')
        # 4.获取用户信息
        try:
            user=request.user
        except Exception as e:
            user=None
        # 5.根据用户信息进行判断
        if user is not None and user.is_authenticated:

            # 6.登陆用户redis
            #     6.1 连接redis
            redis_conn =get_redis_connection('cart')
            #     6.2 删除数据
            #hash
            redis_conn.hdel('cart_%s'%user.id, sku_id)
            #set
            redis_conn.srem('cart_selected_%s'%user.id, sku_id)
            #     6.3 返回相应
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            # 7.未登录用户cookie
            #     7.1 获取cookie数据
            cookie_str=request.COOKIES.get('cart')
            #     7.2 判断cart数据是否存在
            if cookie_str is not None:
                cookie_str=pickle.loads(base64.b64decode(cookie_str))
            else:
                cookie_str={}
            #     7.3 删除指定数据
            if sku_id in cookie_str:
                del cookie_str[sku_id]
            #     7.4 返回相应
            response=Response(status=status.HTTP_204_NO_CONTENT)
            value=base64.b64encode(pickle.dumps(cookie_str)).decode()
            response.set_cookie('cart', value)

            return response
예제 #11
0
파일: views.py 프로젝트: ZHJlYW0/DT_CMS
    def delete(self, request):
        """
        删除购物车数据
        """
        serializer = CartDeleteSerializer(data=request.data)
        print(request.data)
        serializer.is_valid(raise_exception=True)
        goods_id = serializer.data['goods_id']

        user = request.user
        redis_conn = get_redis_connection('cart')
        redis_conn.hdel('cart_%s' % user.id, goods_id)
        # return Response(status=status.HTTP_204_NO_CONTENT)

        datas = {  # 返回购物车总条数
            'total_count': get_cart_count(request, redis_conn)
        }
        return Response(datas)
예제 #12
0
    def delete(self, request):
        """删除购物车数据"""
        # 校验参数(sku_id是否符合规范, 商品是否存在)
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        sku_id = serializer.validated_data['sku_id']
        # 判断用户是否登录
        try:
            user = request.user
        except Exception:
            user = None

        if user and user.is_authenticated:
            # 登录, 删除 redis
            redis_conn = get_redis_connection('cart')
            pl = redis_conn.pipeline()
            # 删除 hash
            pl.hdel('cart_%s' % user.id, sku_id)
            # 删除 set
            pl.srem('cart_selected_%s' % user.id, sku_id)
            pl.execute()
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            # 未登录, 删除 cookie
            cart_cookie = request.COOKIES.get('cart')
            response = Response(status=status.HTTP_204_NO_CONTENT)
            if cart_cookie:
                # cookie中有购物车数据
                # 解析
                cart_dict = pickle.loads(base64.b64decode(cart_cookie.encode()))
            else:
                # 没有购物车数据
                cart_dict = {}
            if sku_id in cart_dict:
                # 删除
                del cart_dict[sku_id]

                # 数据序列化操作
                cart_cookie = base64.b64encode(pickle.dumps(cart_dict)).decode()
                # 设置cookie
                response.set_cookie('cart', cart_cookie, max_age=constants.CART_COOKIE_EXPIRES)
            return response
예제 #13
0
    def delete(self, request):
        #1.接收参数,对参数进行校验
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        sku_id = serializer.data.get('sku_id')
        #2.获取用户信息,根据用户信息进行判断
        try:
            user = request.user
        except Exception:
            user = None
        if user is not None and user.is_authenticated:
            # 3. 登录用户redis
            #     3.1 连接redis
            redis_conn = get_redis_connection('cart')
            #     3.2 删除数据 hash,set
            redis_conn.hdel('cart_%s' % user.id, sku_id)
            redis_conn.srem('cart_selected_%s' % user.id, sku_id)
            #     3.3 返回响应
            return Response(serializer.data)
        else:
            # 4. 未登录用户 cookie
            #     4.1 获取cookie数据并进行判断
            cookie_str = request.COOKIES.get('cart')
            if cookie_str is not None:
                cart = pickle.loads(base64.b64decode(cookie_str))
            else:
                cart = {}
            #     4.2 删除数据
            if sku_id is cart:
                del cart[sku_id]

            #     4.3 dict 对购物车数据进行处理
            new_cookie = base64.b64encode(pickle.dumps(cart)).decode()
            #     4.4 返回响应
            response = Response(serializer.data)

            response.set_cookie('cart', new_cookie)

            return response
예제 #14
0
    def delete(self, request):
        """
        删除购物车数据
        """
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        sku_id = serializer.validated_data['sku_id']

        try:
            user = request.user
        except Exception:
            user = None

        if user is not None and user.is_authenticated:
            # 用户已登录,在redis中保存
            redis_conn = get_redis_connection('cart')
            pl = redis_conn.pipeline()
            pl.hdel('cart_%s' % user.id, sku_id)
            pl.srem('cart_selected_%s' % user.id, sku_id)
            pl.execute()
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            # 用户未登录,在cookie中保存
            response = Response(status=status.HTTP_204_NO_CONTENT)

            # 使用pickle序列化购物车数据,pickle操作的是bytes类型
            cart = request.COOKIES.get('cart')

            # 不存在则直接返回
            if cart is not None:
                cart = pickle.loads(base64.b64decode(cart.encode()))
                if sku_id in cart:
                    del cart[sku_id]
                    cookie_cart = base64.b64encode(pickle.dumps(cart)).decode()
                    # 设置购物车的cookie
                    # 需要设置有效期,否则是临时cookie
                    response.set_cookie('cart',
                                        cookie_cart,
                                        max_age=constants.CART_COOKIE_EXPIRES)
            return response
예제 #15
0
    def delete(self, request):
        """
        删除购物车数据
        """
        serializer = CartDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        sku_id = serializer.validated_data['sku_id']

        try:
            user = request.user
        except Exception:
            user = None

        if user is not None and user.is_authenticated:
            # 用户已登录,在redis中保存
            redis_conn = get_redis_connection('cart')
            pl = redis_conn.pipeline()
            pl.hdel('cart_%s' % user.id, sku_id)
            pl.srem('cart_select_%s' % user.id, sku_id)
            pl.execute()
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            # 用户未登录,在cookie中保存
            response = Response(status=status.HTTP_204_NO_CONTENT)
            cart = request.COOKIES.get('cart')
            if cart is not None:
                cart = myjson.loads(cart)
                if sku_id in cart:
                    del cart[sku_id]
                    cookie_cart = myjson.dumps(cart)
                    # 设置购物车的cookie
                    # 需要设置有效期,否则是临时cookie
                    response.set_cookie('cart',
                                        cookie_cart,
                                        max_age=constants.CART_COOKIE_EXPIRES)
                return response
예제 #16
0
    def delete(self, request):
        """保存商品信息到购物车"""
        # 1. 获取前端json数据
        data = request.data
        # 2. 序列化数据,并取出三个数据(sku_id, count, selected)
        serializer = CartDeleteSerializer(data)
        sku_id = serializer.data['sku_id']
        # 3. 用户登录判断
        try:
            user = request.user
        except:
            user = None

        # 3.1 用户已登录
        if user is not None:
            # 3.1.1 链接数据库
            conn = get_redis_connection('carts')

            # 3.1.2 Hash类型保存
            conn.hdel('cart_%s' % user.id, sku_id)

            # 3.1.4 返回响应
            return Response(status=status.HTTP_204_NO_CONTENT)
        # 3.2 用户未登录
        else:
            response = Response(status=status.HTTP_204_NO_CONTENT)
            # 3.2.1 获取cookie,并判断。为空置为{}
            cart_cookie = request.COOKIES.get('cart_cookie')
            # 3.2.2 如果存在,cookie解码,并判断是否存在响应商品,如果存在,数量增加
            if cart_cookie:
                # 解码
                cart = pickle.loads(base64.b64decode(cart_cookie.encode()))
                if sku_id in cart:  # 判断要删除的商品是否在cookie列表中
                    del cart[sku_id]
                    cart_cookie = base64.b64encode(pickle.dumps(cart)).decode()
                    response.set_cookie('cart_cookie', cart_cookie,
                                        60 * 60 * 24)

            return response
예제 #17
0
    def delete(self, request):
        """
               前端需要将商品id,count(个数 是采用的将最终数量提交给后端),selected提交给后端

               1.接收数据
               2.校验数据
               3.获取验证之后的数据
               4.获取用户信息
               5.根据用户的登陆状态进行判断
               6.登陆用户redis
                   6.1 连接redis
                   6.2 更新数据
                   6.3 返回相应
               7.未登录用户cookie
                   7.1 获取cookie数据
                   7.2 判断cart数据是否存在
                   7.3 更新数据
                   7.4 返回相应

               """
        # 1.接收数据
        data = request.data
        # 2.校验数据
        serializer = CartDeleteSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        # 3.获取验证之后的数据
        sku_id = serializer.validated_data['sku_id']
        # 4.获取用户信息
        try:
            user = request.user
        except Exception as e:
            user = None
        # 5.根据用户的登陆状态进行判断
        # request.user.is_authenticated
        if user is not None and user.is_authenticated:
            # 6.登陆用户redis
            #     6.1 连接redis
            redis_conn = get_redis_connection('cart')
            #     6.2 更新数据
            redis_conn.hdel('cart_%s' % user.id, sku_id)
            redis_conn.srem('cart_selected_%s' % user.id, sku_id)
            #     6.3 返回相应
            return Response(status=status.HTTP_204_NO_CONTENT)

        else:
            # 7.未登录用户cookie
            #     7.1 获取cookie数据
            cookie_str = request.COOKIES.get('cart')
            #     7.2 判断cart数据是否存在
            if cookie_str is not None:
                cookie_cart = pickle.loads(base64.b64decode(cookie_str))
            else:
                cookie_cart = {}
            #     7.3 删除数据
            if sku_id in cookie_cart:
                del cookie_cart[sku_id]
            #     7.4 返回相应
            response = Response(status=status.HTTP_204_NO_CONTENT)
            value = base64.b64encode(pickle.dumps(cookie_cart)).decode()
            response.set_cookie('cart', value)
            return response