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())
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="失败!")
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="失败!")
def get(self, request): """ 获取host列表 :param request: :return: """ try: page_size = int(request.GET.get("page_size", 20)) page = int(request.GET.get("page", 1)) except (TypeError, ValueError): return JsonResponse(code="999995", msg="page and page_size must be integer!") project_id = request.GET.get("project_id") if not project_id.isdecimal(): return JsonResponse(code="999995", msg="参数有误!") try: pro_data = Project.objects.get(id=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="该项目已禁用") name = request.GET.get("name") if name: obi = GlobalHost.objects.filter(name__contains=name, project=project_id).order_by("id") else: obi = GlobalHost.objects.filter(project=project_id).order_by("id") paginator = Paginator(obi, page_size) # paginator对象 total = paginator.num_pages # 总页数 try: obm = paginator.page(page) except PageNotAnInteger: obm = paginator.page(1) except EmptyPage: obm = paginator.page(paginator.num_pages) serialize = GlobalHostSerializer(obm, many=True) return JsonResponse(data={ "data": serialize.data, "page": page, "total": total }, code="999999", msg="成功!")
def get(self, request): """ 获取host列表 :param request: :return: """ try: page_size = int(request.GET.get("page_size", 20)) page = int(request.GET.get("page", 1)) except (TypeError, ValueError): return JsonResponse(code_msg=GlobalStatusCode.page_not_int()) project_id = request.GET.get("project_id") if not project_id.isdecimal(): return JsonResponse(code_msg=GlobalStatusCode.parameter_wrong()) try: Project.objects.get(id=project_id) except ObjectDoesNotExist: return JsonResponse(code_msg=GlobalStatusCode.project_not_exist()) name = request.GET.get("name") if name: obi = GlobalHost.objects.filter(name__contains=name, project=project_id).order_by("id") else: obi = GlobalHost.objects.filter(project=project_id).order_by("id") paginator = Paginator(obi, page_size) # paginator对象 total = paginator.num_pages # 总页数 try: obm = paginator.page(page) except PageNotAnInteger: obm = paginator.page(1) except EmptyPage: obm = paginator.page(paginator.num_pages) serialize = GlobalHostSerializer(obm, many=True) return JsonResponse(data={ "data": serialize.data, "page": page, "total": total }, code_msg=GlobalStatusCode.success())
def host_total(request): """ 获取host列表 project_id 项目ID :return: """ try: page_size = int(request.GET.get('page_size', 20)) page = int(request.GET.get('page', 1)) except (TypeError, ValueError): return JsonResponse(code_msg=GlobalStatusCode.page_not_int()) project_id = request.GET.get('project_id') if not project_id.isdecimal(): return JsonResponse(code_msg=GlobalStatusCode.parameter_wrong()) obj = Project.objects.filter(id=project_id) if obj: name = request.GET.get('name') if name: obi = GlobalHost.objects.filter(name__contains=name, project=project_id).order_by('id') else: obi = GlobalHost.objects.filter(project=project_id).order_by('id') paginator = Paginator(obi, page_size) # paginator对象 total = paginator.num_pages # 总页数 try: obm = paginator.page(page) except PageNotAnInteger: obm = paginator.page(1) except EmptyPage: obm = paginator.page(paginator.num_pages) serialize = GlobalHostSerializer(obm, many=True) return JsonResponse(data={ 'data': serialize.data, 'page': page, 'total': total }, code_msg=GlobalStatusCode.success()) else: return JsonResponse(code_msg=GlobalStatusCode.project_not_exist())