Skip to content

Commit

Permalink
Add EditTeamMemberAPIView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 12, 2023
1 parent 31405a1 commit b6bca11
Show file tree
Hide file tree
Showing 3 changed files with 70 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 (
EditTeamMemberAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
)

__all__ = [
"CommunityDetailAPIView",
Expand All @@ -17,5 +22,6 @@
"PackageDependantsListAPIView",
"TeamDetailAPIView",
"TeamMembersAPIView",
"EditTeamMemberAPIView",
"TeamServiceAccountsAPIView",
]
59 changes: 57 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 EditTeamMemberForm
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,51 @@ def get_queryset(self) -> QuerySet[TeamMember]:
)


class CyberstormEditTeamMemberRequestSerialiazer(serializers.Serializer):
username = serializers.CharField()
role = serializers.ChoiceField(
choices=EditTeamMemberForm.base_fields["role"].choices
)


class CyberstormEditTeamMemberResponseSerialiazer(serializers.Serializer):
username = serializers.CharField(source="user")
role = serializers.ChoiceField(
choices=EditTeamMemberForm.base_fields["role"].choices
)
team_name = serializers.CharField(source="team")


class EditTeamMemberAPIView(APIView):
@conditional_swagger_auto_schema(
request_body=CyberstormEditTeamMemberRequestSerialiazer,
responses={200: CyberstormEditTeamMemberResponseSerialiazer},
operation_id="cyberstorm.team.members.edit",
tags=["cyberstorm"],
)
def post(self, request, team_name, format=None):
serializer = CyberstormEditTeamMemberRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)
team_member = get_object_or_404(
TeamMember,
user__username__iexact=request.data["username"],
team__name__iexact=team_name,
)
form = EditTeamMemberForm(
user=request.user,
instance=team_member,
data=request.data,
)

if form.is_valid():
team_member = form.save()
return Response(
CyberstormEditTeamMemberResponseSerialiazer(team_member).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 @@ -5,6 +5,7 @@
CommunityFiltersAPIView,
CommunityListAPIView,
CommunityPackageListAPIView,
EditTeamMemberAPIView,
NamespacePackageListAPIView,
PackageDependantsListAPIView,
TeamDetailAPIView,
Expand Down Expand Up @@ -53,6 +54,11 @@
TeamMembersAPIView.as_view(),
name="cyberstorm.team.members",
),
path(
"team/<str:team_name>/members/edit/",
EditTeamMemberAPIView.as_view(),
name="cyberstorm.team.members.edit",
),
path(
"team/<str:team_id>/service-accounts/",
TeamServiceAccountsAPIView.as_view(),
Expand Down

0 comments on commit b6bca11

Please sign in to comment.