Skip to content

Commit

Permalink
Add RemoveTeamMemberAPIView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 13, 2023
1 parent f2a2960 commit c09de58
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django/thunderstore/api/cyberstorm/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from .team import (
AddTeamMemberAPIView,
RemoveTeamMemberAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
Expand All @@ -31,4 +32,5 @@
"AddTeamMemberAPIView",
"TeamMembersAPIView",
"TeamServiceAccountsAPIView",
"RemoveTeamMemberAPIView",
]
54 changes: 53 additions & 1 deletion django/thunderstore/api/cyberstorm/views/team.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth import get_user_model
from django.db.models import Q, QuerySet
from django.http import HttpRequest
from rest_framework import serializers
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
Expand All @@ -19,7 +20,7 @@
CyberstormAutoSchemaMixin,
conditional_swagger_auto_schema,
)
from thunderstore.repository.forms import AddTeamMemberForm
from thunderstore.repository.forms import AddTeamMemberForm, RemoveTeamMemberForm
from thunderstore.repository.models.team import Team, TeamMember

User = get_user_model()
Expand Down Expand Up @@ -105,6 +106,57 @@ def post(self, request, team_name, format=None):
raise ValidationError(form.errors)


class CyberstormRemoveTeamMemberRequestSerialiazer(serializers.Serializer):
username = serializers.CharField()


class CyberstormRemoveTeamMemberResponseSerialiazer(serializers.Serializer):
username = serializers.CharField()
team_name = serializers.CharField()


class RemoveTeamMemberAPIView(APIView):
permission_classes = [IsAuthenticated]

@conditional_swagger_auto_schema(
request_body=CyberstormRemoveTeamMemberRequestSerialiazer,
responses={200: CyberstormRemoveTeamMemberResponseSerialiazer},
operation_id="cyberstorm.team.members.remove",
tags=["cyberstorm"],
)
def post(self, request: HttpRequest, team_name: str):
team = get_object_or_404(Team, name__iexact=team_name)

serializer = CyberstormRemoveTeamMemberRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)

team_member = get_object_or_404(
TeamMember,
user__username__iexact=serializer.validated_data["username"],
team=team,
)

membership = team.get_membership_for_user(team_member.user)

form = RemoveTeamMemberForm(
user=request.user,
data={"membership": membership},
)

if form.is_valid():
form.save()
return Response(
CyberstormRemoveTeamMemberResponseSerialiazer(
{
"username": serializer.validated_data["username"],
"team_name": team_name,
}
).data
)
else:
raise ValidationError(form.errors)


class TeamServiceAccountsAPIView(CyberstormAutoSchemaMixin, TeamRestrictedAPIView):
serializer_class = CyberstormServiceAccountSerializer
filter_backends = [StrictOrderingFilter]
Expand Down
6 changes: 6 additions & 0 deletions django/thunderstore/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
PackageVersionChangelogAPIView,
PackageVersionReadmeAPIView,
PackageVersionsAPIView,
RemoveTeamMemberAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
Expand Down Expand Up @@ -68,6 +69,11 @@
AddTeamMemberAPIView.as_view(),
name="cyberstorm.team.members.add",
),
path(
"team/<str:team_name>/members/remove/",
RemoveTeamMemberAPIView.as_view(),
name="cyberstorm.team.members.remove",
),
path(
"team/<str:team_id>/service-accounts/",
TeamServiceAccountsAPIView.as_view(),
Expand Down

0 comments on commit c09de58

Please sign in to comment.