Skip to content

Commit

Permalink
Add RemoveTeamMemberAPIView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 12, 2023
1 parent 31405a1 commit 0c832cb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
8 changes: 7 additions & 1 deletion django/thunderstore/api/cyberstorm/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
NamespacePackageListAPIView,
PackageDependantsListAPIView,
)
from .team import TeamDetailAPIView, TeamMembersAPIView, TeamServiceAccountsAPIView
from .team import (
RemoveTeamMemberAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
)

__all__ = [
"CommunityDetailAPIView",
Expand All @@ -18,4 +23,5 @@
"TeamDetailAPIView",
"TeamMembersAPIView",
"TeamServiceAccountsAPIView",
"RemoveTeamMemberAPIView",
]
58 changes: 56 additions & 2 deletions django/thunderstore/api/cyberstorm/views/team.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.contrib.auth import get_user_model
from django.db.models import Q, QuerySet
from rest_framework.exceptions import PermissionDenied
from rest_framework import serializers
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from thunderstore.account.models.service_account import ServiceAccount
from thunderstore.api.cyberstorm.serializers import (
Expand All @@ -11,9 +15,15 @@
CyberstormTeamSerializer,
)
from thunderstore.api.ordering import StrictOrderingFilter
from thunderstore.api.utils import CyberstormAutoSchemaMixin
from thunderstore.api.utils import (
CyberstormAutoSchemaMixin,
conditional_swagger_auto_schema,
)
from thunderstore.repository.forms import RemoveTeamMemberForm
from thunderstore.repository.models.team import Team, TeamMember

User = get_user_model()


class TeamDetailAPIView(CyberstormAutoSchemaMixin, RetrieveAPIView):
serializer_class = CyberstormTeamSerializer
Expand Down Expand Up @@ -52,6 +62,50 @@ def get_queryset(self) -> QuerySet[TeamMember]:
)


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


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


class RemoveTeamMemberAPIView(APIView):
@conditional_swagger_auto_schema(
request_body=CyberstormRemoveTeamMemberRequestSerialiazer,
responses={200: CyberstormRemoveTeamMemberResponseSerialiazer},
operation_id="cyberstorm.team.members.remove",
tags=["cyberstorm"],
)
def post(self, request, team_name, format=None):
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=request.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": request.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 @@ -7,6 +7,7 @@
CommunityPackageListAPIView,
NamespacePackageListAPIView,
PackageDependantsListAPIView,
RemoveTeamMemberAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
Expand Down Expand Up @@ -53,6 +54,11 @@
TeamMembersAPIView.as_view(),
name="cyberstorm.team.members",
),
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 0c832cb

Please sign in to comment.