Commit ca0489ec authored by okhin's avatar okhin

Create the active_constituency

parent 132589d0
Pipeline #872 canceled with stage
in 7 minutes and 2 seconds
...@@ -32,8 +32,8 @@ from .models import ( ...@@ -32,8 +32,8 @@ from .models import (
WebSite, WebSite,
) )
from .filters import ActiveMandateQueryFilterBackend from .filters import (ActiveMandateQueryFilterBackend,
ActiveConstituencyFilterBackend)
class DefaultWebPagination(pagination.PageNumberPagination): class DefaultWebPagination(pagination.PageNumberPagination):
default_web_page_size = 10 default_web_page_size = 10
...@@ -59,7 +59,8 @@ class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -59,7 +59,8 @@ class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
filters.SearchFilter, filters.SearchFilter,
filters.OrderingFilter, filters.OrderingFilter,
RQLFilterBackend, RQLFilterBackend,
ActiveMandateQueryFilterBackend ActiveMandateQueryFilterBackend,
ActiveConstituencyFilterBackend
) )
filter_fields = { filter_fields = {
'active': ['exact'], 'active': ['exact'],
......
...@@ -13,7 +13,7 @@ class ActiveMandateQueryFilterBackend(BaseFilterBackend): ...@@ -13,7 +13,7 @@ class ActiveMandateQueryFilterBackend(BaseFilterBackend):
""" """
A filter which check if a mandate is active for a reprensentative 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. and it list a list of mandates among which one should be active.
""" """
query_param = getattr(settings, 'ACTIVE_MANDATES_PARAM', 'active_mandates') query_param = getattr(settings, 'ACTIVE_MANDATES_PARAM', 'active_mandates')
...@@ -31,5 +31,25 @@ class ActiveMandateQueryFilterBackend(BaseFilterBackend): ...@@ -31,5 +31,25 @@ class ActiveMandateQueryFilterBackend(BaseFilterBackend):
Q(group__name__in=mandates) | Q(group__name__in=mandates) |
Q(group__abbreviation__in=mandates) Q(group__abbreviation__in=mandates)
)).distinct() )).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 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