Skip to content

Commit

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

__all__ = [
"CommunityDetailAPIView",
Expand All @@ -16,6 +21,7 @@
"NamespacePackageListAPIView",
"PackageDependantsListAPIView",
"TeamDetailAPIView",
"DisbandTeamAPIView",
"TeamMembersAPIView",
"TeamServiceAccountsAPIView",
]
48 changes: 46 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.db.models import Q, QuerySet
from rest_framework.exceptions import PermissionDenied
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
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,7 +15,11 @@
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 DisbandTeamForm
from thunderstore.repository.models.team import Team, TeamMember


Expand Down Expand Up @@ -39,6 +47,42 @@ def check_permissions(self, request: Request) -> None:
raise PermissionDenied()


class CyberstormDisbandTeamRequestSerialiazer(serializers.Serializer):
verification = serializers.CharField()


class CyberstormDisbandTeamResponseSerialiazer(serializers.Serializer):
name = serializers.CharField()


class DisbandTeamAPIView(APIView):
@conditional_swagger_auto_schema(
request_body=CyberstormDisbandTeamRequestSerialiazer,
responses={200: CyberstormDisbandTeamResponseSerialiazer},
operation_id="cyberstorm.team.disband",
tags=["cyberstorm"],
)
def post(self, request: HttpRequest, team_name: str):
serializer = CyberstormDisbandTeamRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)

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

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L66-L67

Added lines #L66 - L67 were not covered by tests

team = get_object_or_404(Team, name__iexact=team_name)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L69 was not covered by tests

form = DisbandTeamForm(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L71 was not covered by tests
user=request.user,
instance=team,
data=serializer.validated_data,
)

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

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

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L78-L79

Added lines #L78 - L79 were not covered by tests
CyberstormDisbandTeamResponseSerialiazer({"name": team_name}).data
)
else:
raise ValidationError(form.errors)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L83 was not covered by tests


class TeamMembersAPIView(CyberstormAutoSchemaMixin, TeamRestrictedAPIView):
serializer_class = CyberstormTeamMemberSerializer
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,
DisbandTeamAPIView,
NamespacePackageListAPIView,
PackageDependantsListAPIView,
TeamDetailAPIView,
Expand Down Expand Up @@ -48,6 +49,11 @@
TeamDetailAPIView.as_view(),
name="cyberstorm.team.detail",
),
path(
"team/<str:team_name>/disband/",
DisbandTeamAPIView.as_view(),
name="cyberstorm.team.disband",
),
path(
"team/<str:team_id>/members/",
TeamMembersAPIView.as_view(),
Expand Down

0 comments on commit e014d90

Please sign in to comment.