From ac9591c32079f75295d100c37de34ced52c4fc50 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 5 May 2020 05:57:57 +0200 Subject: [PATCH] Pool endpoint --- apps/api/urls.py | 31 +++++++++++++++++++++++++++++-- apps/tournament/urls.py | 5 +++-- apps/tournament/views.py | 13 ++++++++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/apps/api/urls.py b/apps/api/urls.py index 6771c3c..9647aa6 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -2,10 +2,10 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.conf.urls import url, include -from django.contrib.auth.models import User from django_filters.rest_framework import DjangoFilterBackend -from rest_framework import routers, serializers +from rest_framework import routers, serializers, status from rest_framework.filters import SearchFilter +from rest_framework.response import Response from rest_framework.viewsets import ModelViewSet from member.models import TFJMUser, Authorization, Solution, Synthesis, MotivationLetter @@ -125,6 +125,32 @@ class PoolViewSet(ModelViewSet): filter_backends = [DjangoFilterBackend] filterset_fields = ['teams', 'teams__trigram', 'round', ] + def create(self, request, *args, **kwargs): + data = request.data + try: + spl = data.split(";") + if len(spl) >= 7: + round = int(spl[0]) + teams = [] + solutions = [] + for i in range((len(spl) - 1) // 2): + trigram = spl[1 + 2 * i] + pb = int(spl[2 + 2 * i]) + team = Team.objects.get(trigram=trigram) + solution = Solution.objects.get(team=team, problem=pb, final=team.selected_for_final) + teams.append(team) + solutions.append(solution) + pool = Pool.objects.create(round=round) + pool.teams.set(teams) + pool.solutions.set(solutions) + pool.save() + serializer = PoolSerializer(pool) + headers = self.get_success_headers(serializer.data) + return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) + except BaseException: # JSON data + pass + return super().create(request, *args, **kwargs) + # Routers provide an easy way of automatically determining the URL conf. # Register each app API router and user viewset @@ -136,6 +162,7 @@ router.register('authorization', AuthorizationViewSet) router.register('motivation_letter', MotivationLetterViewSet) router.register('solution', SolutionViewSet) router.register('synthesis', SynthesisViewSet) +router.register('pool', PoolViewSet) app_name = 'api' diff --git a/apps/tournament/urls.py b/apps/tournament/urls.py index e09182b..eb1bdb3 100644 --- a/apps/tournament/urls.py +++ b/apps/tournament/urls.py @@ -2,7 +2,7 @@ from django.urls import path from .views import TournamentListView, TournamentCreateView, TournamentDetailView, TournamentUpdateView, \ TeamDetailView, TeamUpdateView, AddOrganizerView, SolutionsView, SolutionsOrgaListView, SynthesesView, \ - SynthesesOrgaListView, PoolListView, PoolCreateView, PoolDetailView + SynthesesOrgaListView, PoolListView, PoolCreateView, PoolDetailView, PoolEndpoint app_name = "tournament" @@ -19,6 +19,7 @@ urlpatterns = [ path("syntheses/", SynthesesView.as_view(), name="syntheses"), path("all_syntheses/", SynthesesOrgaListView.as_view(), name="all_syntheses"), path("pools/", PoolListView.as_view(), name="pools"), - path("pools/add/", PoolCreateView.as_view(), name="create_pool"), + path("pool/add/", PoolCreateView.as_view(), name="create_pool"), + path("pool/add/endpoint/", PoolEndpoint.as_view(), name="create_pool_endpoint"), path("pool//", PoolDetailView.as_view(), name="pool_detail"), ] diff --git a/apps/tournament/views.py b/apps/tournament/views.py index 2e2144c..f957d45 100644 --- a/apps/tournament/views.py +++ b/apps/tournament/views.py @@ -11,7 +11,10 @@ from django.http import HttpResponse from django.shortcuts import redirect from django.template.loader import render_to_string from django.urls import reverse_lazy +from django.utils.decorators import method_decorator from django.utils.translation import gettext_lazy as _ +from django.views import View +from django.views.decorators.csrf import csrf_exempt from django.views.generic import DetailView, CreateView, UpdateView from django.views.generic.edit import BaseFormView from django_tables2.views import SingleTableView @@ -480,4 +483,12 @@ class PoolDetailView(LoginRequiredMixin, DetailView): .format(_("Syntheses of a pool.zip").replace(" ", "%20")) return resp - return self.get(request, *args, **kwargs) \ No newline at end of file + return self.get(request, *args, **kwargs) + + +@method_decorator(csrf_exempt, 'dispatch') +class PoolEndpoint(View): + def post(self, request, *args, **kwargs): + print(request.headers) + print(request.user) + print(self.request.POST)