Commit ac835173 authored by okhin's avatar okhin 🚴
Browse files

Merge branch '184-returning-meps-with-an-active-mandates-in-django-rql-filter' into 'master'

FIX #184: Add a active_mandates= filter on queries

Closes #184

See merge request !185
parents 3abe72b1 04b90922
...@@ -32,6 +32,8 @@ from .models import ( ...@@ -32,6 +32,8 @@ from .models import (
WebSite, WebSite,
) )
from .filters import ActiveMandateQueryFilterBackend
class DefaultWebPagination(pagination.PageNumberPagination): class DefaultWebPagination(pagination.PageNumberPagination):
default_web_page_size = 10 default_web_page_size = 10
...@@ -56,7 +58,8 @@ class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -56,7 +58,8 @@ class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
filters.DjangoFilterBackend, filters.DjangoFilterBackend,
filters.SearchFilter, filters.SearchFilter,
filters.OrderingFilter, filters.OrderingFilter,
RQLFilterBackend RQLFilterBackend,
) )
filter_fields = { filter_fields = {
'active': ['exact'], 'active': ['exact'],
from datetime import datetime
from rest_framework.filters import BaseFilterBackend
from django.db.models import Q
from django.conf import settings
from .models import Mandate
class ActiveMandateQueryFilterBackend(BaseFilterBackend):
A filter which check if a mandate is active for a reprensentative
the parameter used in the query to filter is, by default mandates_active
and it list a list of mandates among which one should be active.
query_param = getattr(settings, 'ACTIVE_MANDATES_PARAM', 'active_mandates')
def filter_queryset(self, request, queryset, view):
qs = queryset
if self.query_param in request.GET:
if len(request.GET[self.query_param]):
# We want to check for params in a list of mandates
mandates = request.GET[self.query_params].split(',')
qs = qs.filter(mandates__in=Mandate.objects.filter(
Q( |
Q(group__name__in=mandates) |
return qs
return qs
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment