Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
luxcem
memopol
Commits
ca0489ec
Commit
ca0489ec
authored
Feb 09, 2017
by
okhin
Browse files
Create the active_constituency
parent
132589d0
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/representatives/api.py
View file @
ca0489ec
...
...
@@ -32,8 +32,8 @@ from .models import (
WebSite
,
)
from
.filters
import
ActiveMandateQueryFilterBackend
from
.filters
import
(
ActiveMandateQueryFilterBackend
,
ActiveConstituencyFilterBackend
)
class
DefaultWebPagination
(
pagination
.
PageNumberPagination
):
default_web_page_size
=
10
...
...
@@ -59,7 +59,8 @@ class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
filters
.
SearchFilter
,
filters
.
OrderingFilter
,
RQLFilterBackend
,
ActiveMandateQueryFilterBackend
ActiveMandateQueryFilterBackend
,
ActiveConstituencyFilterBackend
)
filter_fields
=
{
'active'
:
[
'exact'
],
...
...
src/representatives/filters.py
View file @
ca0489ec
...
...
@@ -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,25 @@ 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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment