def updateApi(data): with transaction.atomic(): try: serialize = ApiInfoDeserializer(data=data) if serialize.is_valid(): try: data["userUpdate"] = User.objects.get( id=data["userUpdate"]) obi = ApiInfo.objects.get(id=data["id"]) serialize.update(instance=obi, validated_data=data) except KeyError: serialize.update(instance=obi, validated_data=data) header = Q() if "headDict" in data and len(data.get("headDict")): for i in data["headDict"]: if i.get("api") and i.get("id"): header = header | Q(id=i["id"]) if i["name"]: head_serialize = ApiHeadDeserializer( data=i) if head_serialize.is_valid(): i["api"] = ApiInfo.objects.get( id=i["api"]) head_serialize.update( instance=ApiHead.objects.get( id=i["id"]), validated_data=i) else: if i.get("name"): i["api"] = data['id'] head_serialize = ApiHeadDeserializer( data=i) if head_serialize.is_valid(): head_serialize.save( api=ApiInfo.objects.get( id=data["id"])) header = header | Q( id=head_serialize.data.get("id")) ApiHead.objects.exclude(header).filter( api=data["id"]).delete() api_param = Q() api_param_raw = Q() if "requestList" in data and len(data.get("requestList")): if data["requestParameterType"] in ( "application/x-www-form-urlencoded", "application/json", "text/plain"): ApiParameterRaw.objects.filter( api=data["id"]).delete() for i in data["requestList"]: if i.get("api") and i.get("id"): api_param = api_param | Q(id=i["id"]) if i["name"]: param_serialize = ApiParameterDeserializer( data=i) if param_serialize.is_valid(): i["api"] = ApiInfo.objects.get( id=i["api"]) param_serialize.update( instance=ApiParameter.objects. get(id=i["id"]), validated_data=i) else: if i.get("name"): i["api"] = data['id'] param_serialize = ApiParameterDeserializer( data=i) if param_serialize.is_valid(): param_serialize.save( api=ApiInfo.objects.get( id=data["id"])) api_param = api_param | Q( id=param_serialize.data.get( "id")) else: try: obj = ApiParameterRaw.objects.get( api=data["id"]) obj.data = data["requestList"] obj.save() except ObjectDoesNotExist: obj = ApiParameterRaw( api=ApiInfo.objects.get(id=data['id']), data=data["requestList"]) obj.save() api_param_raw = api_param_raw | Q(id=obj.id) ApiParameter.objects.exclude(api_param).filter( api=data["id"]).delete() ApiParameterRaw.objects.exclude(api_param_raw).filter( api=data["id"]).delete() api_response = Q() if "responseList" in data and len( data.get("responseList")): for i in data["responseList"]: if i.get("api") and i.get("id"): api_response = api_response | Q(id=i["id"]) if i["name"]: response_serialize = ApiResponseDeserializer( data=i) if response_serialize.is_valid(): i["api"] = ApiInfo.objects.get( id=i["api"]) response_serialize.update( instance=ApiResponse.objects.get( id=i["id"]), validated_data=i) else: if i.get("name"): i["api"] = data['id'] response_serialize = ApiResponseDeserializer( data=i) if response_serialize.is_valid(): response_serialize.save( api=ApiInfo.objects.get( id=data["id"])) api_response = api_response | Q( id=response_serialize.data.get( "id")) ApiResponse.objects.exclude(api_response).filter( api=data["id"]).delete() return data else: print("更新接口[%s]出现异常: %s" % (data["name"], serialize.error)) return None except: logging.error(traceback.format_exc())
def post(self, request): """ 修改接口 :param request: :return: """ data = JSONParser().parse(request) result = self.parameter_check(data) if result: return result data["userUpdate"] = request.user.pk try: pro_data = Project.objects.get(id=data["project_id"]) if not request.user.is_superuser and pro_data.user.is_superuser: return JsonResponse(code="999983", msg="无操作权限!") except ObjectDoesNotExist: return JsonResponse(code="999995", msg="项目不存在!") pro_data = ProjectSerializer(pro_data) if not pro_data.data["status"]: return JsonResponse(code="999985", msg="该项目已禁用") api_name = ApiInfo.objects.filter( name=data["name"], project=data["project_id"]).exclude(id=data["id"]) if len(api_name): return JsonResponse(code="999997", msg="存在相同名称!") try: obi = ApiInfo.objects.get(id=data["id"]) except ObjectDoesNotExist: return JsonResponse(code="999990", msg="接口不存在!") with transaction.atomic(): serialize = ApiInfoDeserializer(data=data) if serialize.is_valid(): data["userUpdate"] = request.user try: if not isinstance(data.get("apiGroupLevelFirst_id"), int): return JsonResponse(code="999996", msg="参数有误!") ApiGroupLevelFirst.objects.get( id=data["apiGroupLevelFirst_id"], project=data["project_id"]) User.objects.get(id=request.user.pk) serialize.update(instance=obi, validated_data=data) except KeyError: User.objects.get(id=request.user.pk) serialize.update(instance=obi, validated_data=data) except ObjectDoesNotExist: return JsonResponse(code="999991", msg="分组不存在!") header = Q() if len(data.get("headDict")): for i in data["headDict"]: if i.get("api") and i.get("id"): header = header | Q(id=i["id"]) if i["name"]: head_serialize = ApiHeadDeserializer(data=i) if head_serialize.is_valid(): i["api"] = ApiInfo.objects.get(id=i["api"]) head_serialize.update( instance=ApiHead.objects.get( id=i["id"]), validated_data=i) else: if i.get("name"): i["api"] = data['id'] head_serialize = ApiHeadDeserializer(data=i) if head_serialize.is_valid(): head_serialize.save( api=ApiInfo.objects.get(id=data["id"])) header = header | Q( id=head_serialize.data.get("id")) ApiHead.objects.exclude(header).delete() api_param = Q() api_param_raw = Q() if len(data.get("requestList")): if data["requestParameterType"] == "form-data": ApiParameterRaw.objects.filter(api=data["id"]).delete() for i in data["requestList"]: if i.get("api") and i.get("id"): api_param = api_param | Q(id=i["id"]) if i["name"]: param_serialize = ApiParameterDeserializer( data=i) if param_serialize.is_valid(): i["api"] = ApiInfo.objects.get( id=i["api"]) param_serialize.update( instance=ApiParameter.objects.get( id=i["id"]), validated_data=i) else: if i.get("name"): i["api"] = data['id'] param_serialize = ApiParameterDeserializer( data=i) if param_serialize.is_valid(): param_serialize.save( api=ApiInfo.objects.get( id=data["id"])) api_param = api_param | Q( id=param_serialize.data.get("id")) else: try: obj = ApiParameterRaw.objects.get(api=data["id"]) obj.data = data["requestList"] obj.save() except ObjectDoesNotExist: obj = ApiParameterRaw( api=ApiInfo.objects.get(id=data['id']), data=data["requestList"]) obj.save() api_param_raw = api_param_raw | Q(id=obj.id) ApiParameter.objects.exclude(api_param).delete() ApiParameterRaw.objects.exclude(api_param_raw).delete() api_response = Q() if len(data.get("responseList")): for i in data["responseList"]: if i.get("api") and i.get("id"): api_response = api_response | Q(id=i["id"]) if i["name"]: response_serialize = ApiResponseDeserializer( data=i) if response_serialize.is_valid(): i["api"] = ApiInfo.objects.get(id=i["api"]) response_serialize.update( instance=ApiResponse.objects.get( id=i["id"]), validated_data=i) else: if i.get("name"): i["api"] = data['id'] response_serialize = ApiResponseDeserializer( data=i) if response_serialize.is_valid(): response_serialize.save( api=ApiInfo.objects.get(id=data["id"])) api_response = api_response | Q( id=response_serialize.data.get("id")) ApiResponse.objects.exclude(api_response).delete() record_dynamic(project=data["project_id"], _type="新增", operationObject="接口", user=request.user.pk, data="新增接口“%s”" % data["name"]) api_record = ApiOperationHistory( api=ApiInfo.objects.get(id=data['id']), user=User.objects.get(id=request.user.pk), description="新增接口\"%s\"" % data["name"]) api_record.save() return JsonResponse(code="999999", msg="成功!") return JsonResponse(code="999996", msg="参数有误!")