2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework import mixins, viewsets
|
2023-03-30 16:34:55 +01:00
|
|
|
from rest_framework.filters import SearchFilter
|
2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2023-04-19 16:22:14 +01:00
|
|
|
from rest_framework.response import Response
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2023-03-22 15:25:30 +08:00
|
|
|
from apps.api.permissions import RBACPermission
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.api.serializers.team import TeamSerializer
|
2022-11-23 15:56:43 +00:00
|
|
|
from apps.auth_token.auth import PluginAuthentication
|
|
|
|
|
from apps.mobile_app.auth import MobileAppAuthTokenAuthentication
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.user_management.models import Team
|
2023-03-22 00:57:20 +08:00
|
|
|
from common.api_helpers.mixins import PublicPrimaryKeyMixin
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
2023-03-22 00:57:20 +08:00
|
|
|
class TeamViewSet(PublicPrimaryKeyMixin, mixins.ListModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet):
|
2022-06-03 08:09:47 -06:00
|
|
|
authentication_classes = (
|
|
|
|
|
MobileAppAuthTokenAuthentication,
|
|
|
|
|
PluginAuthentication,
|
|
|
|
|
)
|
2023-03-22 15:25:30 +08:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"list": [RBACPermission.Permissions.OTHER_SETTINGS_READ],
|
|
|
|
|
"retrieve": [RBACPermission.Permissions.OTHER_SETTINGS_READ],
|
|
|
|
|
"update": [RBACPermission.Permissions.OTHER_SETTINGS_WRITE],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
serializer_class = TeamSerializer
|
2023-03-30 16:34:55 +01:00
|
|
|
filter_backends = [SearchFilter]
|
|
|
|
|
search_fields = ["name"]
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2023-03-22 00:57:20 +08:00
|
|
|
return self.request.user.available_teams
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2023-04-19 16:22:14 +01:00
|
|
|
def list(self, request, *args, **kwargs):
|
2023-03-30 16:34:55 +01:00
|
|
|
"""
|
|
|
|
|
Adds general team to the queryset in a way that it always shows up first (even when not searched for).
|
|
|
|
|
"""
|
2023-03-22 00:57:20 +08:00
|
|
|
general_team = Team(public_primary_key="null", name="No team", email=None, avatar_url=None)
|
2023-04-19 16:22:14 +01:00
|
|
|
queryset = [general_team] + list(self.filter_queryset(self.get_queryset()))
|
|
|
|
|
|
|
|
|
|
serializer = self.get_serializer(queryset, many=True)
|
|
|
|
|
return Response(serializer.data)
|