Skip to content

Commit

Permalink
Add RemoveTeamMemberAPIView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 11, 2023
1 parent 31405a1 commit 909104b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 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",
]
56 changes: 55 additions & 1 deletion django/thunderstore/api/cyberstorm/views/team.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from django.contrib.auth import get_user_model
from django.db.models import Q, QuerySet
from rest_framework.exceptions import PermissionDenied
from drf_yasg.utils import swagger_auto_schema
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 @@ -12,8 +17,11 @@
)
from thunderstore.api.ordering import StrictOrderingFilter
from thunderstore.api.utils import CyberstormAutoSchemaMixin
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 +60,52 @@ def get_queryset(self) -> QuerySet[TeamMember]:
)


class CyberstormRemoveTeamMemberRequestSerialiazer(serializers.Serializer):
username = serializers.CharField(
max_length=User._meta.get_field("username").max_length
)


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


class RemoveTeamMemberAPIView(APIView):
@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)

Check warning on line 82 in django/thunderstore/api/cyberstorm/views/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L82

Added line #L82 was not covered by tests

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

Check warning on line 85 in django/thunderstore/api/cyberstorm/views/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L84-L85

Added lines #L84 - L85 were not covered by tests

team_member = get_object_or_404(

Check warning on line 87 in django/thunderstore/api/cyberstorm/views/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L87

Added line #L87 was not covered by tests
TeamMember, user__username__iexact=request.data["username"], team=team
)

membership = team.get_membership_for_user(team_member.user)

Check warning on line 91 in django/thunderstore/api/cyberstorm/views/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L91

Added line #L91 was not covered by tests

form = RemoveTeamMemberForm(

Check warning on line 93 in django/thunderstore/api/cyberstorm/views/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L93

Added line #L93 was not covered by tests
user=request.user,
data={"membership": membership},
)

if form.is_valid():
form.save()
return Response(

Check warning on line 100 in django/thunderstore/api/cyberstorm/views/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L99-L100

Added lines #L99 - L100 were not covered by tests
CyberstormRemoveTeamMemberResponseSerialiazer(
{"username": request.data["username"], "team_name": team_name}
).data
)
else:
raise ValidationError(form.errors)

Check warning on line 106 in django/thunderstore/api/cyberstorm/views/team.py

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L106

Added line #L106 was not covered by tests


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 909104b

Please sign in to comment.