-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [#357] Support ordering and pagination params in request body
Since the filters now can come through the request body, so can the ordering and the pagination params. So these need to be supported
- Loading branch information
1 parent
ba30cea
commit bed01bc
Showing
4 changed files
with
57 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
backend/src/openarchiefbeheer/utils/django_filters/backends.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,34 @@ | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
from rest_framework.filters import OrderingFilter | ||
|
||
|
||
class NoModelFilterBackend(DjangoFilterBackend): | ||
pass | ||
|
||
|
||
class OrderingWithPostFilterBackend(OrderingFilter): | ||
"""Support ordering params also in the request body.""" | ||
|
||
def get_ordering_filters(self, request): | ||
return request.query_params or request.data | ||
|
||
def get_ordering(self, request, queryset, view): | ||
""" | ||
Ordering is set by a comma delimited ?ordering=... query parameter. | ||
The `ordering` query parameter can be overridden by setting | ||
the `ordering_param` value on the OrderingFilter or by | ||
specifying an `ORDERING_PARAM` value in the API settings. | ||
""" | ||
# Overriding where the filters are coming from (for us from the POST request body). | ||
# Normally they are query params. | ||
ordering_filters = self.get_ordering_filters(request) | ||
params = ordering_filters.get(self.ordering_param) | ||
if params: | ||
fields = [param.strip() for param in params.split(",")] | ||
ordering = self.remove_invalid_fields(queryset, fields, view, request) | ||
if ordering: | ||
return ordering | ||
|
||
# No ordering was included, or all the ordering fields were invalid | ||
return self.get_default_ordering(view) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters