Skip to content

Commit

Permalink
Add EditTeamAPIView
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksamies committed Dec 13, 2023
1 parent f2a2960 commit bcc4efb
Show file tree
Hide file tree
Showing 3 changed files with 45 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,
EditTeamAPIView,
TeamDetailAPIView,
TeamMembersAPIView,
TeamServiceAccountsAPIView,
Expand All @@ -29,6 +30,7 @@
"PackageVersionsAPIView",
"TeamDetailAPIView",
"AddTeamMemberAPIView",
"EditTeamAPIView",
"TeamMembersAPIView",
"TeamServiceAccountsAPIView",
]
38 changes: 37 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, DonationLinkTeamForm
from thunderstore.repository.models.team import Team, TeamMember

User = get_user_model()
Expand Down Expand Up @@ -49,6 +50,41 @@ def check_permissions(self, request: Request) -> None:
raise PermissionDenied()


class CyberstormEditTeamRequestSerialiazer(serializers.Serializer):
donation_link = serializers.CharField(
max_length=Team._meta.get_field("donation_link").max_length,
validators=Team._meta.get_field("donation_link").validators,
)


class CyberstormEditTeamResponseSerialiazer(serializers.Serializer):
donation_link = serializers.CharField()


class EditTeamAPIView(APIView):
@conditional_swagger_auto_schema(
request_body=CyberstormEditTeamRequestSerialiazer,
responses={200: CyberstormEditTeamResponseSerialiazer},
operation_id="cyberstorm.team.edit",
tags=["cyberstorm"],
)
def post(self, request: HttpRequest, team_name: str):
serializer = CyberstormEditTeamRequestSerialiazer(data=request.data)
serializer.is_valid(raise_exception=True)
team = get_object_or_404(Team, name__iexact=team_name)
form = DonationLinkTeamForm(

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#L72-L75

Added lines #L72 - L75 were not covered by tests
user=request.user,
instance=team,
data=serializer.validated_data,
)

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

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

View check run for this annotation

Codecov / codecov/patch

django/thunderstore/api/cyberstorm/views/team.py#L82-L83

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L85 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 @@ -6,6 +6,7 @@
CommunityFiltersAPIView,
CommunityListAPIView,
CommunityPackageListAPIView,
EditTeamAPIView,
NamespacePackageListAPIView,
PackageDependantsListAPIView,
PackageDetailAPIView,
Expand Down Expand Up @@ -58,6 +59,11 @@
TeamDetailAPIView.as_view(),
name="cyberstorm.team.detail",
),
path(
"team/<str:team_id>/edit/",
EditTeamAPIView.as_view(),
name="cyberstorm.team.edit",
),
path(
"team/<str:team_id>/members/",
TeamMembersAPIView.as_view(),
Expand Down

0 comments on commit bcc4efb

Please sign in to comment.