2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework import viewsets
|
|
|
|
|
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
|
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
|
|
2024-10-30 06:54:55 -03:00
|
|
|
from apps.api.permissions import RBACPermission
|
2024-11-19 09:52:23 -03:00
|
|
|
from apps.auth_token.auth import ApiTokenAuthentication, GrafanaServiceAccountAuthentication
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.public_api.serializers.teams import TeamSerializer
|
2023-01-24 13:44:07 +08:00
|
|
|
from apps.public_api.tf_sync import is_request_from_terraform, sync_teams_on_tf_request
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.public_api.throttlers.user_throttle import UserThrottle
|
|
|
|
|
from apps.user_management.models import Team
|
|
|
|
|
from common.api_helpers.mixins import PublicPrimaryKeyMixin
|
|
|
|
|
from common.api_helpers.paginators import FiftyPageSizePaginator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TeamView(PublicPrimaryKeyMixin, RetrieveModelMixin, ListModelMixin, viewsets.GenericViewSet):
|
|
|
|
|
serializer_class = TeamSerializer
|
2024-11-19 09:52:23 -03:00
|
|
|
authentication_classes = (GrafanaServiceAccountAuthentication, ApiTokenAuthentication)
|
2024-10-30 06:54:55 -03:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"list": [RBACPermission.Permissions.USER_SETTINGS_READ],
|
|
|
|
|
"retrieve": [RBACPermission.Permissions.USER_SETTINGS_READ],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
model = Team
|
|
|
|
|
pagination_class = FiftyPageSizePaginator
|
|
|
|
|
throttle_classes = [UserThrottle]
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2023-01-24 13:44:07 +08:00
|
|
|
if is_request_from_terraform(self.request):
|
|
|
|
|
sync_teams_on_tf_request(self.request.auth.organization)
|
2022-06-03 08:09:47 -06:00
|
|
|
name = self.request.query_params.get("name", None)
|
|
|
|
|
queryset = self.request.auth.organization.teams.all()
|
|
|
|
|
if name:
|
|
|
|
|
queryset = queryset.filter(name=name)
|
2023-06-07 12:51:58 +02:00
|
|
|
return queryset.order_by("id")
|