From 6049fba8ad5f8c5f0f592474b2f35b0b8418d060 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 | 2 + .../thunderstore/api/cyberstorm/views/team.py | 41 ++++++++++++++++++- django/thunderstore/api/urls.py | 6 +++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/django/thunderstore/api/cyberstorm/views/__init__.py b/django/thunderstore/api/cyberstorm/views/__init__.py index 909dafcbb..a30fdf799 100644 --- a/django/thunderstore/api/cyberstorm/views/__init__.py +++ b/django/thunderstore/api/cyberstorm/views/__init__.py @@ -11,6 +11,7 @@ ) from .team import ( AddTeamMemberAPIView, + DisbandTeamAPIView, TeamDetailAPIView, TeamMembersAPIView, TeamServiceAccountsAPIView, @@ -29,6 +30,7 @@ "PackageVersionsAPIView", "TeamDetailAPIView", "AddTeamMemberAPIView", + "DisbandTeamAPIView", "TeamMembersAPIView", "TeamServiceAccountsAPIView", ] diff --git a/django/thunderstore/api/cyberstorm/views/team.py b/django/thunderstore/api/cyberstorm/views/team.py index 194a4187f..363670c21 100644 --- a/django/thunderstore/api/cyberstorm/views/team.py +++ b/django/thunderstore/api/cyberstorm/views/team.py @@ -1,5 +1,6 @@ from django.contrib.auth import get_user_model from django.db.models import Q, QuerySet +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 @@ -19,7 +20,7 @@ CyberstormAutoSchemaMixin, conditional_swagger_auto_schema, ) -from thunderstore.repository.forms import AddTeamMemberForm +from thunderstore.repository.forms import AddTeamMemberForm, DisbandTeamForm from thunderstore.repository.models.team import Team, TeamMember User = get_user_model() @@ -49,6 +50,44 @@ 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): + permission_classes = [IsAuthenticated] + + @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) + + team = get_object_or_404(Team, name__iexact=team_name) + + form = DisbandTeamForm( + user=request.user, + instance=team, + data=serializer.validated_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 4437f6e60..ba8b312b2 100644 --- a/django/thunderstore/api/urls.py +++ b/django/thunderstore/api/urls.py @@ -6,6 +6,7 @@ CommunityFiltersAPIView, CommunityListAPIView, CommunityPackageListAPIView, + DisbandTeamAPIView, NamespacePackageListAPIView, PackageDependantsListAPIView, PackageDetailAPIView, @@ -58,6 +59,11 @@ TeamDetailAPIView.as_view(), name="cyberstorm.team.detail", ), + path( + "team//disband/", + DisbandTeamAPIView.as_view(), + name="cyberstorm.team.disband", + ), path( "team//members/", TeamMembersAPIView.as_view(),