# What this PR does Team search doesn't work when filtering by team: <img width="331" alt="Screenshot 2023-03-30 at 15 48 50" src="https://user-images.githubusercontent.com/20116910/228875971-0f55bdf8-6aa7-4759-9882-cdf7d11bb0c7.png"> This PR fixes it + adds backend unit tests. ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required)
37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
from rest_framework import mixins, viewsets
|
|
from rest_framework.filters import SearchFilter
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
from apps.api.permissions import RBACPermission
|
|
from apps.api.serializers.team import TeamSerializer
|
|
from apps.auth_token.auth import PluginAuthentication
|
|
from apps.mobile_app.auth import MobileAppAuthTokenAuthentication
|
|
from apps.user_management.models import Team
|
|
from common.api_helpers.mixins import PublicPrimaryKeyMixin
|
|
|
|
|
|
class TeamViewSet(PublicPrimaryKeyMixin, mixins.ListModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet):
|
|
authentication_classes = (
|
|
MobileAppAuthTokenAuthentication,
|
|
PluginAuthentication,
|
|
)
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
rbac_permissions = {
|
|
"list": [RBACPermission.Permissions.OTHER_SETTINGS_READ],
|
|
"retrieve": [RBACPermission.Permissions.OTHER_SETTINGS_READ],
|
|
"update": [RBACPermission.Permissions.OTHER_SETTINGS_WRITE],
|
|
}
|
|
|
|
serializer_class = TeamSerializer
|
|
filter_backends = [SearchFilter]
|
|
search_fields = ["name"]
|
|
|
|
def get_queryset(self):
|
|
return self.request.user.available_teams
|
|
|
|
def filter_queryset(self, queryset):
|
|
"""
|
|
Adds general team to the queryset in a way that it always shows up first (even when not searched for).
|
|
"""
|
|
general_team = Team(public_primary_key="null", name="No team", email=None, avatar_url=None)
|
|
return [general_team] + list(super().filter_queryset(queryset))
|