Commit 953a7a7c authored by okhin's avatar okhin 🚴

Merge branch '186-add-a-query-filter-to-sort-meps-by-active-constituency' into 'master'

Create the active_constituency

Closes #186

See merge request !188
parents 132589d0 9c200545
Pipeline #875 passed with stages
in 8 minutes and 6 seconds
......@@ -32,7 +32,8 @@ from .models import (
WebSite,
)
from .filters import ActiveMandateQueryFilterBackend
from .filters import (ActiveMandateQueryFilterBackend,
ActiveConstituencyFilterBackend)
class DefaultWebPagination(pagination.PageNumberPagination):
......@@ -59,7 +60,8 @@ class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
filters.SearchFilter,
filters.OrderingFilter,
RQLFilterBackend,
ActiveMandateQueryFilterBackend
ActiveMandateQueryFilterBackend,
ActiveConstituencyFilterBackend
)
filter_fields = {
'active': ['exact'],
......
......@@ -13,7 +13,7 @@ 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
the parameter used in the query to filter is, by default active_mandates
and it list a list of mandates among which one should be active.
"""
query_param = getattr(settings, 'ACTIVE_MANDATES_PARAM', 'active_mandates')
......@@ -31,5 +31,28 @@ class ActiveMandateQueryFilterBackend(BaseFilterBackend):
Q(group__name__in=mandates) |
Q(group__abbreviation__in=mandates)
)).distinct()
return qs
return qs
class ActiveConstituencyFilterBackend(BaseFilterBackend):
"""
A filter which check if a representative is active in a constituency
the parameter used in the query to filter is, by default,
active_constituency.
"""
query_param = getattr(settings,
'ACTIVE_CONSTITUENCY_PARAM', 'active_constituency')
def filter_queryset(self, request, queryset, view):
qs = queryset
if self.query_param in request.GET:
if len(request.GET[self.query_params]):
mandates = request.GET[self.query_param].split(',')
qs = qs.filter(mandates__in=Mandate.objects.filter(
Q(end_date__gte=datetime.today) |
Q(end_date__isnull=True)).filter(
Q(constituency__name__in=mandates)
)).distinct()
return qs
Markdown is supported
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