Compare commits

...

2 Commits

Author SHA1 Message Date
9bdd07d59e change tounament: viewset, serializer, models
simply the code and use django feat
2023-12-20 21:58:52 +01:00
9523ac4554 change url patern to tournament 2023-12-20 21:57:16 +01:00
4 changed files with 18 additions and 30 deletions

View File

@ -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()

View File

@ -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]] = []

View File

@ -5,6 +5,6 @@ from django.conf.urls.static import static
from .viewset import TournamentViewSet
urlpatterns = [
path("<int:pk>", TournamentViewSet.as_view({"get": "retrieve", "post": "create"}), name="tournament_page"),
re_path(r"(<str:state>)?", TournamentViewSet.as_view({"get": "list"}), name="tournaments"),
path("<int:pk>", TournamentViewSet.as_view({"get": "retrieve"}), name="tournament_page"),
re_path(r"search/(?P<state>\w*)", TournamentViewSet.as_view({"get": "list", "post": "create"}), name="tournaments"),
]

View File

@ -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()