Compare commits

..

No commits in common. "56cfd563d77c42781cdb83e3c51f2e8271013875" and "9714918de915d9196d621de13c120f4201021433" have entirely different histories.

7 changed files with 37 additions and 53 deletions

View File

@ -5,7 +5,7 @@ class GameModel(models.Model):
finished = models.BooleanField(default=False)
started = models.BooleanField(default=False)
winner_id = models.IntegerField(null=True, blank=True)
winner_id = models.IntegerField()
def create(self, users_id: [int]):
self.save()

View File

@ -31,6 +31,9 @@ class ProfileViewSet(viewsets.ModelViewSet):
profile["avatar_url"] = profile["avatar_url"][profile["avatar_url"].find("static") - 1:]
return Response(serializer.data)
def perform_create(self, serializer):
serializer.save(user=self.request.user)
class MyProfileViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.IsAuthenticated,)

View File

@ -5,7 +5,7 @@ from games.models import GameModel
# Create your models here.tu
class TournamentModel(models.Model):
name = models.CharField(max_length=100)
name = models.CharField()
nb_players = models.IntegerField()
nb_players_by_game = models.IntegerField()
level = models.IntegerField()
@ -21,9 +21,7 @@ class TournamentModel(models.Model):
self.nb_players = nb_players
self.nb_players_by_game = nb_players_by_game
self.name = name
self.save()
return self.pk
return self.save()
def create_game(self, users_id):
game_id = GameModel.create(users_id=users_id)

View File

@ -1,19 +0,0 @@
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

View File

@ -1,10 +1,10 @@
from django.urls import path, re_path
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from .viewset import TournamentViewSet
from .views import TournamentsView, TournamentView
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>", name="tournament_page"),
path("", TournamentsView.as_view(), name="tournaments"),
]

View File

@ -1,25 +1,21 @@
from rest_framework import viewsets
from rest_framework.views import APIView
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 TournamentViewSet(viewsets.ModelViewSet):
class TournamentsView(APIView):
queryset = TournamentModel.objects.all
serializer_class = TournamentSerializer
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def perform_create(self, request: HttpRequest):
def post(self, request: HttpRequest):
data: dict = request.data
@ -31,29 +27,35 @@ class TournamentViewSet(viewsets.ModelViewSet):
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_id: int = TournamentModel.create(users_id=users_id, nb_players=nb_players)
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):
def retrieve(self, request: HttpRequest, pk):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def get(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)
return Response(self.serializer_class(tournament).data, status=status.HTTP_200_OK)
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)

View File

@ -22,6 +22,6 @@ urlpatterns = [
path('api/profiles/', include('profiles.urls')),
path('api/accounts/', include('accounts.urls')),
path('api/chat/', include('chat.urls')),
path('api/tournaments/', include('tournament.urls')),
path('api/tournament', include('tournament.urls')),
path('', include('frontend.urls')),
]