def get(self, request, *args, **kwargs): res_obj=BaseResponse() try: instance = self.get_object() serializer = self.get_serializer(instance) res_obj.data=serializer.data except Exception as e: logger.debug(str(e)) res_obj.code=3 res_obj.msg=str(e) return Response(res_obj.dict)
def get(self,request,*args,**kwargs): res_obj=BaseResponse() try: queryset = self.filter_queryset(self.get_queryset()) # 手动过滤 # 拿到过滤的条件 # category_id = str(request.query_params.get('category', '')) # logger.debug(f'category_id:{category_id}') # # 如果category_id是0或者不是数字 我们就返回所有的课程 # if category_id != '0' and category_id.isdigit(): # # 按照条件去过滤 # queryset = queryset.filter(course_category_id=category_id) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) res_obj.data = serializer.data data = serializer.data # 因为要排序的字段是我们序列化的时候自己加的字段,不能使用内置的order_by # 进行分类排序 ordering_key = request.query_params.get("ordering", "") logger.debug(f'排序:{ordering_key}') if ordering_key: if ordering_key.startswith("-"): ordering_key = ordering_key[1:] is_reverse = True else: is_reverse = False # 对返回的数据进行排序 data = sorted(data, key=lambda dict: dict.get(ordering_key, 0), reverse=is_reverse) res_obj.data = data except Exception as e: res_obj.code=1 res_obj.msg=str(e) # logger.debug(str(e)) # debug 级别为10,配置的级别如果高于他,将不会写入文件 logger.error(str(e)) #error 级别为40 #INFO = 20,waring=30 return Response(res_obj.dict)
def get(self, request, *args, **kwars): # 从缓存中读取当前用户的购物车 shopping_cat_value = CACHE.get(f'SHOPPING_CAT_{request.user.id}') #判断是否存在 if shopping_cat_value: #反序列化 shopping_cat_lst = json.loads(shopping_cat_value) logger.debug(shopping_cat_lst) else: shopping_cat_lst = [] res_obj = BaseResponse() res_obj.data = shopping_cat_lst return Response(res_obj.dict)
def post(self, request, *args, **kwargs): res_obj = BaseResponse() #滑动验证的代码,获取用户提交的验证码 gt = GeetestLib(pc_geetest_id, pc_geetest_key) challenge = request.data.get(gt.FN_CHALLENGE, '') validate = request.data.get(gt.FN_VALIDATE, '') seccode = request.data.get(gt.FN_SECCODE, '') status = True if status: # 调用极验科技的接口检验是不是真人 result = gt.success_validate(challenge, validate, seccode, None) else: # 本地校验是不是正经人 result = gt.failback_validate(challenge, validate, seccode) if result: username = request.data.get("username") password = request.data.get("password") ##使用内置的auth模块提供的authenticate方法校验用户名密码是否正确(密码是加过密的) user_obj = authenticate(username=username, password=password) if user_obj: logger.debug("用户名密码正确") # logger最好接受一个参数,接收两个将会出现报错信息 # logger.debug(res_obj,user_obj) # 创建Token token = uuid.uuid1().hex #当前时间 now = datetime.datetime.now(tz=datetime.timezone.utc) # 设置到库中 # ? 不是简单地创建,如果当前用户有token就更新 没有才创建新的 # models.Token.objects.create(key=token, user=user_obj, created=now) obj, created = models.Token.objects.update_or_create( user=user_obj, defaults={ "key": token, "created": now }) #将token设置到缓存中,读取方便: cache.set(token, user_obj, settings.AUTH_TOKEN_TIMEOUT) #返回token res_obj.data = token else: logger.debug('用户名和密码错误') res_obj.code = 20 res_obj.msg = "用户名或密码错误" else: res_obj.code = 10 res_obj.msg = "请滑动验证码进行校验" return Response(res_obj.dict)
def get(self,request,*args,**kwargs): res_obj=BaseResponse() try: queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) res_obj.data=serializer.data except Exception as e: res_obj.code=2 res_obj.msg=str(e) return Response(res_obj.dict)
def get(self, request, *args, **kwargs): '''结算中心页面''' res_obj = BaseResponse() user_id = request.user.id # 获取缓存中存在的课程列表信息 checkout_value = CACHE.get(f"BUY_{user_id}") if not checkout_value: res_obj.code = 2004 res_obj.msg = "暂无结算数据" logger.warning("暂无结算信息") return Response(res_obj.dict) buy_list = json.loads(checkout_value) #获取通用的优惠券列表 common_couple_value = CACHE.get(f'COMMON_COUPON_LIST_{user_id}') if not common_couple_value: common_couple_list = [] else: common_couple_list = json.loads(common_couple_value) #拼接返回的数据 res_obj.data = { "checkout_list": buy_list, "common_couple_list": common_couple_list } return Response(res_obj.dict)