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="失败!")
예제 #3
0
 def post(self, request):
     """
     修改host域名
     :param request:
     :return:
     """
     data = JSONParser().parse(request)
     result = self.parameter_check(data)
     if result:
         return result
     try:
         pro_data = Project.objects.get(id=data["project_id"])
     except ObjectDoesNotExist:
         return JsonResponse(code="999995", msg="项目不存在!")
     pro_data = ProjectSerializer(pro_data)
     if not pro_data.data["status"]:
         return JsonResponse(code="999985", msg="该项目已禁用")
     try:
         obi = GlobalHost.objects.get(id=data["id"])
     except ObjectDoesNotExist:
         return JsonResponse(code="999992", msg="host不存在!")
     host_name = GlobalHost.objects.filter(name=data["name"]).exclude(
         id=data["id"])
     if len(host_name):
         return JsonResponse(code="999997", msg="存在相同名称!")
     else:
         serializer = GlobalHostSerializer(data=data)
         with transaction.atomic():
             if serializer.is_valid():
                 # 外键project_id
                 serializer.update(instance=obi, validated_data=data)
                 # 记录动态
                 record_dynamic(project=data["project_id"],
                                _type="修改",
                                operationObject="域名",
                                user=request.user.pk,
                                data=data["name"])
                 return JsonResponse(code="999999", msg="成功!")
             return JsonResponse(code="999998", msg="失败!")