-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(access): Add Team Users API endpoint
- Loading branch information
Showing
4 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
from rest_framework.reverse import reverse | ||
|
||
from rest_framework import serializers | ||
from rest_framework.exceptions import ParseError | ||
|
||
from access.models import TeamUsers | ||
from app.serializers.user import UserBaseSerializer | ||
|
||
|
||
|
||
class TeamUserBaseSerializer(serializers.ModelSerializer): | ||
|
||
|
||
display_name = serializers.SerializerMethodField('get_display_name') | ||
|
||
def get_display_name(self, item): | ||
|
||
return str( item ) | ||
|
||
url = serializers.SerializerMethodField('get_url') | ||
|
||
def get_url(self, item): | ||
|
||
return reverse( | ||
"API:_api_v2_organization_team_user-detail", | ||
request=self.context['view'].request, | ||
kwargs={ | ||
'organization_id': item.team.organization.id, | ||
'team_id': item.team.id, | ||
'pk': item.pk | ||
} | ||
) | ||
|
||
|
||
class Meta: | ||
|
||
model = TeamUsers | ||
|
||
fields = [ | ||
'id', | ||
'display_name', | ||
'url', | ||
] | ||
|
||
read_only_fields = [ | ||
'id', | ||
'display_name', | ||
'url', | ||
] | ||
|
||
|
||
|
||
class TeamUserModelSerializer(TeamUserBaseSerializer): | ||
|
||
_urls = serializers.SerializerMethodField('get_url') | ||
|
||
def get_url(self, item): | ||
|
||
return { | ||
'_self': reverse( | ||
'API:_api_v2_organization_team_user-detail', | ||
request=self.context['view'].request, | ||
kwargs={ | ||
'organization_id': item.team.organization.id, | ||
'team_id': item.team.id, | ||
'pk': item.pk | ||
} | ||
) | ||
} | ||
|
||
|
||
class Meta: | ||
|
||
model = TeamUsers | ||
|
||
fields = [ | ||
'id', | ||
'display_name', | ||
'manager', | ||
'user', | ||
'created', | ||
'modified', | ||
'_urls', | ||
] | ||
|
||
read_only_fields = [ | ||
'id', | ||
'display_name', | ||
'created', | ||
'modified', | ||
'_urls', | ||
] | ||
|
||
|
||
|
||
def is_valid(self, *, raise_exception=True) -> bool: | ||
|
||
is_valid = False | ||
|
||
try: | ||
|
||
is_valid = super().is_valid(raise_exception=raise_exception) | ||
|
||
self.validated_data['team_id'] = int(self._context['view'].kwargs['team_id']) | ||
|
||
except Exception as unhandled_exception: | ||
|
||
ParseError( | ||
detail=f"Server encountered an error during validation, Traceback: {unhandled_exception.with_traceback}" | ||
) | ||
|
||
return is_valid | ||
|
||
|
||
|
||
class TeamUserViewSerializer(TeamUserModelSerializer): | ||
|
||
user = UserBaseSerializer(read_only = True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiResponse | ||
|
||
from access.serializers.team_user import ( | ||
TeamUsers, | ||
TeamUserModelSerializer, | ||
TeamUserViewSerializer | ||
) | ||
|
||
from api.viewsets.common import ModelViewSet | ||
|
||
|
||
|
||
@extend_schema_view( | ||
create=extend_schema( | ||
summary = 'Create a user within this team', | ||
description='', | ||
responses = { | ||
# 200: OpenApiResponse(description='Allready exists', response=TeamUserViewSerializer), | ||
201: OpenApiResponse(description='Created', response=TeamUserViewSerializer), | ||
# 400: OpenApiResponse(description='Validation failed.'), | ||
403: OpenApiResponse(description='User is missing add permissions'), | ||
} | ||
), | ||
destroy = extend_schema( | ||
summary = 'Delete a user from this team', | ||
description = '', | ||
responses = { | ||
204: OpenApiResponse(description=''), | ||
403: OpenApiResponse(description='User is missing delete permissions'), | ||
} | ||
), | ||
list = extend_schema( | ||
summary = 'Fetch all users from this team', | ||
description='', | ||
responses = { | ||
200: OpenApiResponse(description='', response=TeamUserViewSerializer), | ||
403: OpenApiResponse(description='User is missing view permissions'), | ||
} | ||
), | ||
retrieve = extend_schema( | ||
summary = 'Fetch a single user from this team', | ||
description='', | ||
responses = { | ||
200: OpenApiResponse(description='', response=TeamUserViewSerializer), | ||
403: OpenApiResponse(description='User is missing view permissions'), | ||
} | ||
), | ||
update = extend_schema(exclude = True), | ||
partial_update = extend_schema( | ||
summary = 'Update a user within this team', | ||
description = '', | ||
responses = { | ||
200: OpenApiResponse(description='', response=TeamUserViewSerializer), | ||
# 201: OpenApiResponse(description='Created', response=OrganizationViewSerializer), | ||
# # 400: OpenApiResponse(description='Validation failed.'), | ||
403: OpenApiResponse(description='User is missing change permissions'), | ||
} | ||
), | ||
) | ||
class ViewSet( ModelViewSet ): | ||
|
||
filterset_fields = [ | ||
'manager', | ||
'team__organization', | ||
] | ||
|
||
search_fields = [] | ||
|
||
model = TeamUsers | ||
|
||
documentation: str = '' | ||
|
||
view_description = 'Users belonging to a single team' | ||
|
||
def get_queryset(self): | ||
|
||
queryset = super().get_queryset() | ||
|
||
queryset = queryset.filter( | ||
team_id = self.kwargs['team_id'] | ||
) | ||
|
||
self.queryset = queryset | ||
|
||
return self.queryset | ||
|
||
|
||
def get_serializer_class(self): | ||
|
||
if ( | ||
self.action == 'list' | ||
or self.action == 'retrieve' | ||
): | ||
|
||
return globals()[str( self.model._meta.verbose_name).replace(' ', '') + 'ViewSerializer'] | ||
|
||
|
||
return globals()[str( self.model._meta.verbose_name).replace(' ', '') + 'ModelSerializer'] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters