Skip to content

Commit

Permalink
Add TeamCreateAPI view
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 11, 2023
1 parent 31405a1 commit a4d49e7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 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 (
TeamCreateAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
)

__all__ = [
"CommunityDetailAPIView",
Expand All @@ -16,6 +21,7 @@
"NamespacePackageListAPIView",
"PackageDependantsListAPIView",
"TeamDetailAPIView",
"TeamCreateAPIView",
"TeamMembersAPIView",
"TeamServiceAccountsAPIView",
]
41 changes: 40 additions & 1 deletion 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 drf_yasg.utils import swagger_auto_schema
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 @@ -12,6 +16,7 @@
)
from thunderstore.api.ordering import StrictOrderingFilter
from thunderstore.api.utils import CyberstormAutoSchemaMixin
from thunderstore.repository.forms import CreateTeamForm
from thunderstore.repository.models.team import Team, TeamMember


Expand Down Expand Up @@ -39,6 +44,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):
@swagger_auto_schema(
request_body=CyberstormTeamCreateRequestSerialiazer,
responses={200: CyberstormTeamCreateResponseSerialiazer},
operation_id="cyberstorm.teams.create",
tags=["cyberstorm"],
)
def post(self, request, format=None):
serializer = CyberstormTeamCreateRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)

form = CreateTeamForm(
user=request.user,
data=request.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]
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 @@ -7,6 +7,7 @@
CommunityPackageListAPIView,
NamespacePackageListAPIView,
PackageDependantsListAPIView,
TeamCreateAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
Expand Down Expand Up @@ -43,6 +44,11 @@
PackageDependantsListAPIView.as_view(),
name="cyberstorm.package.community.namespace.package-dependants",
),
path(
"teams/create/",
TeamCreateAPIView.as_view(),
name="cyberstorm.teams.create",
),
path(
"team/<str:team_id>/",
TeamDetailAPIView.as_view(),
Expand Down

0 comments on commit a4d49e7

Please sign in to comment.