From 4f9dfadb712c3652e2d58c2da226055787b3d9e6 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 29 Mar 2021 16:24:58 +0200 Subject: [PATCH] Add API filters for registration --- apps/api/urls.py | 4 +++ apps/registration/api/__init__.py | 2 ++ apps/registration/api/serializers.py | 47 ++++++++++++++++++++++++++++ apps/registration/api/urls.py | 11 +++++++ apps/registration/api/views.py | 15 +++++++++ requirements.txt | 2 +- 6 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 apps/registration/api/__init__.py create mode 100644 apps/registration/api/serializers.py create mode 100644 apps/registration/api/urls.py create mode 100644 apps/registration/api/views.py diff --git a/apps/api/urls.py b/apps/api/urls.py index c196e80..1215747 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -20,6 +20,10 @@ if "participation" in settings.INSTALLED_APPS: from participation.api.urls import register_participation_urls register_participation_urls(router, "participation") +if "registration" in settings.INSTALLED_APPS: + from registration.api.urls import register_registration_urls + register_registration_urls(router, "registration") + app_name = 'api' # Wire up our API using automatic URL routing. diff --git a/apps/registration/api/__init__.py b/apps/registration/api/__init__.py new file mode 100644 index 0000000..4e945ad --- /dev/null +++ b/apps/registration/api/__init__.py @@ -0,0 +1,2 @@ +# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later diff --git a/apps/registration/api/serializers.py b/apps/registration/api/serializers.py new file mode 100644 index 0000000..8f2c902 --- /dev/null +++ b/apps/registration/api/serializers.py @@ -0,0 +1,47 @@ +# Copyright (C) 2020 by Animath +# SPDX-License-Identifier: GPL-3.0-or-later + +from rest_framework import serializers +from rest_polymorphic.serializers import PolymorphicSerializer + +from ..models import AdminRegistration, CoachRegistration, ParticipantRegistration, \ + StudentRegistration, VolunteerRegistration + + +class AdminSerializer(serializers.ModelSerializer): + class Meta: + model = AdminRegistration + fields = '__all__' + + +class CoachSerializer(serializers.ModelSerializer): + class Meta: + model = CoachRegistration + fields = '__all__' + + +class ParticipantSerializer(serializers.ModelSerializer): + class Meta: + model = ParticipantRegistration + fields = '__all__' + + +class StudentSerializer(serializers.ModelSerializer): + class Meta: + model = StudentRegistration + fields = '__all__' + + +class VolunteerSerializer(serializers.ModelSerializer): + class Meta: + model = VolunteerRegistration + fields = '__all__' + + +class RegistrationSerializer(PolymorphicSerializer): + model_serializer_mapping = { + AdminRegistration: AdminSerializer, + CoachRegistration: CoachSerializer, + StudentRegistration: StudentSerializer, + VolunteerRegistration: VolunteerSerializer, + } diff --git a/apps/registration/api/urls.py b/apps/registration/api/urls.py new file mode 100644 index 0000000..620d6ac --- /dev/null +++ b/apps/registration/api/urls.py @@ -0,0 +1,11 @@ +# Copyright (C) 2020 by Animath +# SPDX-License-Identifier: GPL-3.0-or-later + +from .views import RegistrationViewSet + + +def register_registration_urls(router, path): + """ + Configure router for registration REST API. + """ + router.register(path + "/registration", RegistrationViewSet) diff --git a/apps/registration/api/views.py b/apps/registration/api/views.py new file mode 100644 index 0000000..7e48cfe --- /dev/null +++ b/apps/registration/api/views.py @@ -0,0 +1,15 @@ +# Copyright (C) 2020 by Animath +# SPDX-License-Identifier: GPL-3.0-or-later + +from django_filters.rest_framework import DjangoFilterBackend +from rest_framework.viewsets import ModelViewSet + +from .serializers import RegistrationSerializer +from ..models import Registration + + +class RegistrationViewSet(ModelViewSet): + queryset = Registration.objects.all() + serializer_class = RegistrationSerializer + filter_backends = [DjangoFilterBackend] + filterset_fields = ['user', 'participantregistration__team', ] diff --git a/requirements.txt b/requirements.txt index 3613662..0c1ff5f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,7 +19,7 @@ phonenumbers~=8.9.10 psycopg2-binary~=2.8 PyPDF3~=1.0.2 ipython~=7.19.0 -python-magic>=0.4.21 +python-magic>=0.4.22 requests~=2.25.0 sympasoap~=1.0 whoosh~=2.7 \ No newline at end of file