ft_transcendence/tournament/views.py
2023-12-20 19:15:47 +01:00

61 lines
1.9 KiB
Python

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, status
from rest_framework.authentication import SessionAuthentication
from django.http import HttpRequest
from django.contrib.auth import login
from matchmaking.models import in_matchmaking
from .models import TournamentModel
# Create your views here.
class TournamentsView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def post(self, request: HttpRequest):
data: dict = request.data
nb_players_by_game = data.get("nb_players_by_game")
if (nb_players_by_game is None):
return Response("nb_player_by_game is required.", status=status.HTTP_400_BAD_REQUEST)
nb_players = data.get("nb_players")
if (nb_players is None):
return Response("nb_players is required.", status=status.HTTP_400_BAD_REQUEST)
tournament_id: int = TournamentModel.create(users_id=users_id, nb_players=nb_players)
return Response({"tournament_id": tournament_id}, status=status.HTTP_201_CREATED)
class TournamentView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def get(self, request: HttpRequest, pk):
if (not TournamentModel.objects.filter(pk=pk).exists()):
return Response({"detail": "Tournament not found."}, status=status.HTTP_404_NOT_FOUND)
tournament = TournamentModel.objects.get(pk=pk)
levels: [[int]] = []
level: [int] = tournament.get_games_id_by_level
while level != []:
levels.append(level)
level: [int] = tournament.get_games_id_by_level
data = {
"name": tournament.name,
"finished": tournament.finished,
"started": tournament.finished,
"nb_players": tournament.nb_players,
"nb_players_by_game": tournament.nb_players_by_game,
"levels": levels
}
return Response(data, status=status.HTTP_200_OK)