diff --git a/django/thunderstore/api/cyberstorm/views/__init__.py b/django/thunderstore/api/cyberstorm/views/__init__.py index 909dafcbb..2571ed699 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, + TeamCreateAPIView, TeamDetailAPIView, TeamMembersAPIView, TeamServiceAccountsAPIView, @@ -29,6 +30,7 @@ "PackageVersionsAPIView", "TeamDetailAPIView", "AddTeamMemberAPIView", + "TeamCreateAPIView", "TeamMembersAPIView", "TeamServiceAccountsAPIView", ] diff --git a/django/thunderstore/api/cyberstorm/views/team.py b/django/thunderstore/api/cyberstorm/views/team.py index 194a4187f..c91498c4a 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, CreateTeamForm from thunderstore.repository.models.team import Team, TeamMember User = get_user_model() @@ -49,6 +50,40 @@ def check_permissions(self, request: Request) -> None: raise PermissionDenied() +class CyberstormTeamCreateRequestSerialiazer(serializers.Serializer): + name = serializers.CharField( + max_length=Team._meta.get_field("name").max_length, + validators=Team._meta.get_field("name").validators, + ) + + +class CyberstormTeamCreateResponseSerialiazer(serializers.Serializer): + name = serializers.CharField() + + +class TeamCreateAPIView(APIView): + @conditional_swagger_auto_schema( + request_body=CyberstormTeamCreateRequestSerialiazer, + responses={200: CyberstormTeamCreateResponseSerialiazer}, + operation_id="cyberstorm.teams.create", + tags=["cyberstorm"], + ) + def post(self, request: HttpRequest): + serializer = CyberstormTeamCreateRequestSerialiazer(data=request.data) + serializer.is_valid(raise_exception=True) + + form = CreateTeamForm( + user=request.user, + data=serializer.validated_data, + ) + + if form.is_valid(): + team = form.save() + return Response(CyberstormTeamCreateResponseSerialiazer(team).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..b7ae87619 100644 --- a/django/thunderstore/api/urls.py +++ b/django/thunderstore/api/urls.py @@ -12,6 +12,7 @@ PackageVersionChangelogAPIView, PackageVersionReadmeAPIView, PackageVersionsAPIView, + TeamCreateAPIView, TeamDetailAPIView, TeamMembersAPIView, TeamServiceAccountsAPIView, @@ -53,6 +54,11 @@ PackageDependantsListAPIView.as_view(), name="cyberstorm.package.community.namespace.package-dependants", ), + path( + "teams/create/", + TeamCreateAPIView.as_view(), + name="cyberstorm.teams.create", + ), path( "team//", TeamDetailAPIView.as_view(),