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 4, 2024
1 parent 463470a commit 4a22183
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 @@ -14,6 +14,7 @@
TeamMemberAddAPIView,
TeamMemberListAPIView,
TeamServiceAccountListAPIView,
TeamCreateAPIView,
)

__all__ = [
Expand All @@ -31,4 +32,5 @@
"TeamMemberAddAPIView",
"TeamMemberListAPIView",
"TeamServiceAccountListAPIView",
"TeamCreateAPIView",
]
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 TeamMemberListAPIView(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 @@ -15,6 +15,7 @@
TeamMemberAddAPIView,
TeamMemberListAPIView,
TeamServiceAccountListAPIView,
TeamCreateAPIView,
)

cyberstorm_urls = [
Expand Down Expand Up @@ -78,6 +79,11 @@
PackageVersionListAPIView.as_view(),
name="cyberstorm.package.versions",
),
path(
"teams/create/",
TeamCreateAPIView.as_view(),
name="cyberstorm.teams.create",
),
path(
"team/<str:team_id>/",
TeamAPIView.as_view(),
Expand Down

0 comments on commit 4a22183

Please sign in to comment.