core: recreation of tournament view by using
queryset
This commit is contained in:
parent
9714918de9
commit
1032a8fd98
19
tournament/serializers.py
Normal file
19
tournament/serializers.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from .models import TournamentModel
|
||||||
|
|
||||||
|
class TournamentSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
levels = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = TournamentModel
|
||||||
|
fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels"]
|
||||||
|
|
||||||
|
def get_levels(self, instance):
|
||||||
|
levels: [[int]] = []
|
||||||
|
for i in range(instance.level):
|
||||||
|
level: [int] = instance.get_games_id_by_level(i)
|
||||||
|
if (level == []):
|
||||||
|
break
|
||||||
|
levels.append(level)
|
||||||
|
return levels
|
@ -1,10 +1,10 @@
|
|||||||
from django.urls import path
|
from django.urls import path, re_path
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.conf.urls.static import static
|
from django.conf.urls.static import static
|
||||||
|
|
||||||
from .views import TournamentsView, TournamentView
|
from .viewset import TournamentViewSet
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("<int:pk>", name="tournament_page"),
|
path("<int:pk>", TournamentViewSet.as_view({"get": "retrieve", "post": "create"}), name="tournament_page"),
|
||||||
path("", TournamentsView.as_view(), name="tournaments"),
|
re_path(r"(<str:state>)?", TournamentViewSet.as_view({"get": "list"}), name="tournaments"),
|
||||||
]
|
]
|
@ -1,21 +1,25 @@
|
|||||||
from rest_framework.views import APIView
|
from rest_framework import viewsets
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import permissions, status
|
from rest_framework import permissions, status
|
||||||
from rest_framework.authentication import SessionAuthentication
|
from rest_framework.authentication import SessionAuthentication
|
||||||
|
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
from django.contrib.auth import login
|
from django.contrib.auth import login
|
||||||
|
from django.db.models import QuerySet
|
||||||
|
|
||||||
from matchmaking.models import in_matchmaking
|
from matchmaking.models import in_matchmaking
|
||||||
from .models import TournamentModel
|
from .models import TournamentModel
|
||||||
|
from .serializers import TournamentSerializer
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
class TournamentsView(APIView):
|
class TournamentViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
|
queryset = TournamentModel.objects.all
|
||||||
|
serializer_class = TournamentSerializer
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
authentication_classes = (SessionAuthentication,)
|
authentication_classes = (SessionAuthentication,)
|
||||||
|
|
||||||
def post(self, request: HttpRequest):
|
def perform_create(self, request: HttpRequest):
|
||||||
|
|
||||||
data: dict = request.data
|
data: dict = request.data
|
||||||
|
|
||||||
@ -27,35 +31,29 @@ class TournamentsView(APIView):
|
|||||||
if (nb_players is None):
|
if (nb_players is None):
|
||||||
return Response("nb_players is required.", status=status.HTTP_400_BAD_REQUEST)
|
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)
|
tournament_id: int = TournamentModel().create(nb_players=nb_players, nb_players_by_game=nb_players_by_game)
|
||||||
|
|
||||||
return Response({"tournament_id": tournament_id}, status=status.HTTP_201_CREATED)
|
return Response({"tournament_id": tournament_id}, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
class TournamentView(APIView):
|
def list(self, request: HttpRequest, state: str = ""):
|
||||||
|
query: QuerySet
|
||||||
|
match state:
|
||||||
|
case "started":
|
||||||
|
query = TournamentModel.objects.filter(started=True, finished=False)
|
||||||
|
case "finished":
|
||||||
|
query = TournamentModel.objects.filter(finished=True)
|
||||||
|
case "currently":
|
||||||
|
query = TournamentModel.objects.filter(started=False, finished=False)
|
||||||
|
case _:
|
||||||
|
query = TournamentModel.objects.all()
|
||||||
|
serializer = TournamentSerializer(query, many=True)
|
||||||
|
return Response(serializer.data)
|
||||||
|
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
def retrieve(self, request: HttpRequest, pk):
|
||||||
authentication_classes = (SessionAuthentication,)
|
|
||||||
|
|
||||||
def get(self, request: HttpRequest, pk):
|
|
||||||
|
|
||||||
if (not TournamentModel.objects.filter(pk=pk).exists()):
|
if (not TournamentModel.objects.filter(pk=pk).exists()):
|
||||||
return Response({"detail": "Tournament not found."}, status=status.HTTP_404_NOT_FOUND)
|
return Response({"detail": "Tournament not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
tournament = TournamentModel.objects.get(pk=pk)
|
tournament = TournamentModel.objects.get(pk=pk)
|
||||||
|
|
||||||
levels: [[int]] = []
|
return Response(self.serializer_class(tournament).data, status=status.HTTP_200_OK)
|
||||||
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)
|
|
Loading…
Reference in New Issue
Block a user