def post(self, request): """ 新增项目 :param request: :return: """ data = JSONParser().parse(request) result = self.parameter_check(data) if result: return result data['user'] = request.user.pk project_serializer = ProjectDeserializer(data=data) try: Project.objects.get(name=data['name']) return JsonResponse(code='1002', msg='存在相同的项目名称!') except ObjectDoesNotExist: with transaction.atomic(): if project_serializer.is_valid(): # 保存项目 project_serializer.save() # 记录动态 record_dynamic(project=project_serializer.data.get("id"), _type='添加', operationObject='项目', user=request.user.pk, data=data['name']) # 创建项目的用户添加为该项目的成员 self.add_project_member(project_serializer.data.get('id'), request.user.pk) return JsonResponse( data={'project_id': project_serializer.data.get('id')}, code='0', msg='新增项目成功!') else: return JsonResponse(code='1001', msg='新增项目失败!')
def post(self, request): """ 启用项目 :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='1019', msg=str(obj) + '无权限操作该项目!') obj.status = True obj.save() record_dynamic(project=data['project_id'], _type='禁用', operationObject='项目', user=request.user.pk, data=obj.name) return JsonResponse(code='0', msg='启用成功') except ObjectDoesNotExist: return JsonResponse(code='1004', msg='项目不存在!')
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='1019', msg='无该项目操作权限!') except ObjectDoesNotExist: return JsonResponse(code='1004', msg='项目不存在!') pro_data = ProjectSerializer(obj) if not pro_data.data['status']: return JsonResponse(code='1020', msg='该项目已禁用!') obi = GlobalHost.objects.filter(name=data['name'], project=data['project_id']) if obi: return JsonResponse(code='1006', msg='host已存在!') 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='0', msg='创建成功!') return JsonResponse(code='1001', msg='创建失败!')
def post(self, request): """ 修改项目 :param request: :return: """ data = JSONParser().parse(request) result = self.paramter_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='1019', msg='无该项目操作权限!') except ObjectDoesNotExist: return JsonResponse(code='1004', msg='项目不存在!') # 查找是否存在相同名称的项目 pro_name = Project.objects.filter(name=data['name']).exclude( id=data['project_id']) if len(pro_name): return JsonResponse(code='1005', msg='项目已存在') else: serializer = ProjectDeserializer(data=data) with transaction.atomic(): if serializer.is_valid(): # 修改项目 serializer.update(instance=obj, validated_data=data) # 记录动态 record_dynamic(project=data['project_id'], _type='修改', operationObject='项目', user=request.user.pk, data=data['name']) return JsonResponse(code='0', msg='修改项目成功!') else: return JsonResponse(code='1001', msg='修改项目失败!')