mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 04:50:12 +03:00
Allow disabling of view handlers in child class
This commit is contained in:
parent
20c7a24c14
commit
fcda0c1dff
|
@ -210,7 +210,7 @@ class SimpleRouter(BaseRouter):
|
||||||
"""
|
"""
|
||||||
bound_methods = {}
|
bound_methods = {}
|
||||||
for method, action in method_map.items():
|
for method, action in method_map.items():
|
||||||
if hasattr(viewset, action):
|
if getattr(viewset, action, None) is not None:
|
||||||
bound_methods[method] = action
|
bound_methods[method] = action
|
||||||
return bound_methods
|
return bound_methods
|
||||||
|
|
||||||
|
|
|
@ -65,6 +65,18 @@ class EmptyPrefixViewSet(viewsets.ModelViewSet):
|
||||||
return self.queryset[index]
|
return self.queryset[index]
|
||||||
|
|
||||||
|
|
||||||
|
class TestViewSet(viewsets.ViewSet):
|
||||||
|
def list(self, request, *args, **kwargs):
|
||||||
|
return Response({'method': 'list'})
|
||||||
|
|
||||||
|
def create(self, request, *args, **kwargs):
|
||||||
|
return Response({'method': 'create'})
|
||||||
|
|
||||||
|
|
||||||
|
class DisableCreateViewSet(TestViewSet):
|
||||||
|
create = None
|
||||||
|
|
||||||
|
|
||||||
notes_router = SimpleRouter()
|
notes_router = SimpleRouter()
|
||||||
notes_router.register(r'notes', NoteViewSet)
|
notes_router.register(r'notes', NoteViewSet)
|
||||||
|
|
||||||
|
@ -80,6 +92,10 @@ empty_prefix_urls = [
|
||||||
url(r'^', include(empty_prefix_router.urls)),
|
url(r'^', include(empty_prefix_router.urls)),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
diabled_router = SimpleRouter()
|
||||||
|
diabled_router.register(r'test', TestViewSet, base_name='test')
|
||||||
|
diabled_router.register(r'disabled', DisableCreateViewSet, base_name='disable')
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^non-namespaced/', include(namespaced_router.urls)),
|
url(r'^non-namespaced/', include(namespaced_router.urls)),
|
||||||
url(r'^namespaced/', include(namespaced_router.urls, namespace='example', app_name='example')),
|
url(r'^namespaced/', include(namespaced_router.urls, namespace='example', app_name='example')),
|
||||||
|
@ -87,6 +103,7 @@ urlpatterns = [
|
||||||
url(r'^example2/', include(kwarged_notes_router.urls)),
|
url(r'^example2/', include(kwarged_notes_router.urls)),
|
||||||
|
|
||||||
url(r'^empty-prefix/', include(empty_prefix_urls)),
|
url(r'^empty-prefix/', include(empty_prefix_urls)),
|
||||||
|
url(r'^disabled/', include(diabled_router.urls)),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -390,3 +407,23 @@ class TestEmptyPrefix(TestCase):
|
||||||
response = self.client.get('/empty-prefix/1/')
|
response = self.client.get('/empty-prefix/1/')
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert json.loads(response.content.decode('utf-8')) == {'uuid': '111', 'text': 'First'}
|
assert json.loads(response.content.decode('utf-8')) == {'uuid': '111', 'text': 'First'}
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(ROOT_URLCONF='tests.test_routers')
|
||||||
|
class TestDisableRoute(TestCase):
|
||||||
|
"""
|
||||||
|
Ensure route handlers can be disabled by setting them to `None`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_exception_raised_when_action_applied_to_existing_route(self):
|
||||||
|
response = self.client.get('/disabled/test/')
|
||||||
|
assert response.data == {'method': 'list'}
|
||||||
|
|
||||||
|
response = self.client.post('/disabled/test/')
|
||||||
|
assert response.data == {'method': 'create'}
|
||||||
|
|
||||||
|
response = self.client.get('/disabled/disabled/')
|
||||||
|
assert response.data == {'method': 'list'}
|
||||||
|
|
||||||
|
response = self.client.post('/disabled/disabled/')
|
||||||
|
assert response.status_code == 405
|
||||||
|
|
Loading…
Reference in New Issue
Block a user