change tounament: viewset, serializer, models
simply the code and use django feat
This commit is contained in:
parent
9523ac4554
commit
9bdd07d59e
@ -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()
|
||||
|
@ -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]] = []
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user