Skip to content

Commit

Permalink
Deref user tournaments (#95)
Browse files Browse the repository at this point in the history
* adding deref player and manager endpoint
* Add testings
---------
Co-authored-by: KwikKill <[email protected]>
  • Loading branch information
ShiroUsagi-san authored Nov 21, 2023
1 parent 5378794 commit 8166deb
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
63 changes: 63 additions & 0 deletions insalan/tournament/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]",
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)
1 change: 1 addition & 0 deletions insalan/tournament/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:pk>/", views.TeamDetails.as_view(), name="team/details"),
path("player/", views.PlayerRegistrationList.as_view(), name="player/list"),
Expand Down
64 changes: 62 additions & 2 deletions insalan/tournament/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down Expand Up @@ -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"""
Expand Down

0 comments on commit 8166deb

Please sign in to comment.