Example #1
0
    def post(self, request):
        # 1、提取参数
        data = json.loads(request.body.decode())
        sku_id = data.get('sku_id')
        count = data.get('count')
        selected = data.get('selected', True)  # 默认勾选

        # 2、校验参数
        if not all([sku_id, count]):
            return JsonResponse({'code': 400, 'errmsg': '缺少必要字段'})

        try:
            sku = SKU.objects.get(pk=sku_id)
        except SKU.DoesNotExist as e:
            return JsonResponse({'code': 400, 'errmsg': 'sku不存在'})

        # 3、业务数据处理
        user = request.user
        if user.is_authenticated:
            # 3.1、登陆 —— 存入redis
            conn = get_redis_connection('carts')  # 5号
            # (1)、存哈希对象 —— 商品和数量
            # 如果当前sku_id在哈希对象里面,需要把count累加
            # conn.hmset('carts_%s'%user.id, {sku_id:count})
            conn.hincrby('carts_%s' % user.id, sku_id,
                         count)  # 如果sku_id存在则累加,不存在则新增
            # (2)、存集合 —— 记录选中状态
            if selected:
                conn.sadd('selected_%s' % user.id, sku_id)
            else:
                conn.srem('selected_%s' % user.id, sku_id)
            return JsonResponse({'code': 0, 'errmsg': 'ok'})
        else:
            # 3.2、未登陆 —— 存入cookie
            # (1)、当前请求cookie里面是否已经有购物车数据了
            cookie_str = request.COOKIES.get(
                'carts')  # carts是存入cookie的购物车数据的key
            if cookie_str:
                # cookie有购物车数据,需要解密得到购物车字典数据
                cart_dict = CookieSecret.loads(cookie_str)
            else:
                # cookie中没有购物车数据
                cart_dict = {}

            # (2)、追加当前新增的商品
            if sku_id in cart_dict:
                # 把数量累加
                cart_dict[sku_id]['count'] += count
                cart_dict[sku_id]['selected'] = selected
            else:
                cart_dict[sku_id] = {'count': count, 'selected': selected}

            # (3)、把最新的cookie购物车字典数据,编码成字符串写入cookie
            cookie_str = CookieSecret.dumps(cart_dict)
            response = JsonResponse({'code': 0, 'errmsg': 'ok'})
            response.set_cookie('carts', cookie_str, max_age=14 * 3600 * 24)
            return response
Example #2
0
    def put(self, request):
        # 1、提取参数
        data = json.loads(request.body.decode())
        sku_id = data.get('sku_id')
        count = data.get('count')
        selected = data.get('selected', True)

        # 2、校验参数
        if not all([sku_id, count]):
            return JsonResponse({'code': 400, 'errmsg': '缺少参数'})

        try:
            sku = SKU.objects.get(pk=sku_id)
        except SKU.DoesNotExist as e:
            return JsonResponse({'code': 400, 'errmsg': '商品不存在'})

        # 3、业务数据处理
        user = request.user
        if user.is_authenticated:
            # 登陆,修改redis购物车
            conn = get_redis_connection('carts')
            # (1)、修改商品数量 —— 哈希对象设置键值对默认覆盖原值
            conn.hset('carts_%s' % user.id, sku_id, count)
            # (2)、修改选中状态
            if selected:
                conn.sadd('selected_%s' % user.id, sku_id)
            else:
                conn.srem('selected_%s' % user.id, sku_id)
            return JsonResponse({'code': 0, 'errmsg': 'ok'})
        else:
            # 未登陆,修改cookie购物车
            # (1)、读取cookie购物车字典数据
            cookie_str = request.COOKIES.get('carts')
            if cookie_str:
                cart_dict = CookieSecret.loads(cookie_str)
            else:
                cart_dict = {}
            # (2)、修改为新数据
            if sku_id in cart_dict:
                cart_dict[sku_id]['count'] = count
                cart_dict[sku_id]['selected'] = selected
            # (3)、重新加密写入cookie
            cookie_str = CookieSecret.dumps(cart_dict)
            response = JsonResponse({'code': 0, 'errmsg': 'ok'})
            response.set_cookie('carts', cookie_str)
            return response
Example #3
0
    def put(self, request):
        data = json.loads(request.body.decode())
        selected = data.get('selected')  # 没有传就是None

        # 判读selected类型,必须是bool类型(True or False)
        if not isinstance(selected, bool):
            return JsonResponse({'code': 400, 'errmsg': '参数有误'})

        # 业务数据处理
        user = request.user
        if user.is_authenticated:
            # 登陆,把redis购物车全部商品选中
            conn = get_redis_connection('carts')
            # (1)、获取所有sku商品
            # redis_carts = {b'1':b'5', b'2': b'6'}
            redis_carts = conn.hgetall('carts_%s' % user.id)
            # (2)、把所有sku商品的id加入集合
            sku_ids = redis_carts.keys()  # [b'1', b'2']
            if selected:
                conn.sadd(
                    'selected_%s' % user.id,
                    *sku_ids)  # *sku_ids对列表进行拆包:(*sku_ids) = (b'1', b'2')
            else:
                conn.srem('selected_%s' % user.id, *sku_ids)
            return JsonResponse({'code': 0, 'errmsg': 'ok'})
        else:
            # 未登陆,把cookie购物车中的全部商品选中
            # (1)、读取cookie购物车字典
            cookie_str = request.COOKIES.get('carts')
            if cookie_str:
                cart_dict = CookieSecret.loads(cookie_str)
            else:
                cart_dict = {}
            # (2)、把购物车字典中所有对商品的selected设置未True
            sku_ids = cart_dict.keys()
            for sku_id in sku_ids:
                cart_dict[sku_id][
                    'selected'] = selected  # 前端传来的selected是True表示全选,为False表示全部取消
            # (3)、加密新字典写入cookie
            cookie_str = CookieSecret.dumps(cart_dict)
            response = JsonResponse({'code': 0, 'errmsg': 'ok'})
            response.set_cookie('carts', cookie_str)
            return response
Example #4
0
    def delete(self, request):
        # 1、提取参数
        data = json.loads(request.body.decode())
        sku_id = data.get('sku_id')
        # 2、校验参数
        if not sku_id:
            return JsonResponse({'code': 400, 'errmsg': '缺少参数'})
        try:
            sku = SKU.objects.get(pk=sku_id)
        except SKU.DoesNotExist as e:
            return JsonResponse({'code': 400, 'errmsg': '商品不存在'})

        # 3、业务数据处理
        user = request.user
        if user.is_authenticated:
            # 登陆,删除redis购物车指定sku商品数据
            conn = get_redis_connection('carts')
            # (1)、把sku_id从哈希对象中删除
            conn.hdel('carts_%s' % user.id, sku_id)
            # (2)、把sku_id从集合中删除
            conn.srem('selected_%s' % user.id, sku_id)
            response = JsonResponse({'code': 0, 'errmsg': 'ok'})
            return response

        else:
            # 未登陆,删除cookie购物车指定sku商品数据
            # (1)、读取cookie购物车字典数据
            cookie_str = request.COOKIES.get('carts')
            if cookie_str:
                cart_dict = CookieSecret.loads(cookie_str)
            else:
                cart_dict = {}
            # (2)、删除指定sku_id
            if sku_id in cart_dict:
                # del cart_dict[sku_id] 删除字典的键值对
                cart_dict.pop(sku_id)
            # (3)、重新加密设置在cookie中
            cookie_str = CookieSecret.dumps(cart_dict)
            response = JsonResponse({'code': 0, 'errmsg': 'ok'})
            response.set_cookie('carts', cookie_str)
            return response