From 1032a8fd986db20bda7e994af63d8b419181ab1a Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 20 Dec 2023 21:09:31 +0100 Subject: [PATCH] core: recreation of tournament view by using queryset --- tournament/serializers.py | 19 +++++++++++ tournament/urls.py | 8 ++--- tournament/{views.py => viewset.py} | 50 ++++++++++++++--------------- 3 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 tournament/serializers.py rename tournament/{views.py => viewset.py} (51%) diff --git a/tournament/serializers.py b/tournament/serializers.py new file mode 100644 index 0000000..218eef5 --- /dev/null +++ b/tournament/serializers.py @@ -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 diff --git a/tournament/urls.py b/tournament/urls.py index 4451812..f614391 100644 --- a/tournament/urls.py +++ b/tournament/urls.py @@ -1,10 +1,10 @@ -from django.urls import path +from django.urls import path, re_path from django.conf import settings from django.conf.urls.static import static -from .views import TournamentsView, TournamentView +from .viewset import TournamentViewSet urlpatterns = [ - path("", name="tournament_page"), - path("", TournamentsView.as_view(), name="tournaments"), + path("", TournamentViewSet.as_view({"get": "retrieve", "post": "create"}), name="tournament_page"), + re_path(r"()?", TournamentViewSet.as_view({"get": "list"}), name="tournaments"), ] \ No newline at end of file diff --git a/tournament/views.py b/tournament/viewset.py similarity index 51% rename from tournament/views.py rename to tournament/viewset.py index f055d22..0d7f585 100644 --- a/tournament/views.py +++ b/tournament/viewset.py @@ -1,21 +1,25 @@ -from rest_framework.views import APIView +from rest_framework import viewsets 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 django.db.models import QuerySet from matchmaking.models import in_matchmaking from .models import TournamentModel +from .serializers import TournamentSerializer # Create your views here. -class TournamentsView(APIView): +class TournamentViewSet(viewsets.ModelViewSet): + queryset = TournamentModel.objects.all + serializer_class = TournamentSerializer permission_classes = (permissions.IsAuthenticated,) authentication_classes = (SessionAuthentication,) - def post(self, request: HttpRequest): + def perform_create(self, request: HttpRequest): data: dict = request.data @@ -27,35 +31,29 @@ class TournamentsView(APIView): 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) + 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) + + 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) -class TournamentView(APIView): - - permission_classes = (permissions.IsAuthenticated,) - authentication_classes = (SessionAuthentication,) - - def get(self, request: HttpRequest, pk): + def retrieve(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) \ No newline at end of file + return Response(self.serializer_class(tournament).data, status=status.HTTP_200_OK) \ No newline at end of file