diff --git a/insalan/tournament/tests.py b/insalan/tournament/tests.py index 3e10bc12..20905b61 100644 --- a/insalan/tournament/tests.py +++ b/insalan/tournament/tests.py @@ -1590,3 +1590,66 @@ def test_cant_join_a_team_with_no_valid_email(self): format="json", ) self.assertEquals(request.status_code, 403) + + +class TournamentMeTests(TestCase): + def setUp(self): + self.usrobj = User.objects.create_user( + username="randomplayer", + email="randomplayer@example.com", + password="IUseAVerySecurePassword", + first_name="Random", + last_name="Player", + is_active=True, + ) + self.evobj = Event.objects.create( + name="Test Event", + description="This is a test", + year=2021, + month=12, + ongoing=False, + ) + self.game_obj = Game.objects.create(name="Test Game", short_name="TFG") + self.tourneyobj_one = Tournament.objects.create( + event=self.evobj, + name="Test Tournament", + rules="have fun!", + game=self.game_obj, + is_announced=True, + ) + self.team_one = Team.objects.create(name="Team One", tournament=self.tourneyobj_one, password=make_password("password")) + self.plobjt = Player.objects.create( + user_id=self.usrobj.id, + team=self.team_one, + pseudo="pseudo" + ) + + self.team_two = Team.objects.create(name="Team Two", tournament=self.tourneyobj_one, password=make_password("password")) + self.managojt = Manager.objects.create( + user_id=self.usrobj.id, + team=self.team_two + ) + + def test_get_tournament_me(self): + self.client.login(username="randomplayer", password="IUseAVerySecurePassword") + response = self.client.get(reverse("tournament/me")) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data['player'][0]['pseudo'], self.plobjt.pseudo) + self.assertEqual(response.data['player'][0]['team']['name'], self.team_one.name) + self.assertEqual(response.data['player'][0]['team']['tournament']['name'], self.tourneyobj_one.name) + self.assertEqual(response.data['player'][0]['team']['tournament']['event']['name'], self.evobj.name) + + def test_get_tournament_me_manager(self): + self.client.login(username="randomplayer", password="IUseAVerySecurePassword") + response = self.client.get(reverse("tournament/me")) + + self.assertEqual(response.data['manager'][0]['id'], self.managojt.id) + self.assertEqual(response.data['manager'][0]['team']['name'], self.team_two.name) + self.assertEqual(response.data['manager'][0]['team']['tournament']['name'], self.tourneyobj_one.name) + self.assertEqual(response.data['manager'][0]['team']['tournament']['event']['name'], self.evobj.name) + + def test_get_tournament_me_unauthenticated(self): + response = self.client.get(reverse("tournament/me")) + + self.assertEqual(response.status_code, 403) diff --git a/insalan/tournament/urls.py b/insalan/tournament/urls.py index e34f0f33..6793707c 100644 --- a/insalan/tournament/urls.py +++ b/insalan/tournament/urls.py @@ -26,6 +26,7 @@ views.TournamentDetailsFull.as_view(), name="tournament/details-full", ), + path("me/", views.TournamentMe.as_view(), name="tournament/me"), path("team/", views.TeamList.as_view(), name="team/list"), path("team//", views.TeamDetails.as_view(), name="team/details"), path("player/", views.PlayerRegistrationList.as_view(), name="player/list"), diff --git a/insalan/tournament/views.py b/insalan/tournament/views.py index 44445ec7..8ba5f21a 100644 --- a/insalan/tournament/views.py +++ b/insalan/tournament/views.py @@ -12,16 +12,15 @@ from rest_framework.exceptions import NotFound from rest_framework.permissions import BasePermission, SAFE_METHODS from rest_framework.response import Response -from rest_framework.serializers import PrimaryKeyRelatedField from rest_framework.views import APIView from django.http import QueryDict from insalan.user.models import User import insalan.tournament.serializers as serializers +from rest_framework.authentication import SessionAuthentication from .models import Player, Manager, Event, Tournament, Game, Team, PaymentStatus - class ReadOnly(BasePermission): """Read-Only permissions""" @@ -189,6 +188,67 @@ def get(self, request, primary_key: int): return Response(tourney_serialized, status=status.HTTP_200_OK) +class TournamentMe(APIView): + """ + Details on tournament of a logged user + This endpoint does many requests to the database and should be used wisely + """ + authentication_classes = [SessionAuthentication] + permission_classes = [permissions.IsAuthenticated & ReadOnly] + + def get(self, request): + user: User = request.user + + if user is None: + raise PermissionDenied() + + # retrieve registration as Player + players = Player.objects.filter(user=user) + # serialize it + players = serializers.PlayerSerializer( + players, context={"request": request}, many=True + ).data + for player in players: + # dereference team + player["team"] = serializers.TeamSerializer( + Team.objects.get(id=player["team"]), context={"request": request} + ).data + # dereference tournament + player["team"]["tournament"] = serializers.TournamentSerializer( + Tournament.objects.get(id=player["team"]["tournament"]), + context={"request": request}, + ).data + # dereference event + player["team"]["tournament"]["event"] = serializers.EventSerializer( + Event.objects.get(id=player["team"]["tournament"]["event"]), + context={"request": request}, + ).data + + # retrieve registration as Manager + managers = Manager.objects.filter(user=user) + # serialize it + managers = serializers.ManagerSerializer( + managers, context={"request": request}, many=True + ).data + for manager in managers: + # dereference team + manager["team"] = serializers.TeamSerializer( + Team.objects.get(id=manager["team"]), context={"request": request} + ).data + # dereference tournament + manager["team"]["tournament"] = serializers.TournamentSerializer( + Tournament.objects.get(id=manager["team"]["tournament"]), + context={"request": request}, + ).data + # dereference event + manager["team"]["tournament"]["event"] = serializers.EventSerializer( + Event.objects.get(id=manager["team"]["tournament"]["event"]), + context={"request": request}, + ).data + + return Response({"player": players, "manager": managers}, status=status.HTTP_200_OK) + + # Teams class TeamList(generics.ListCreateAPIView): """List all known teams"""