mirror of
https://gitlab.com/animath/si/plateforme.git
synced 2024-11-30 02:13:01 +00:00
175 lines
5.7 KiB
Python
175 lines
5.7 KiB
Python
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
from django.conf.urls import url, include
|
|
from django_filters.rest_framework import DjangoFilterBackend
|
|
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
|
|
from tournament.models import Team, Tournament, Pool
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = TFJMUser
|
|
exclude = (
|
|
'email',
|
|
'password',
|
|
'groups',
|
|
'user_permissions',
|
|
)
|
|
|
|
|
|
class TeamSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Team
|
|
fields = "__all__"
|
|
|
|
|
|
class TournamentSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Tournament
|
|
fields = "__all__"
|
|
|
|
|
|
class AuthorizationSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Authorization
|
|
fields = "__all__"
|
|
|
|
|
|
class MotivationLetterSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = MotivationLetter
|
|
fields = "__all__"
|
|
|
|
|
|
class SolutionSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Solution
|
|
fields = "__all__"
|
|
|
|
|
|
class SynthesisSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Synthesis
|
|
fields = "__all__"
|
|
|
|
|
|
class PoolSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Pool
|
|
fields = "__all__"
|
|
|
|
|
|
class UserViewSet(ModelViewSet):
|
|
queryset = TFJMUser.objects.all()
|
|
serializer_class = UserSerializer
|
|
filter_backends = [DjangoFilterBackend, SearchFilter]
|
|
filterset_fields = ['id', 'first_name', 'last_name', 'email', 'gender', 'student_class', 'role', 'year', 'team',
|
|
'team__trigram', 'is_superuser', 'is_staff', 'is_active', ]
|
|
search_fields = ['$first_name', '$last_name', ]
|
|
|
|
|
|
class TeamViewSet(ModelViewSet):
|
|
queryset = Team.objects.all()
|
|
serializer_class = TeamSerializer
|
|
filter_backends = [DjangoFilterBackend, SearchFilter]
|
|
filterset_fields = ['name', 'trigram', 'validation_status', 'selected_for_final', 'access_code', 'tournament',
|
|
'year', ]
|
|
search_fields = ['$name', 'trigram', ]
|
|
|
|
|
|
class TournamentViewSet(ModelViewSet):
|
|
queryset = Tournament.objects.all()
|
|
serializer_class = TournamentSerializer
|
|
filter_backends = [DjangoFilterBackend, SearchFilter]
|
|
filterset_fields = ['name', 'size', 'price', 'date_start', 'date_end', 'final', 'organizers', 'year', ]
|
|
search_fields = ['$name', ]
|
|
|
|
|
|
class AuthorizationViewSet(ModelViewSet):
|
|
queryset = Authorization.objects.all()
|
|
serializer_class = AuthorizationSerializer
|
|
filter_backends = [DjangoFilterBackend]
|
|
filterset_fields = ['user', 'type', ]
|
|
|
|
|
|
class MotivationLetterViewSet(ModelViewSet):
|
|
queryset = MotivationLetter.objects.all()
|
|
serializer_class = MotivationLetterSerializer
|
|
filter_backends = [DjangoFilterBackend]
|
|
filterset_fields = ['team', 'team__trigram', ]
|
|
|
|
|
|
class SolutionViewSet(ModelViewSet):
|
|
queryset = Solution.objects.all()
|
|
serializer_class = SolutionSerializer
|
|
filter_backends = [DjangoFilterBackend]
|
|
filterset_fields = ['team', 'team__trigram', 'problem', ]
|
|
|
|
|
|
class SynthesisViewSet(ModelViewSet):
|
|
queryset = Synthesis.objects.all()
|
|
serializer_class = SynthesisSerializer
|
|
filter_backends = [DjangoFilterBackend]
|
|
filterset_fields = ['team', 'team__trigram', 'source', 'round', ]
|
|
|
|
|
|
class PoolViewSet(ModelViewSet):
|
|
queryset = Pool.objects.all()
|
|
serializer_class = PoolSerializer
|
|
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
|
|
router = routers.DefaultRouter()
|
|
router.register('user', UserViewSet)
|
|
router.register('team', TeamViewSet)
|
|
router.register('tournament', TournamentViewSet)
|
|
router.register('authorization', AuthorizationViewSet)
|
|
router.register('motivation_letter', MotivationLetterViewSet)
|
|
router.register('solution', SolutionViewSet)
|
|
router.register('synthesis', SynthesisViewSet)
|
|
router.register('pool', PoolViewSet)
|
|
|
|
app_name = 'api'
|
|
|
|
# Wire up our API using automatic URL routing.
|
|
# Additionally, we include login URLs for the browsable API.
|
|
urlpatterns = [
|
|
url('^', include(router.urls)),
|
|
url('^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
|
]
|