Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Political Memory
memopol
Commits
5fc2da48
Commit
5fc2da48
authored
Jan 12, 2017
by
okhin
Browse files
FIX #184: Add a active_mandates= filter on queries
parent
3abe72b1
Pipeline
#743
failed with stage
in 9 minutes and 49 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/representatives/api.py
View file @
5fc2da48
...
...
@@ -32,6 +32,8 @@ from .models import (
WebSite
,
)
from
.filters
import
ActiveMandateQueryFilterBackend
class
DefaultWebPagination
(
pagination
.
PageNumberPagination
):
default_web_page_size
=
10
...
...
@@ -56,7 +58,8 @@ class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
filters
.
DjangoFilterBackend
,
filters
.
SearchFilter
,
filters
.
OrderingFilter
,
RQLFilterBackend
RQLFilterBackend
,
ActiveMandateQueryFilterBackend
)
filter_fields
=
{
'active'
:
[
'exact'
],
...
...
src/representatives/filters.py
0 → 100644
View file @
5fc2da48
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_QUERY_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
]):
qs
=
qs
.
filter
(
mandates__in
=
Mandate
.
objects
.
filter
(
Q
(
end_date__gte
=
datetime
.
today
)
|
Q
(
end_date__isnull
=
True
))
.
filter
(
Q
(
group__name
=
request
.
GET
[
self
.
query_param
]))).
distinct
()
return
qs
return
qs
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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