1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-30 19:42:22 +00:00
plateforme-tfjm2/registration/api/views.py

41 lines
1.7 KiB
Python
Raw Normal View History

2021-03-29 14:24:58 +00:00
# Copyright (C) 2020 by Animath
# SPDX-License-Identifier: GPL-3.0-or-later
from django.contrib.auth.models import User
2021-03-29 14:24:58 +00:00
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import SearchFilter
from rest_framework.permissions import BasePermission, IsAdminUser, IsAuthenticated, SAFE_METHODS
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
2021-03-29 14:24:58 +00:00
from .serializers import BasicUserSerializer, PaymentSerializer, RegistrationSerializer
from ..models import Payment, Registration
2021-03-29 14:24:58 +00:00
class RegistrationViewSet(ModelViewSet):
queryset = Registration.objects.all()
serializer_class = RegistrationSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['user', 'participantregistration__team', ]
class PaymentViewSet(ModelViewSet):
queryset = Payment.objects.all()
serializer_class = PaymentSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['registrations', 'grouped', 'amount', 'final', 'type', 'valid', ]
class IsTournamentOrganizer(BasePermission):
def has_permission(self, request, view):
reg = request.user.registration
return request.method in SAFE_METHODS and reg.is_volunteer and reg.organized_tournaments.exists()
class VolunteersViewSet(ReadOnlyModelViewSet):
queryset = User.objects.filter(registration__volunteerregistration__isnull=False)
serializer_class = BasicUserSerializer
permission_classes = [IsAdminUser | (IsAuthenticated & IsTournamentOrganizer)]
filter_backends = [DjangoFilterBackend, SearchFilter]
filterset_fields = ['first_name', 'last_name', 'email', ]
search_fields = ['$first_name', '$last_name', '$email', ]