From 9bdd07d59e0274498d9afeef91cf2e4389faa1ec Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 20 Dec 2023 21:58:52 +0100 Subject: [PATCH] change tounament: viewset, serializer, models simply the code and use django feat --- tournament/models.py | 13 ------------- tournament/serializers.py | 7 +++++-- tournament/viewset.py | 24 +++++++++++------------- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/tournament/models.py b/tournament/models.py index 5d2ae37..287c929 100644 --- a/tournament/models.py +++ b/tournament/models.py @@ -12,19 +12,6 @@ class TournamentModel(models.Model): started = models.BooleanField(default=False) finished = models.BooleanField(default=False) - def create(self, nb_players: int, nb_players_by_game: int, name: str = ""): - self.level = 1 - number: int = nb_players - while (number != nb_players_by_game): - number = number // 2 + (number % 2) - self.level += 1 - self.nb_players = nb_players - self.nb_players_by_game = nb_players_by_game - self.name = name - self.save() - - return self.pk - def create_game(self, users_id): game_id = GameModel.create(users_id=users_id) TournamentGamesModel(game_id=game_id, tournament_id=self.pk).save() diff --git a/tournament/serializers.py b/tournament/serializers.py index 218eef5..cb8054f 100644 --- a/tournament/serializers.py +++ b/tournament/serializers.py @@ -3,11 +3,14 @@ from .models import TournamentModel class TournamentSerializer(serializers.ModelSerializer): - levels = serializers.SerializerMethodField() + levels = serializers.SerializerMethodField(read_only=True, required=False) + level = serializers.ReadOnlyField() + started = serializers.ReadOnlyField() + finished = serializers.ReadOnlyField() class Meta: model = TournamentModel - fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels"] + fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels", "id"] def get_levels(self, instance): levels: [[int]] = [] diff --git a/tournament/viewset.py b/tournament/viewset.py index 0d7f585..a99caa9 100644 --- a/tournament/viewset.py +++ b/tournament/viewset.py @@ -19,21 +19,19 @@ class TournamentViewSet(viewsets.ModelViewSet): permission_classes = (permissions.IsAuthenticated,) authentication_classes = (SessionAuthentication,) - def perform_create(self, request: HttpRequest): + def perform_create(self, serializer: TournamentSerializer): - data: dict = request.data + nb_players = serializer.validated_data["nb_players"] + nb_players_by_game = serializer.validated_data["nb_players_by_game"] + level = 1 + number: int = nb_players + while (number != nb_players_by_game): + number = number // 2 + (number % 2) + level += 1 - 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(nb_players=nb_players, nb_players_by_game=nb_players_by_game) + tournament = serializer.save(level = level) - return Response({"tournament_id": tournament_id}, status=status.HTTP_201_CREATED) + return Response(self.serializer_class(tournament).data, status=status.HTTP_201_CREATED) def list(self, request: HttpRequest, state: str = ""): query: QuerySet @@ -42,7 +40,7 @@ class TournamentViewSet(viewsets.ModelViewSet): query = TournamentModel.objects.filter(started=True, finished=False) case "finished": query = TournamentModel.objects.filter(finished=True) - case "currently": + case "waiting": query = TournamentModel.objects.filter(started=False, finished=False) case _: query = TournamentModel.objects.all()