From b33cd317699ce537ce08e9b9fe8f0be8fa91f05a Mon Sep 17 00:00:00 2001 From: Oksamies Date: Mon, 11 Dec 2023 16:07:13 +0200 Subject: [PATCH] Add DisbandTeamAPIView --- .../api/cyberstorm/views/__init__.py | 8 +++- .../thunderstore/api/cyberstorm/views/team.py | 47 ++++++++++++++++++- django/thunderstore/api/urls.py | 6 +++ 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/django/thunderstore/api/cyberstorm/views/__init__.py b/django/thunderstore/api/cyberstorm/views/__init__.py index 6df5135e0..d83874a49 100644 --- a/django/thunderstore/api/cyberstorm/views/__init__.py +++ b/django/thunderstore/api/cyberstorm/views/__init__.py @@ -6,7 +6,12 @@ NamespacePackageListAPIView, PackageDependantsListAPIView, ) -from .team import TeamDetailAPIView, TeamMembersAPIView, TeamServiceAccountsAPIView +from .team import ( + DisbandTeamAPIView, + TeamDetailAPIView, + TeamMembersAPIView, + TeamServiceAccountsAPIView, +) __all__ = [ "CommunityDetailAPIView", @@ -16,6 +21,7 @@ "NamespacePackageListAPIView", "PackageDependantsListAPIView", "TeamDetailAPIView", + "DisbandTeamAPIView", "TeamMembersAPIView", "TeamServiceAccountsAPIView", ] diff --git a/django/thunderstore/api/cyberstorm/views/team.py b/django/thunderstore/api/cyberstorm/views/team.py index 63062a64a..204d636ad 100644 --- a/django/thunderstore/api/cyberstorm/views/team.py +++ b/django/thunderstore/api/cyberstorm/views/team.py @@ -1,8 +1,11 @@ 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 ( @@ -11,7 +14,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 @@ -39,6 +46,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, team_name, format=None): + serializer = CyberstormDisbandTeamRequestSerialiazer(data=request.data) + serializer.is_valid(raise_exception=True) + + team = get_object_or_404(Team, name__iexact=team_name) + + form = DisbandTeamForm( + user=request.user, + instance=team, + data=request.data, + ) + + if form.is_valid(): + form.save() + return Response( + CyberstormDisbandTeamResponseSerialiazer({"name": team_name}).data + ) + else: + raise ValidationError(form.errors) + + class TeamMembersAPIView(CyberstormAutoSchemaMixin, TeamRestrictedAPIView): serializer_class = CyberstormTeamMemberSerializer filter_backends = [StrictOrderingFilter] diff --git a/django/thunderstore/api/urls.py b/django/thunderstore/api/urls.py index 5b777b6f6..eab7bb612 100644 --- a/django/thunderstore/api/urls.py +++ b/django/thunderstore/api/urls.py @@ -5,6 +5,7 @@ CommunityFiltersAPIView, CommunityListAPIView, CommunityPackageListAPIView, + DisbandTeamAPIView, NamespacePackageListAPIView, PackageDependantsListAPIView, TeamDetailAPIView, @@ -48,6 +49,11 @@ TeamDetailAPIView.as_view(), name="cyberstorm.team.detail", ), + path( + "team//disband/", + DisbandTeamAPIView.as_view(), + name="cyberstorm.team.disband", + ), path( "team//members/", TeamMembersAPIView.as_view(),