Example #1
0
    def post(self, request):
        serializer = CreateTeamReqSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        team_data = serializer.data
        team_data["creater"] = self.user.user_id
        team_data["enterprise_id"] = self.enterprise.enterprise_id

        en = self.enterprise
        if not en:
            raise serializers.ValidationError("指定企业不存在")
        region = None
        if team_data.get("region", None):
            region = region_services.get_region_by_region_name(
                team_data.get("region"))
            if not region:
                raise ErrRegionNotFound
        try:
            user = user_services.get_user_by_user_id(
                team_data.get("creater", 0))
        except UserNotExistError:
            user = request.user
        team = team_services.create_team(user,
                                         en,
                                         team_alias=team_data["tenant_name"])
        if region:
            region_services.create_tenant_on_region(
                self.enterprise.enterprise_id, team.tenant_name,
                region.region_name)
        re = TeamBaseInfoSerializer(team)
        return Response(re.data, status=status.HTTP_201_CREATED)
Example #2
0
    def post(self, request):
        serializer = CreateTeamReqSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        team_data = serializer.data

        en = enterprise_services.get_enterprise_by_enterprise_id(request.data.get("enterprise_id"))
        if not en:
            raise serializers.ValidationError("指定企业不存在")
        region = None
        if team_data.get("region", None):
            region = region_services.get_region_by_region_name(team_data.get("region"))
            if not region:
                raise serializers.ValidationError("指定数据中心不存在")
        try:
            user = user_services.get_user_by_user_id(team_data.get("creater", 0))
        except UserNotExistError:
            user = request.user
        code, msg, team = team_services.create_team(user, en, team_alias=team_data["tenant_name"])
        if code == 200 and region:
            code, message, bean = region_services.create_tenant_on_region(team.tenant_name, region.region_name)
            if code != 200:
                team.delete()
                raise serializers.ValidationError("数据中心创建团队时发生错误")
        if code == 200:
            re = TeamBaseInfoSerializer(team)
            return Response(re.data, status=status.HTTP_201_CREATED)
        else:
            return Response(None, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #3
0
class ListTeamInfo(BaseOpenAPIView):
    @swagger_auto_schema(
        operation_description="获取用户所在团队列表",
        manual_parameters=[
            openapi.Parameter("query", openapi.IN_QUERY, description="团队名称搜索", type=openapi.TYPE_STRING),
            openapi.Parameter("page", openapi.IN_QUERY, description="页码", type=openapi.TYPE_STRING),
            openapi.Parameter("page_size", openapi.IN_QUERY, description="每页数量", type=openapi.TYPE_STRING),
        ],
        responses={200: ListTeamRespSerializer()},
        tags=['openapi-team'],
    )
    def get(self, req, *args, **kwargs):
        query = req.GET.get("query", "")
        try:
            page = int(req.GET.get("page", 1))
        except ValueError:
            page = 1
        try:
            page_size = int(req.GET.get("page_size", 10))
        except ValueError:
            page_size = 10
        tenants, total = team_services.list_teams_by_user_id(
            eid=self.enterprise.enterprise_id, user_id=req.user.user_id, query=query, page=page, page_size=page_size)
        result = {"tenants": tenants, "total": total, "page": page, "page_size": page_size}
        serializer = ListTeamRespSerializer(data=result)
        serializer.is_valid(raise_exception=True)
        return Response(serializer.data, status.HTTP_200_OK)

    @swagger_auto_schema(
        operation_description="add team",
        request_body=CreateTeamReqSerializer(),
        responses={
            status.HTTP_201_CREATED: TeamBaseInfoSerializer(),
        },
        tags=['openapi-team'],
    )
    def post(self, request):
        serializer = CreateTeamReqSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        team_data = serializer.data
        team_data["creater"] = self.user.user_id
        team_data["enterprise_id"] = self.enterprise.enterprise_id

        en = self.enterprise
        if not en:
            raise serializers.ValidationError("指定企业不存在")
        region = None
        if team_data.get("region", None):
            region = region_services.get_region_by_region_name(team_data.get("region"))
            if not region:
                raise ErrRegionNotFound
        try:
            user = user_services.get_user_by_user_id(team_data.get("creater", 0))
        except UserNotExistError:
            user = request.user
        team = team_services.create_team(user, en, team_alias=team_data["tenant_name"])
        if region:
            region_services.create_tenant_on_region(self.enterprise.enterprise_id, team.tenant_name, region.region_name)
        re = TeamBaseInfoSerializer(team)
        return Response(re.data, status=status.HTTP_201_CREATED)
Example #4
0
class ListRegionsView(ListAPIView):
    @swagger_auto_schema(
        operation_description="获取团队开通的数据中心列表",
        manual_parameters=[
            openapi.Parameter("query", openapi.IN_QUERY, description="根据数据中心名称搜索", type=openapi.TYPE_STRING),
            openapi.Parameter("page", openapi.IN_QUERY, description="页码", type=openapi.TYPE_STRING),
            openapi.Parameter("page_size", openapi.IN_QUERY, description="每页数量", type=openapi.TYPE_STRING),
        ],
        responses={200: ListTeamRegionsRespSerializer()},
        tags=['openapi-team-region'],
    )
    def get(self, req, team_id, *args, **kwargs):
        query = req.GET.get("query", "")
        try:
            page = int(req.GET.get("page", 1))
        except ValueError:
            page = 1
        try:
            page_size = int(req.GET.get("page_size", 10))
        except ValueError:
            page_size = 10

        regions, total = region_services.list_by_tenant_id(team_id, query, page, page_size)

        data = {"regions": regions, "total": total}

        serializer = ListTeamRegionsRespSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        return Response(serializer.data, status.HTTP_200_OK)

    @swagger_auto_schema(
        operation_description="开通数据中心",
        request_body=TeamRegionReqSerializer(),
        responses={
            status.HTTP_201_CREATED: TeamBaseInfoSerializer(),
            status.HTTP_500_INTERNAL_SERVER_ERROR: None,
            status.HTTP_400_BAD_REQUEST: None,
        },
        tags=['openapi-team-region'],
    )
    def post(self, request, team_id):
        serializer = TeamRegionReqSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        team_data = serializer.data

        region = None
        if team_data.get("region", None):
            region = region_services.get_region_by_region_name(team_data.get("region"))
            if not region:
                raise serializers.ValidationError("指定数据中心不存在")
        team = team_services.get_team_by_team_id(team_id)
        code, message, bean = region_services.create_tenant_on_region(team.tenant_name, region.region_name)
        if code != 200:
            raise serializers.ValidationError("数据中心创建团队时发生错误")
        if code == 200:
            re = TeamBaseInfoSerializer(team)
            return Response(re.data, status=status.HTTP_201_CREATED)
        else:
            return Response(None, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #5
0
class ListRegionsView(TeamNoRegionAPIView):
    @swagger_auto_schema(
        operation_description="获取团队开通的数据中心列表",
        manual_parameters=[
            openapi.Parameter("query", openapi.IN_QUERY, description="根据数据中心名称搜索", type=openapi.TYPE_STRING),
            openapi.Parameter("page", openapi.IN_QUERY, description="页码", type=openapi.TYPE_STRING),
            openapi.Parameter("page_size", openapi.IN_QUERY, description="每页数量", type=openapi.TYPE_STRING),
        ],
        responses={200: ListTeamRegionsRespSerializer()},
        tags=['openapi-team-region'],
    )
    def get(self, req, team_id, *args, **kwargs):
        query = req.GET.get("query", "")
        try:
            page = int(req.GET.get("page", 1))
        except ValueError:
            page = 1
        try:
            page_size = int(req.GET.get("page_size", 10))
        except ValueError:
            page_size = 10

        regions, total = region_services.list_by_tenant_id(self.team.tenant_id, query, page, page_size)

        data = {"regions": regions, "total": total}

        serializer = ListTeamRegionsRespSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        return Response(serializer.data, status.HTTP_200_OK)

    @swagger_auto_schema(
        operation_description="开通数据中心",
        request_body=TeamRegionReqSerializer(),
        responses={
            status.HTTP_201_CREATED: TeamBaseInfoSerializer(),
        },
        tags=['openapi-team-region'],
    )
    def post(self, request, team_id):
        serializer = TeamRegionReqSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        team_data = serializer.data

        region = None
        if team_data.get("region", None):
            region = region_services.get_region_by_region_name(team_data.get("region"))
            if not region:
                raise ErrRegionNotFound
        team = team_services.get_team_by_team_id(team_id)
        region_services.create_tenant_on_region(self.enterprise.enterprise_id, team.tenant_name, region.region_name)
        re = TeamBaseInfoSerializer(team)
        return Response(re.data, status=status.HTTP_201_CREATED)
Example #6
0
    def post(self, request, team_id):
        serializer = TeamRegionReqSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        team_data = serializer.data

        region = None
        if team_data.get("region", None):
            region = region_services.get_region_by_region_name(team_data.get("region"))
            if not region:
                raise ErrRegionNotFound
        team = team_services.get_team_by_team_id(team_id)
        region_services.create_tenant_on_region(self.enterprise.enterprise_id, team.tenant_name, region.region_name)
        re = TeamBaseInfoSerializer(team)
        return Response(re.data, status=status.HTTP_201_CREATED)
Example #7
0
 def delete(self, request, team_id):
     serializer = TeamRegionReqSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     team_data = serializer.data
     region = team_data.get("region", None)
     if region:
         region = region_services.get_region_by_region_name(region)
         if not region:
             raise serializers.ValidationError("指定数据中心不存在")
     code, msg, team = team_services.delete_team_region(team_id, region)
     if code == 200:
         re = TeamBaseInfoSerializer(team)
         return Response(re.data, status=status.HTTP_200_OK)
     else:
         return Response(None, status=status.HTTP_404_NOT_FOUND)
Example #8
0
    def post(self, request, team_id):
        serializer = TeamRegionReqSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        team_data = serializer.data

        region = None
        if team_data.get("region", None):
            region = region_services.get_region_by_region_name(team_data.get("region"))
            if not region:
                raise serializers.ValidationError("指定数据中心不存在")
        team = team_services.get_team_by_team_id(team_id)
        code, message, bean = region_services.create_tenant_on_region(team.tenant_name, region.region_name)
        if code != 200:
            raise serializers.ValidationError("数据中心创建团队时发生错误")
        if code == 200:
            re = TeamBaseInfoSerializer(team)
            return Response(re.data, status=status.HTTP_201_CREATED)
        else:
            return Response(None, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #9
0
class ListTeamInfo(ListAPIView):
    @swagger_auto_schema(
        operation_description="获取团队列表",
        manual_parameters=[
            openapi.Parameter("eid", openapi.IN_QUERY, description="企业ID", type=openapi.TYPE_STRING),
            openapi.Parameter("query", openapi.IN_QUERY, description="团队名称搜索", type=openapi.TYPE_STRING),
            openapi.Parameter("tenant_names", openapi.IN_QUERY, description="根据租户名称获取列表", type=openapi.TYPE_STRING),
            openapi.Parameter("page", openapi.IN_QUERY, description="页码", type=openapi.TYPE_STRING),
            openapi.Parameter("page_size", openapi.IN_QUERY, description="每页数量", type=openapi.TYPE_STRING),
        ],
        responses={200: ListTeamRespSerializer()},
        tags=['openapi-team'],
    )
    def get(self, req, *args, **kwargs):
        eid = req.GET.get("eid", "")
        if not eid:
            raise serializers.ValidationError("缺少'eid'字段")
        query = req.GET.get("query", "")
        try:
            page = int(req.GET.get("page", 1))
        except ValueError:
            page = 1
        try:
            page_size = int(req.GET.get("page_size", 10))
        except ValueError:
            page_size = 10
        tenant_names = req.GET.get("tenant_names", "")

        if tenant_names:
            data = team_services.list_by_tenant_names(tenant_names.split(","))
            result = {"tenants": data, "total": len(data)}
        else:
            data, total = team_services.list_teams_v2(
                eid, query=query, page=page, page_size=page_size)
            result = {"tenants": data, "total": total}
        serializer = ListTeamRespSerializer(data=result)
        serializer.is_valid(raise_exception=True)
        return Response(result, status.HTTP_200_OK)

    @swagger_auto_schema(
        operation_description="add team",
        request_body=CreateTeamReqSerializer(),
        responses={
            status.HTTP_201_CREATED: TeamBaseInfoSerializer(),
            status.HTTP_500_INTERNAL_SERVER_ERROR: None,
            status.HTTP_400_BAD_REQUEST: None,
        },
        tags=['openapi-team'],
    )
    def post(self, request):
        serializer = CreateTeamReqSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        team_data = serializer.data

        en = enterprise_services.get_enterprise_by_enterprise_id(request.data.get("enterprise_id"))
        if not en:
            raise serializers.ValidationError("指定企业不存在")
        region = None
        if team_data.get("region", None):
            region = region_services.get_region_by_region_name(team_data.get("region"))
            if not region:
                raise serializers.ValidationError("指定数据中心不存在")
        try:
            user = user_services.get_user_by_user_id(team_data.get("creater", 0))
        except UserNotExistError:
            user = request.user
        code, msg, team = team_services.create_team(user, en, team_alias=team_data["tenant_name"])
        if code == 200 and region:
            code, message, bean = region_services.create_tenant_on_region(team.tenant_name, region.region_name)
            if code != 200:
                team.delete()
                raise serializers.ValidationError("数据中心创建团队时发生错误")
        if code == 200:
            re = TeamBaseInfoSerializer(team)
            return Response(re.data, status=status.HTTP_201_CREATED)
        else:
            return Response(None, status=status.HTTP_500_INTERNAL_SERVER_ERROR)