Pool endpoint

This commit is contained in:
Yohann D'ANELLO 2020-05-05 05:57:57 +02:00
parent 14cde3ac91
commit ac9591c320
3 changed files with 44 additions and 5 deletions

View File

@ -2,10 +2,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from django.conf.urls import url, include from django.conf.urls import url, include
from django.contrib.auth.models import User
from django_filters.rest_framework import DjangoFilterBackend 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.filters import SearchFilter
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet from rest_framework.viewsets import ModelViewSet
from member.models import TFJMUser, Authorization, Solution, Synthesis, MotivationLetter from member.models import TFJMUser, Authorization, Solution, Synthesis, MotivationLetter
@ -125,6 +125,32 @@ class PoolViewSet(ModelViewSet):
filter_backends = [DjangoFilterBackend] filter_backends = [DjangoFilterBackend]
filterset_fields = ['teams', 'teams__trigram', 'round', ] 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. # Routers provide an easy way of automatically determining the URL conf.
# Register each app API router and user viewset # Register each app API router and user viewset
@ -136,6 +162,7 @@ router.register('authorization', AuthorizationViewSet)
router.register('motivation_letter', MotivationLetterViewSet) router.register('motivation_letter', MotivationLetterViewSet)
router.register('solution', SolutionViewSet) router.register('solution', SolutionViewSet)
router.register('synthesis', SynthesisViewSet) router.register('synthesis', SynthesisViewSet)
router.register('pool', PoolViewSet)
app_name = 'api' app_name = 'api'

View File

@ -2,7 +2,7 @@ from django.urls import path
from .views import TournamentListView, TournamentCreateView, TournamentDetailView, TournamentUpdateView, \ from .views import TournamentListView, TournamentCreateView, TournamentDetailView, TournamentUpdateView, \
TeamDetailView, TeamUpdateView, AddOrganizerView, SolutionsView, SolutionsOrgaListView, SynthesesView, \ TeamDetailView, TeamUpdateView, AddOrganizerView, SolutionsView, SolutionsOrgaListView, SynthesesView, \
SynthesesOrgaListView, PoolListView, PoolCreateView, PoolDetailView SynthesesOrgaListView, PoolListView, PoolCreateView, PoolDetailView, PoolEndpoint
app_name = "tournament" app_name = "tournament"
@ -19,6 +19,7 @@ urlpatterns = [
path("syntheses/", SynthesesView.as_view(), name="syntheses"), path("syntheses/", SynthesesView.as_view(), name="syntheses"),
path("all_syntheses/", SynthesesOrgaListView.as_view(), name="all_syntheses"), path("all_syntheses/", SynthesesOrgaListView.as_view(), name="all_syntheses"),
path("pools/", PoolListView.as_view(), name="pools"), 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/<int:pk>/", PoolDetailView.as_view(), name="pool_detail"), path("pool/<int:pk>/", PoolDetailView.as_view(), name="pool_detail"),
] ]

View File

@ -11,7 +11,10 @@ from django.http import HttpResponse
from django.shortcuts import redirect from django.shortcuts import redirect
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _ 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 import DetailView, CreateView, UpdateView
from django.views.generic.edit import BaseFormView from django.views.generic.edit import BaseFormView
from django_tables2.views import SingleTableView from django_tables2.views import SingleTableView
@ -480,4 +483,12 @@ class PoolDetailView(LoginRequiredMixin, DetailView):
.format(_("Syntheses of a pool.zip").replace(" ", "%20")) .format(_("Syntheses of a pool.zip").replace(" ", "%20"))
return resp return resp
return self.get(request, *args, **kwargs) 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)