diff --git a/registration/api/serializers.py b/registration/api/serializers.py index 14330b1..ffe431d 100644 --- a/registration/api/serializers.py +++ b/registration/api/serializers.py @@ -1,6 +1,7 @@ # Copyright (C) 2020 by Animath # SPDX-License-Identifier: GPL-3.0-or-later +from django.contrib.auth.models import User from rest_framework import serializers from rest_polymorphic.serializers import PolymorphicSerializer @@ -44,3 +45,9 @@ class PaymentSerializer(serializers.ModelSerializer): class Meta: model = Payment fields = '__all__' + + +class BasicUserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = ['first_name', 'last_name', 'email', ] diff --git a/registration/api/urls.py b/registration/api/urls.py index b7bce3d..0166d81 100644 --- a/registration/api/urls.py +++ b/registration/api/urls.py @@ -1,7 +1,7 @@ # Copyright (C) 2020 by Animath # SPDX-License-Identifier: GPL-3.0-or-later -from .views import PaymentViewSet, RegistrationViewSet +from .views import PaymentViewSet, RegistrationViewSet, VolunteersViewSet def register_registration_urls(router, path): @@ -10,3 +10,4 @@ def register_registration_urls(router, path): """ router.register(path + "/payment", PaymentViewSet) router.register(path + "/registration", RegistrationViewSet) + router.register(path + "/volunteers", VolunteersViewSet) diff --git a/registration/api/views.py b/registration/api/views.py index dc787e9..06fa574 100644 --- a/registration/api/views.py +++ b/registration/api/views.py @@ -1,10 +1,13 @@ # Copyright (C) 2020 by Animath # SPDX-License-Identifier: GPL-3.0-or-later +from django.contrib.auth.models import User from django_filters.rest_framework import DjangoFilterBackend -from rest_framework.viewsets import ModelViewSet +from rest_framework.filters import SearchFilter +from rest_framework.permissions import BasePermission, IsAuthenticated, SAFE_METHODS +from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet -from .serializers import PaymentSerializer, RegistrationSerializer +from .serializers import BasicUserSerializer, PaymentSerializer, RegistrationSerializer from ..models import Payment, Registration @@ -20,3 +23,18 @@ class PaymentViewSet(ModelViewSet): 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 = [IsAuthenticated & IsTournamentOrganizer] + filter_backends = [DjangoFilterBackend, SearchFilter] + filterset_fields = ['first_name', 'last_name', 'email', ] + search_fields = ['$first_name', '$last_name', '$email', ]