def get_enterprise_user_active_teams(self, enterprise_id, user_id): tenants = self.get_enterprise_user_teams(enterprise_id, user_id) if not tenants: return None active_tenants_list = [] for tenant in tenants: user = user_repo.get_user_by_user_id(tenant.creater) try: role = user_role_repo.get_role_names(user.user_id, tenant.tenant_id) except UserRoleNotFoundException: if tenant.creater == user.user_id: role = "owner" else: role = None region_name_list = [] user = user_repo.get_user_by_user_id(tenant.creater) region_list = team_repo.get_team_regions(tenant.tenant_id) if region_list: region_name_list = region_list.values_list("region_name", flat=True) active_tenants_list.append({ "tenant_id": tenant.tenant_id, "team_alias": tenant.tenant_alias, "owner": tenant.creater, "owner_name": user.get_name(), "enterprise_id": tenant.enterprise_id, "create_time": tenant.create_time, "team_name": tenant.tenant_name, "region": tenant.region, "region_list": region_name_list, "num": len(ServiceGroup.objects.filter(tenant_id=tenant.tenant_id)), "role": role }) active_tenants_list.sort(key=lambda x: x["num"], reverse=True) active_tenants_list = active_tenants_list[:3] return active_tenants_list
def __team_with_region_info(self, tenant, user_id=None): role = "" owner_name = "" try: user = user_repo.get_user_by_user_id(tenant.creater) owner_name = user.get_name() if user_id: role = user_role_repo.get_role_names(user_id, tenant.tenant_id) except UserNotExistError: pass except UserRoleNotFoundException: if tenant.creater == user_id: role = "owner" region_info_map = [] region_list = team_repo.get_team_regions(tenant.tenant_id) if region_list: region_name_list = region_list.values_list("region_name", flat=True) region_infos = region_repo.get_region_by_region_names( region_name_list) if region_infos: for region in region_infos: region_info_map.append({ "region_name": region.region_name, "region_alias": region.region_alias }) info = { "team_name": tenant.tenant_name, "team_alias": tenant.tenant_alias, "team_id": tenant.tenant_id, "create_time": tenant.create_time, "region": tenant.region, "region_list": region_info_map, "enterprise_id": tenant.enterprise_id, "owner": tenant.creater, "owner_name": owner_name, } if user_id: info["role"] = role return info
def __team_with_region_info(self, tenant, request_user=None): try: user = user_repo.get_user_by_user_id(tenant.creater) owner_name = user.get_name() except UserNotExistError: owner_name = None if request_user: user_role_list = user_kind_role_service.get_user_roles( kind="team", kind_id=tenant.tenant_id, user=request_user) roles = map(lambda x: x["role_name"], user_role_list["roles"]) if tenant.creater == request_user.user_id: roles.append("owner") region_info_map = [] region_list = team_repo.get_team_regions(tenant.tenant_id) if region_list: region_name_list = region_list.values_list("region_name", flat=True) region_infos = region_repo.get_region_by_region_names( region_name_list) if region_infos: for region in region_infos: region_info_map.append({ "region_name": region.region_name, "region_alias": region.region_alias }) info = { "team_name": tenant.tenant_name, "team_alias": tenant.tenant_alias, "team_id": tenant.tenant_id, "create_time": tenant.create_time, "region": tenant.region, "region_list": region_info_map, "enterprise_id": tenant.enterprise_id, "owner": tenant.creater, "owner_name": owner_name, } if request_user: info["roles"] = roles return info
def get(self, request, enterprise_id, *args, **kwargs): code = 200 new_join_team = [] request_join_team = [] try: tenants = enterprise_repo.get_enterprise_user_teams( enterprise_id, request.user.user_id) join_tenants = enterprise_repo.get_enterprise_user_join_teams( enterprise_id, request.user.user_id) active_tenants = enterprise_repo.get_enterprise_user_active_teams( enterprise_id, request.user.user_id) request_tenants = enterprise_repo.get_enterprise_user_request_join( enterprise_id, request.user.user_id) if tenants: for tenant in tenants[:3]: region_name_list = [] user = user_repo.get_user_by_user_id(tenant.creater) region_list = team_repo.get_team_regions(tenant.tenant_id) if region_list: region_name_list = region_list.values_list( "region_name", flat=True) try: role = user_role_repo.get_role_names( user.user_id, tenant.tenant_id) except UserRoleNotFoundException: if tenant.creater == user.user_id: role = "owner" else: role = None new_join_team.append({ "team_name": tenant.tenant_name, "team_alias": tenant.tenant_alias, "team_id": tenant.tenant_id, "create_time": tenant.create_time, "region": tenant.region, "region_list": region_name_list, "enterprise_id": tenant.enterprise_id, "owner": tenant.creater, "owner_name": user.nick_name, "role": role, "is_pass": True, }) if join_tenants: for tenant in join_tenants: region_name_list = [] region_list = team_repo.get_team_regions(tenant.team_id) if region_list: region_name_list = region_list.values_list( "region_name", flat=True) tenant_info = team_repo.get_team_by_team_id(tenant.team_id) try: user = user_repo.get_user_by_user_id( tenant_info.creater) nick_name = user.nick_name except UserNotExistError: nick_name = None new_join_team.append({ "team_name": tenant.team_name, "team_alias": tenant.team_alias, "team_id": tenant.team_id, "create_time": tenant_info.create_time, "region": tenant_info.region, "region_list": region_name_list, "enterprise_id": tenant_info.enterprise_id, "owner": tenant_info.creater, "owner_name": nick_name, "role": None, "is_pass": tenant.is_pass, }) if request_tenants: for request_tenant in request_tenants: region_name_list = [] region_list = team_repo.get_team_regions( request_tenant.team_id) if region_list: region_name_list = region_list.values_list( "region_name", flat=True) tenant_info = team_repo.get_team_by_team_id( request_tenant.team_id) try: user = user_repo.get_user_by_user_id( tenant_info.creater) nick_name = user.nick_name except UserNotExistError: nick_name = None request_join_team.append({ "team_name": request_tenant.team_name, "team_alias": request_tenant.team_alias, "team_id": request_tenant.team_id, "apply_time": request_tenant.apply_time, "user_id": request_tenant.user_id, "user_name": request_tenant.user_name, "region": team_repo.get_team_by_team_id( request_tenant.team_id).region, "region_list": region_name_list, "enterprise_id": enterprise_id, "owner": tenant_info.creater, "owner_name": nick_name, "role": "viewer", "is_pass": request_tenant.is_pass, }) data = { "active_teams": active_tenants, "new_join_team": new_join_team, "request_join_team": request_join_team, } result = general_message(200, "success", None, bean=data) except Exception as e: logger.exception(e) code = 400 result = general_message(code, "failed", "请求失败") return Response(result, status=code)