def get(self,request): form = UserForm(request.GET) if form.is_valid(): user_id = form.cleaned_data.get('u').id username = form.cleaned_data.get('u').username data = {"id":user_id,'username':username} token = Token.create_token(data) r = get_redis_connection() key = session_pre + username r.set(key,token,token_expire) return NbResponse(token=token) else: return NbResponse(-1,form.error_msg)
def process_request(self, request): if not self.check_uri(request.path_info): # 不在白名单,校验token token = request.META.get('HTTP_TOKEN') # 从header里面获取token # token = request.GET.get('token') redis = django_redis.get_redis_connection() if token: user_bytes = redis.get(const.token_prefix + token) if user_bytes: user = pickle.loads(user_bytes) # 从redis里面获取到用户信息 request.user = user # 加到request里面 request.token = token # request里面添加token else: return NbResponse(-1, '请登录!') else: return NbResponse(-1, '请登录!')
def post(self, request): user_form_obj = forms.LoginForm(request.POST) if user_form_obj.is_valid(): user = user_form_obj.cleaned_data['user'] token = '%s%s' % (user.username, time.time()) token = md5(token) try: redis = django_redis.get_redis_connection() except: raise Exception("连接不上redis,请检查redis!") redis.set(const.token_prefix + token, pickle.dumps(user), const.token_expire) return NbResponse(token=token, user=user.username) else: return NbResponse(-1, user_form_obj.error_format)
def post(self, request): form_obj = self.form(request.POST) if form_obj.is_valid(): # print(form_obj.cleaned_data) # md5加密 md5_password = self.model.make_password( form_obj.cleaned_data['password']) # 更新原有的cleaned_data form_obj.cleaned_data['password'] = md5_password # 再创建 # print(form_obj.cleaned_data) self.model.objects.create(**form_obj.cleaned_data) ret = NbResponse() # 默认请求成功 else: ret = NbResponse(code=-1, msg=form_obj.error_format) return ret
def put(self, request): # 用例在更新时处理自关联的关系。 # 先清掉之前所有的关联,然后在重新创建 rely_case = request.PUT.get('rely_case') instance = self.model.objects.get(id=request.PUT.get('id')) form_obj = self.form(request.PUT, instance=instance) if form_obj.is_valid(): # 通过当前用例 获取所有被依赖的用例 并删除 instance.case.all().delete() # 重新建立自关联绑定关系 for item in json.loads(rely_case): models.CasePremise.objects.create(case_id=instance.id, premise_case_id=item) form_obj.save() ret = NbResponse() # 默认请求成功 else: ret = NbResponse(code=-1, msg=form_obj.error_format) return ret
def post(self, request): collect_id = request.POST.get('collect_id') user_id = request.POST.get('user_id') for item_id in json.loads(collect_id): # 当页面调用异步任务时,会返回一个唯一的任务id task_id = run_collection.delay(item_id, user_id) models.CaseCollection.objects.filter(id=item_id).update( report_batch=task_id, status=3) return NbResponse()
def get(self, request): page_data, page_obj = self.get_query_set_page_data() # 获取分页之后的数据 data_list = [] for instance in page_data: model_dict = model_to_dict(instance, self.fields, self.exclude_fields) # 转成字典 model_dict['author_name'] = instance.author.name # 给字典里面加字段 data_list.append(model_dict) return NbResponse(data=data_list, count=page_obj.count)
def get(self, request): page_data, page_obj = self.get_query_set_page_data() # 获取分页之后的数据 data_list = [] for instance in page_data: # model_dict = model_to_dict(instance, self.fields, self.exclude_fields) # 转成字典 model_dict['user'] = instance.user.username model_dict['project_name'] = instance.project.name model_dict['project_id'] = instance.project.id data_list.append(model_dict) return NbResponse(data=data_list, count=page_obj.count)
def post(self, request): form_obj = self.form(request.POST) # 从request.POST将用例依赖的case取出来 rely_case = request.POST.get('rely_case') if form_obj.is_valid(): # 想要进行自关联绑定,需要具备依赖用用例的id、以及自己的id case = self.model.objects.create(**form_obj.cleaned_data) # 绑定自关联 创建自关联关系 for rely_case_id in json.loads(rely_case): models.CasePremise.objects.create(case_id=case.id, premise_case_id=rely_case_id) ret = NbResponse() # 默认请求成功
def get(self, requests): project_id = requests.GET.get('project_id') case_id = requests.GET.get('case_id') if case_id: # 编辑操作 当前这条用例不可以返回 通过exclude 进行过滤 qs_data = models.Case.objects.filter( project_id=project_id, is_delete=1).exclude(id=case_id).values('id', 'title') else: qs_data = models.Case.objects.filter(project_id=project_id, is_delete=1).values( 'id', 'title') return NbResponse(data=list(qs_data))
def get(self, request): filter_dict = self.get_filter_dic() search_obj = self.get_search_obj() query_set = self.model.objects.filter(**filter_dict).filter(search_obj) page_data, count = self.get_paginator(query_set) data = [] for c in page_data: d = model_to_dict(c, exclude=self.exclude, fields=self.field) # 把查出来的每条数据转成字典 d['category_name'] = c.category.name d['category_id'] = c.category.id d.pop('category') data.append(d) return NbResponse(data=data, count=count)
def get(self, request): ''' #http://127.0.0.1:8000/api/join_case?project_id=6&id=3 { "code": 0, "msg": "操作成功", "data": { "all_case": [ { "id": 2, "title": "测试平台项目接口" }, { "id": 1, "title": "测试平台登录接口" } ], "join_case": [ 2, 1 ] } } :param request: :return: 1、根据项目id 返回当前项目下所有没有被删除的用例。 2、根据集合id 获取已经和集合创建关系的用例id。 ''' project_id = request.GET.get('project_id') # 项目id coll_id = request.GET.get('id') # 集合id all_case = models.Case.objects.filter(project_id=project_id, is_delete=1).values( 'id', 'title') # 获取集合 coll_obj = models.CaseCollection.objects.get(id=coll_id) # 通过多对多的查询获取集合下的用例 只要id字段 join_case_qs = coll_obj.case.all().values('id') join_case = [] # 具体返回的joincase for item in join_case_qs: join_case.append(item.get('id')) data = {"all_case": list(all_case), "join_case": join_case} return NbResponse(data=data)
def post(self, request): # http://127.0.0.1:8000/api/join_case ''' join_case_list: [11,9,8,7] id: 3 :param request: :return: ''' join_case_list = request.POST.get('join_case_list') coll_id = request.POST.get('id') coll_obj = models.CaseCollection.objects.get(id=coll_id) # # 更新时第一种方案 清掉之前绑定的数据 # coll_obj.case.clear() # for item in json.loads(join_case_list): # coll_obj.case.add(item) # 第二种方案 通过 set的方式进行创建和更新 coll_obj.case.set(json.loads(join_case_list)) return NbResponse()
def post(self, request): redis = django_redis.get_redis_connection() redis.delete(const.token_prefix + request.token) return NbResponse()
def post(self, request): redis = django_redis.get_redis_connection() print(dir(request)) redis.delete(const.token_prefix + request.META.get('HTTP_TOKEN')) return NbResponse()
def process_exception(self, request, exception): if not DEBUG: return NbResponse(500, '系统开小差了,请联系管理员 %s' % (exception))
# 由于需要创建依赖用例的自关联,所以需要重写post def post(self, request): form_obj = self.form(request.POST) # 从request.POST将用例依赖的case取出来 rely_case = request.POST.get('rely_case') if form_obj.is_valid(): # 想要进行自关联绑定,需要具备依赖用用例的id、以及自己的id case = self.model.objects.create(**form_obj.cleaned_data) # 绑定自关联 创建自关联关系 for rely_case_id in json.loads(rely_case): models.CasePremise.objects.create(case_id=case.id, premise_case_id=rely_case_id) ret = NbResponse() # 默认请求成功 else: ret = NbResponse(code=-1, msg=form_obj.error_format) return ret def put(self, request): # 用例在更新时处理自关联的关系。 # 先清掉之前所有的关联,然后在重新创建 rely_case = request.PUT.get('rely_case') instance = self.model.objects.get(id=request.PUT.get('id')) form_obj = self.form(request.PUT, instance=instance) if form_obj.is_valid(): # 通过当前用例 获取所有被依赖的用例 并删除 instance.case.all().delete() # 重新建立自关联绑定关系 for item in json.loads(rely_case): models.CasePremise.objects.create(case_id=instance.id, premise_case_id=item)