-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Add SimplePathRouter #6789
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
Merged
Merged
Add SimplePathRouter #6789
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
765d728
Allow usage of Django 2.x path in SimpleRouter
sevdog 1486e73
Use path in Default router
sevdog a0ebb51
Update docs/api-guide/routers.md
auvipy 06a9244
Update docs/api-guide/routers.md
auvipy ff69c69
Add tests also for default router with path
sevdog 4f232c7
Use a more relevant attribute for lookup when using path converters
sevdog 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
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 |
---|---|---|
|
@@ -10,7 +10,9 @@ | |
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
from rest_framework.routers import DefaultRouter, SimpleRouter | ||
from rest_framework.test import APIRequestFactory, URLPatternsTestCase | ||
from rest_framework.test import ( | ||
APIClient, APIRequestFactory, URLPatternsTestCase | ||
) | ||
from rest_framework.utils import json | ||
|
||
factory = APIRequestFactory() | ||
|
@@ -75,9 +77,28 @@ def regex_url_path_detail(self, request, *args, **kwargs): | |
return Response({'pk': pk, 'kwarg': kwarg}) | ||
|
||
|
||
class UrlPathViewSet(viewsets.ViewSet): | ||
@action(detail=False, url_path='list/<int:kwarg>') | ||
def url_path_list(self, request, *args, **kwargs): | ||
kwarg = self.kwargs.get('kwarg', '') | ||
return Response({'kwarg': kwarg}) | ||
|
||
@action(detail=True, url_path='detail/<int:kwarg>') | ||
def url_path_detail(self, request, *args, **kwargs): | ||
pk = self.kwargs.get('pk', '') | ||
kwarg = self.kwargs.get('kwarg', '') | ||
return Response({'pk': pk, 'kwarg': kwarg}) | ||
|
||
|
||
notes_router = SimpleRouter() | ||
notes_router.register(r'notes', NoteViewSet) | ||
|
||
notes_path_router = SimpleRouter(use_regex_path=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add another test case which use DeafultRouter please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should not be a problem to replicate that test also with the other router. |
||
notes_path_router.register('notes', NoteViewSet) | ||
|
||
notes_path_default_router = DefaultRouter(use_regex_path=False) | ||
notes_path_default_router.register('notes', NoteViewSet) | ||
|
||
kwarged_notes_router = SimpleRouter() | ||
kwarged_notes_router.register(r'notes', KWargedNoteViewSet) | ||
|
||
|
@@ -90,6 +111,9 @@ def regex_url_path_detail(self, request, *args, **kwargs): | |
regex_url_path_router = SimpleRouter() | ||
regex_url_path_router.register(r'', RegexUrlPathViewSet, basename='regex') | ||
|
||
url_path_router = SimpleRouter(use_regex_path=False) | ||
url_path_router.register('', UrlPathViewSet, basename='path') | ||
|
||
|
||
class BasicViewSet(viewsets.ViewSet): | ||
def list(self, request, *args, **kwargs): | ||
|
@@ -459,6 +483,81 @@ def test_regex_url_path_detail(self): | |
assert json.loads(response.content.decode()) == {'pk': pk, 'kwarg': kwarg} | ||
|
||
|
||
class TestUrlPath(URLPatternsTestCase, TestCase): | ||
auvipy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client_class = APIClient | ||
urlpatterns = [ | ||
path('path/', include(url_path_router.urls)), | ||
path('default/', include(notes_path_default_router.urls)), | ||
path('example/', include(notes_path_router.urls)), | ||
] | ||
|
||
def setUp(self): | ||
RouterTestModel.objects.create(uuid='123', text='foo bar') | ||
RouterTestModel.objects.create(uuid='a b', text='baz qux') | ||
|
||
def test_create(self): | ||
new_note = { | ||
'uuid': 'foo', | ||
'text': 'example' | ||
} | ||
response = self.client.post('/example/notes/', data=new_note) | ||
assert response.status_code == 201 | ||
assert response['location'] == 'http://testserver/example/notes/foo/' | ||
assert response.data == {"url": "http://testserver/example/notes/foo/", "uuid": "foo", "text": "example"} | ||
assert RouterTestModel.objects.filter(uuid='foo').exists() | ||
|
||
def test_retrieve(self): | ||
for url in ('/example/notes/123/', '/default/notes/123/'): | ||
with self.subTest(url=url): | ||
response = self.client.get(url) | ||
assert response.status_code == 200 | ||
# only gets example path since was the last to be registered | ||
assert response.data == {"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"} | ||
|
||
def test_list(self): | ||
for url in ('/example/notes/', '/default/notes/'): | ||
with self.subTest(url=url): | ||
response = self.client.get(url) | ||
assert response.status_code == 200 | ||
# only gets example path since was the last to be registered | ||
assert response.data == [ | ||
{"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"}, | ||
{"url": "http://testserver/example/notes/a%20b/", "uuid": "a b", "text": "baz qux"}, | ||
] | ||
|
||
def test_update(self): | ||
updated_note = { | ||
'text': 'foo bar example' | ||
} | ||
response = self.client.patch('/example/notes/123/', data=updated_note) | ||
assert response.status_code == 200 | ||
assert response.data == {"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar example"} | ||
|
||
def test_delete(self): | ||
response = self.client.delete('/example/notes/123/') | ||
assert response.status_code == 204 | ||
assert not RouterTestModel.objects.filter(uuid='123').exists() | ||
|
||
def test_list_extra_action(self): | ||
kwarg = 1234 | ||
response = self.client.get('/path/list/{}/'.format(kwarg)) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content.decode()) == {'kwarg': kwarg} | ||
|
||
def test_detail_extra_action(self): | ||
pk = '1' | ||
kwarg = 1234 | ||
response = self.client.get('/path/{}/detail/{}/'.format(pk, kwarg)) | ||
assert response.status_code == 200 | ||
assert json.loads(response.content.decode()) == {'pk': pk, 'kwarg': kwarg} | ||
|
||
def test_defaultrouter_root(self): | ||
response = self.client.get('/default/') | ||
assert response.status_code == 200 | ||
# only gets example path since was the last to be registered | ||
assert response.data == {"notes": "http://testserver/example/notes/"} | ||
|
||
|
||
class TestViewInitkwargs(URLPatternsTestCase, TestCase): | ||
urlpatterns = [ | ||
path('example/', include(notes_router.urls)), | ||
|
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.
can we use better name for lookup_value_converter?any better suited name instead of converter?
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.
I used "converter" since django calls these elements path converters, maybe we can use the full expanded name to have
lookup_value_pathconverter
(orlookup_value_path_converter
).Any suggestion is welcome.
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.
I would inclined to @tomchristie for a better insight here as I'm bit confused about naming
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.
what about just lookup_value_path? this alighned with the name lookup_value_regex
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.
I think that it would be better to use the same naming of django.
In this case it was named "converter" thus an attribute which should define one should be named after that. In this way it is easier to find references to what this element does.
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.
OK I am going to merge this, we can improve it if needed