From 338c94ed0559943247cc23161ddfa7a4672849b1 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 22 Dec 2020 02:54:11 +0100 Subject: [PATCH] More API filters for the member app --- apps/member/api/views.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/apps/member/api/views.py b/apps/member/api/views.py index 3dc07fe1..8e08aad7 100644 --- a/apps/member/api/views.py +++ b/apps/member/api/views.py @@ -1,7 +1,8 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from rest_framework.filters import SearchFilter +from django_filters.rest_framework import DjangoFilterBackend +from rest_framework.filters import OrderingFilter, SearchFilter from api.viewsets import ReadProtectedModelViewSet from .serializers import ProfileSerializer, ClubSerializer, MembershipSerializer @@ -16,6 +17,13 @@ class ProfileViewSet(ReadProtectedModelViewSet): """ queryset = Profile.objects.all() serializer_class = ProfileSerializer + filter_backends = [DjangoFilterBackend, SearchFilter] + filterset_fields = ['user', 'user__first_name', 'user__last_name', 'user__username', 'user__email', + 'user__note__alias__name', 'user__note__alias__normalized_name', 'phone_number', "section", + 'department', 'promotion', 'address', 'paid', 'ml_events_registration', 'ml_sport_registration', + 'ml_art_registration', 'report_frequency', 'email_confirmed', 'registration_valid', ] + search_fields = ['$user__first_name' '$user__last_name', '$user__username', '$user__email', + '$user__note__alias__name', '$user__note__alias__normalized_name', ] class ClubViewSet(ReadProtectedModelViewSet): @@ -26,8 +34,11 @@ class ClubViewSet(ReadProtectedModelViewSet): """ queryset = Club.objects.all() serializer_class = ClubSerializer - filter_backends = [SearchFilter] - search_fields = ['$name', ] + filter_backends = [DjangoFilterBackend, SearchFilter] + filterset_fields = ['name', 'email', 'note__alias__name', 'note__alias__normalized_name', 'parent_club', + 'parent_club__name', 'require_memberships', 'membership_fee_paid', 'membership_fee_unpaid', + 'membership_duration', 'membership_start', 'membership_end', ] + search_fields = ['$name', '$email', '$note__alias__name', '$note__alias__normalized_name', ] class MembershipViewSet(ReadProtectedModelViewSet): @@ -38,3 +49,12 @@ class MembershipViewSet(ReadProtectedModelViewSet): """ queryset = Membership.objects.all() serializer_class = MembershipSerializer + filter_backends = [DjangoFilterBackend, OrderingFilter, SearchFilter] + filterset_fields = ['club__name', 'club__email', 'club__note__alias__name', 'club__note__alias__normalized_name', + 'user__username', 'user__last_name', 'user__first_name', 'user__email', + 'user__note__alias__name', 'user__note__alias__normalized_name', + 'date_start', 'date_end', 'fee', ] + ordering_fields = ['id', 'date_start', 'date_end', ] + search_fields = ['$club__name', '$club__email', '$club__note__alias__name', '$club__note__alias__normalized_name', + '$user__username', '$user__last_name', '$user__first_name', '$user__email', + '$user__note__alias__name', '$user__note__alias__normalized_name', ]