-
Notifications
You must be signed in to change notification settings - Fork 300
Add filter feature per JSON API specs. #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -37,3 +37,8 @@ pip-delete-this-directory.txt | |
|
|
||
| # VirtualEnv | ||
| .venv/ | ||
|
|
||
| #python3 pyvenv | ||
| bin/ | ||
| lib64 | ||
| pyvenv.cfg | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import django_filters | ||
|
|
||
| from example.models import Comment | ||
|
|
||
|
|
||
| class CommentFilter(django_filters.FilterSet): | ||
|
|
||
| class Meta: | ||
| model = Comment | ||
| fileds = {'body': ['exact', 'in', 'icontains', 'contains'], | ||
| 'author': ['exact', 'gte', 'lte'], | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| from django.core.urlresolvers import reverse | ||
|
|
||
| from rest_framework.settings import api_settings | ||
|
|
||
| import pytest | ||
|
|
||
| from example.tests.utils import dump_json, redump_json | ||
|
|
||
| pytestmark = pytest.mark.django_db | ||
|
|
||
|
|
||
| class TestJsonApiFilter(object): | ||
|
|
||
| def test_request_without_filter(self, client, comment_factory): | ||
| comment = comment_factory() | ||
| comment2 = comment_factory() | ||
|
|
||
| expected = { | ||
| "links": { | ||
| "first": "http://testserver/comments?page=1", | ||
| "last": "http://testserver/comments?page=2", | ||
| "next": "http://testserver/comments?page=2", | ||
| "prev": None | ||
| }, | ||
| "data": [ | ||
| { | ||
| "type": "comments", | ||
| "id": str(comment.pk), | ||
| "attributes": { | ||
| "body": comment.body | ||
| }, | ||
| "relationships": { | ||
| "entry": { | ||
| "data": { | ||
| "type": "entries", | ||
| "id": str(comment.entry.pk) | ||
| } | ||
| }, | ||
| "author": { | ||
| "data": { | ||
| "type": "authors", | ||
| "id": str(comment.author.pk) | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| ], | ||
| "meta": { | ||
| "pagination": { | ||
| "page": 1, | ||
| "pages": 2, | ||
| "count": 2 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| response = client.get('/comments') | ||
| # assert 0 | ||
|
|
||
| assert response.status_code == 200 | ||
| actual = redump_json(response.content) | ||
| expected_json = dump_json(expected) | ||
| assert actual == expected_json | ||
|
|
||
| def test_request_with_filter(self, client, comment_factory): | ||
| comment = comment_factory(body='Body for comment 1') | ||
| comment2 = comment_factory() | ||
|
|
||
| expected = { | ||
| "links": { | ||
| "first": "http://testserver/comments?filter%5Bbody%5D=Body+for+comment+1&page=1", | ||
| "last": "http://testserver/comments?filter%5Bbody%5D=Body+for+comment+1&page=1", | ||
| "next": None, | ||
| "prev": None | ||
| }, | ||
| "data": [ | ||
| { | ||
| "type": "comments", | ||
| "id": str(comment.pk), | ||
| "attributes": { | ||
| "body": comment.body | ||
| }, | ||
| "relationships": { | ||
| "entry": { | ||
| "data": { | ||
| "type": "entries", | ||
| "id": str(comment.entry.pk) | ||
| } | ||
| }, | ||
| "author": { | ||
| "data": { | ||
| "type": "authors", | ||
| "id": str(comment.author.pk) | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| ], | ||
| "meta": { | ||
| "pagination": { | ||
| "page": 1, | ||
| "pages": 1, | ||
| "count": 1 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| response = client.get('/comments?filter[body]=Body for comment 1') | ||
|
|
||
| assert response.status_code == 200 | ||
| actual = redump_json(response.content) | ||
| expected_json = dump_json(expected) | ||
| assert actual == expected_json | ||
|
|
||
| def test_failed_request_with_filter(self, client, comment_factory): | ||
| comment = comment_factory(body='Body for comment 1') | ||
| comment2 = comment_factory() | ||
|
|
||
| expected = { | ||
| "links": { | ||
| "first": "http://testserver/comments?filter%5Bbody%5D=random+comment&page=1", | ||
| "last": "http://testserver/comments?filter%5Bbody%5D=random+comment&page=1", | ||
| "next": None, | ||
| "prev": None | ||
| }, | ||
| "data": [], | ||
| "meta": { | ||
| "pagination": { | ||
| "page": 1, | ||
| "pages": 1, | ||
| "count": 0 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| response = client.get('/comments?filter[body]=random comment') | ||
| assert response.status_code == 200 | ||
| actual = redump_json(response.content) | ||
| expected_json = dump_json(expected) | ||
| assert actual == expected_json |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -3,5 +3,6 @@ pytest>=2.9.0,<3.0 | |
| pytest-django | ||
| pytest-factoryboy | ||
| fake-factory | ||
| django-filter | ||
| tox | ||
| mock | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| try: | ||
| import rest_framework_filters | ||
| DjangoFilterBackend = rest_framework_filters.backends.DjangoFilterBackend | ||
| except ImportError: | ||
| from rest_framework import filters | ||
| DjangoFilterBackend = filters.DjangoFilterBackend | ||
|
|
||
| from rest_framework_json_api.utils import format_query_params | ||
|
|
||
| class JsonApiFilterBackend(DjangoFilterBackend): | ||
|
|
||
| def filter_queryset(self, request, queryset, view): | ||
|
|
||
| filter_class = self.get_filter_class(view, queryset) | ||
| new_query_params = format_query_params(request.query_params) | ||
| if filter_class: | ||
| return filter_class(new_query_params, queryset=queryset).qs | ||
|
|
||
| return queryset |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There does not seem to be an example of using DRF filters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide a link that shows an example?
DRF filtering depends on
django_filterand similar examples are found in DRF docs.