コード例 #1
0
 def post(self, request):
     """
     添加Host
     :param request:
     :return:
     """
     data = JSONParser().parse(request)
     result = self.parameter_check(data)
     if result:
         return result
     try:
         obj = Project.objects.get(id=data["project_id"])
     except ObjectDoesNotExist:
         return JsonResponse(code_msg=GlobalStatusCode.project_not_exist())
     obi = GlobalHost.objects.filter(name=data["name"],
                                     project=data["project_id"])
     if obi:
         return JsonResponse(code_msg=GlobalStatusCode.name_repetition())
     else:
         serializer = GlobalHostSerializer(data=data)
         with transaction.atomic():
             if serializer.is_valid():
                 # 外键project_id
                 serializer.save(project=obj)
                 # 记录动态
                 record_dynamic(project=data["project_id"],
                                _type="添加",
                                operationObject="域名",
                                user=request.user.pk,
                                data=data["name"])
                 return JsonResponse(
                     data={"host_id": serializer.data.get("id")},
                     code_msg=GlobalStatusCode.success())
             return JsonResponse(code_msg=GlobalStatusCode.fail())
コード例 #2
0
 def post(self, request):
     """
     添加Host
     :param request:
     :return:
     """
     data = JSONParser().parse(request)
     result = self.parameter_check(data)
     if result:
         return result
     try:
         obj = Project.objects.get(id=data["project_id"])
         if not request.user.is_superuser and obj.user.is_superuser:
             return JsonResponse(code="999983", msg="无操作权限!")
     except ObjectDoesNotExist:
         return JsonResponse(code="999995", msg="项目不存在!")
     pro_data = ProjectSerializer(obj)
     if not pro_data.data["status"]:
         return JsonResponse(code="999985", msg="该项目已禁用")
     obi = GlobalHost.objects.filter(name=data["name"], project=data["project_id"])
     if obi:
         return JsonResponse(code="999997", msg="存在相同名称!")
     else:
         serializer = GlobalHostSerializer(data=data)
         with transaction.atomic():
             if serializer.is_valid():
                 # 外键project_id
                 serializer.save(project=obj)
                 # 记录动态
                 record_dynamic(project=data["project_id"],
                                _type="添加", operationObject="域名", user=request.user.pk, data=data["name"])
                 return JsonResponse(data={
                     "host_id": serializer.data.get("id")
                 }, code="999999", msg="成功!")
             return JsonResponse(code="999998", msg="失败!")