Skip to content

Commit

Permalink
Add TeamCreateAPI view
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Jan 2, 2024
1 parent a47e946 commit d302512
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django/thunderstore/api/cyberstorm/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from .team import (
AddTeamMemberAPIView,
TeamCreateAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
Expand All @@ -29,6 +30,7 @@
"PackageVersionsAPIView",
"TeamDetailAPIView",
"AddTeamMemberAPIView",
"TeamCreateAPIView",
"TeamMembersAPIView",
"TeamServiceAccountsAPIView",
]
39 changes: 38 additions & 1 deletion django/thunderstore/api/cyberstorm/views/team.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -49,6 +50,42 @@ 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):
permission_classes = [IsAuthenticated]

@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)

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

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L74-L75

Added lines #L74 - L75 were not covered by tests

form = CreateTeamForm(

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

View check run for this annotation

Codecov / codecov/patch

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

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

if form.is_valid():
team = form.save()
return Response(CyberstormTeamCreateResponseSerialiazer(team).data)

Check warning on line 84 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-L84

Added lines #L83 - L84 were not covered by tests
else:
raise ValidationError(form.errors)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L86 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 @@ -12,6 +12,7 @@
PackageVersionChangelogAPIView,
PackageVersionReadmeAPIView,
PackageVersionsAPIView,
TeamCreateAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
Expand Down Expand Up @@ -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/<str:team_id>/",
TeamDetailAPIView.as_view(),
Expand Down

0 comments on commit d302512

Please sign in to comment.