def get_selected_course(self, request): user_id = request.user.id redis_conn = get_redis_connection('cart') cart_hash = redis_conn.hgetall('cart_%s' % user_id) course_id_list = redis_conn.smembers('selected_%s' % user_id) data = [] for course_id_byte in course_id_list: course_id = course_id_byte.decode() course_time = int(cart_hash.get(course_id_byte).decode()) course = Course.objects.get(pk=course_id) course.real_price(course_time) CourseExpire.get_expire_text(course_id, course_time) data.append({ "course_id": course.id, "course_img": constants.SERVER_IMAGE_URL + course.course_img.url, # 返回图片的url地址 "course_name": course.name, "price": course.real_price(course_time), # 商品原价 "real_price": 0.00, # 折扣以后的价格 "expire_text": CourseExpire.get_expire_text(course_id, course_time) }) return Response(data)
def get_selected_course(self, request): """获取购物车中勾选的商品信息""" user_id = request.user.id redis_conn = get_redis_connection("cart") cart_hash = redis_conn.hgetall("cart_%s" % user_id) selected_set = redis_conn.smembers("selected_%s" % user_id) data = [] print("cart_hash==>", cart_hash) for course_id_bytes in selected_set: print("course_id_bytes==>", course_id_bytes) print("cart_hash.get(course_id_bytes)==>", cart_hash.get(course_id_bytes)) course_id = course_id_bytes.decode() expire_time = int(cart_hash.get(course_id_bytes).decode()) try: course = Course.objects.get(pk=course_id) data.append({ "course_id": course.id, "course_img": constants.SERVER_IMAGE_URL + course.course_img.url, # 返回图片的url地址 "course_name": course.name, "price": course.original_price(expire_time), # 商品原价 "real_price": course.real_price(expire_time), # 折扣以后的价格 "selected": True if course_id_bytes in selected_set else False, "expire_text": CourseExpire.get_expire_text(course_id, expire_time) }) except CourseExpire.DoesNotExist: pass return Response(data)
def change_expire(self, request): user_id = request.user.id course_id = request.data.get('course_id') expire_id = request.data.get('expire_id') print(user_id, '选择有效期为', expire_id, '的课程', course_id) # 查询操作的课程是否存在 try: course = Course.objects.get(is_delete=False, is_show=True, pk=course_id) price = course.real_price # 如果前端传递的有效期的id不是0 则修改对应课程的有效期 if expire_id > 0: try: expire_obj = CourseExpire.objects.get(is_show=True, is_delete=False, pk=expire_id) except CourseExpire.DoesNotExist: expire_obj = None if not expire_obj: raise CourseExpire.DoesNotExist("课程有效期不存在") except Course.DoesNotExist: return Response({"message": "课程信息不存在"}, status=http_status.HTTP_400_BAD_REQUEST) redis_connection = get_redis_connection("cart") redis_connection.hset("cart_%s" % user_id, course_id, expire_id) try: course.price = expire_obj.price price = course.real_price except: print("出现了错误") return Response({"message": "有效期切换失败", 'price': price}) return Response({'message': '有效期切换成功', 'price': price})
def change_expire(self, request): """改变redis的课程有效期""" user_id = request.user.id course_id = request.data.get("course_id") expire_id = request.data.get("expire_id") print(expire_id, "expire_id") try: course = Course.objects.get(is_show=True, is_delete=False, pk=course_id) # 如果前端传递的有效期选项不是0 则修改对应的有效 if expire_id > 0: expire_item = CourseExpire.objects.filter(is_delete=False, is_show=True, pk=expire_id) if not expire_item: raise CourseExpire.DoesNotExist() except Course.DoesNotExist: return Response({"message": "操作的课程不存在"}, status=status.HTTP_400_BAD_REQUEST) redis_connection = get_redis_connection("cart") redis_connection.hset("cart_%s" % user_id, course_id, expire_id) # TODO 重新计算修改有效期的课程的价格 real_price = course.real_expire_price(expire_id) return Response({"message": "切换有效期成功", "price": real_price})
def change_expire(self, request): """改变redis中课程有效期""" # 获取用户id 课程id 有效期id user_id = request.user.id expire_id = request.data.get("expire_id") course_id = request.data.get("course_id") # 查询操作的课程是否存在 try: course = Course.objects.get(is_delete=False, is_show=True, pk=course_id) # 如果前端传递的有效期的id不是0 则修改对应课程的有效期 if expire_id > 0: expire_obj = CourseExpire.objects.filter(is_show=True, is_delete=False, pk=expire_id) if not expire_obj: raise CourseExpire.DoesNotExist("课程有效期不存在") except Course.DoesNotExist: return Response({"message": "课程信息不存在"}, status=status.HTTP_400_BAD_REQUEST) redis_connection = get_redis_connection("cart") redis_connection.hset("cart_%s" % user_id, course_id, expire_id) return Response({"message": "有效期切换成功"})