Skip to content

Commit 01f568f

Browse files
author
Ryan P Kilby
committed
Replace route decorators with 'action'
1 parent a591e16 commit 01f568f

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

tests/test_routers.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from rest_framework import permissions, serializers, viewsets
1313
from rest_framework.compat import get_regex_pattern
14-
from rest_framework.decorators import detail_route, list_route
14+
from rest_framework.decorators import action
1515
from rest_framework.response import Response
1616
from rest_framework.routers import DefaultRouter, SimpleRouter
1717
from rest_framework.test import APIRequestFactory
@@ -67,12 +67,12 @@ def get_object(self, *args, **kwargs):
6767

6868

6969
class RegexUrlPathViewSet(viewsets.ViewSet):
70-
@list_route(url_path='list/(?P<kwarg>[0-9]{4})')
70+
@action(detail=False, url_path='list/(?P<kwarg>[0-9]{4})')
7171
def regex_url_path_list(self, request, *args, **kwargs):
7272
kwarg = self.kwargs.get('kwarg', '')
7373
return Response({'kwarg': kwarg})
7474

75-
@detail_route(url_path='detail/(?P<kwarg>[0-9]{4})')
75+
@action(detail=True, url_path='detail/(?P<kwarg>[0-9]{4})')
7676
def regex_url_path_detail(self, request, *args, **kwargs):
7777
pk = self.kwargs.get('pk', '')
7878
kwarg = self.kwargs.get('kwarg', '')
@@ -112,23 +112,23 @@ class BasicViewSet(viewsets.ViewSet):
112112
def list(self, request, *args, **kwargs):
113113
return Response({'method': 'list'})
114114

115-
@detail_route(methods=['post'])
115+
@action(methods=['post'], detail=True)
116116
def action1(self, request, *args, **kwargs):
117117
return Response({'method': 'action1'})
118118

119-
@detail_route(methods=['post'])
119+
@action(methods=['post'], detail=True)
120120
def action2(self, request, *args, **kwargs):
121121
return Response({'method': 'action2'})
122122

123-
@detail_route(methods=['post', 'delete'])
123+
@action(methods=['post', 'delete'], detail=True)
124124
def action3(self, request, *args, **kwargs):
125125
return Response({'method': 'action2'})
126126

127-
@detail_route()
127+
@action(detail=True)
128128
def link1(self, request, *args, **kwargs):
129129
return Response({'method': 'link1'})
130130

131-
@detail_route()
131+
@action(detail=True)
132132
def link2(self, request, *args, **kwargs):
133133
return Response({'method': 'link2'})
134134

@@ -297,7 +297,7 @@ def setUp(self):
297297
class TestViewSet(viewsets.ModelViewSet):
298298
permission_classes = []
299299

300-
@detail_route(methods=['post'], permission_classes=[permissions.AllowAny])
300+
@action(methods=['post'], detail=True, permission_classes=[permissions.AllowAny])
301301
def custom(self, request, *args, **kwargs):
302302
return Response({
303303
'permission_classes': self.permission_classes
@@ -315,14 +315,14 @@ def test_action_kwargs(self):
315315

316316
class TestActionAppliedToExistingRoute(TestCase):
317317
"""
318-
Ensure `@detail_route` decorator raises an except when applied
318+
Ensure `@action` decorator raises an except when applied
319319
to an existing route
320320
"""
321321

322322
def test_exception_raised_when_action_applied_to_existing_route(self):
323323
class TestViewSet(viewsets.ModelViewSet):
324324

325-
@detail_route(methods=['post'])
325+
@action(methods=['post'], detail=True)
326326
def retrieve(self, request, *args, **kwargs):
327327
return Response({
328328
'hello': 'world'
@@ -339,27 +339,27 @@ class DynamicListAndDetailViewSet(viewsets.ViewSet):
339339
def list(self, request, *args, **kwargs):
340340
return Response({'method': 'list'})
341341

342-
@list_route(methods=['post'])
342+
@action(methods=['post'], detail=False)
343343
def list_route_post(self, request, *args, **kwargs):
344344
return Response({'method': 'action1'})
345345

346-
@detail_route(methods=['post'])
346+
@action(methods=['post'], detail=True)
347347
def detail_route_post(self, request, *args, **kwargs):
348348
return Response({'method': 'action2'})
349349

350-
@list_route()
350+
@action(detail=False)
351351
def list_route_get(self, request, *args, **kwargs):
352352
return Response({'method': 'link1'})
353353

354-
@detail_route()
354+
@action(detail=True)
355355
def detail_route_get(self, request, *args, **kwargs):
356356
return Response({'method': 'link2'})
357357

358-
@list_route(url_path="list_custom-route")
358+
@action(detail=False, url_path="list_custom-route")
359359
def list_custom_route_get(self, request, *args, **kwargs):
360360
return Response({'method': 'link1'})
361361

362-
@detail_route(url_path="detail_custom-route")
362+
@action(detail=True, url_path="detail_custom-route")
363363
def detail_custom_route_get(self, request, *args, **kwargs):
364364
return Response({'method': 'link2'})
365365

tests/test_schemas.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
filters, generics, pagination, permissions, serializers
1111
)
1212
from rest_framework.compat import coreapi, coreschema, get_regex_pattern, path
13-
from rest_framework.decorators import (
14-
api_view, detail_route, list_route, schema
15-
)
13+
from rest_framework.decorators import action, api_view, schema
1614
from rest_framework.request import Request
1715
from rest_framework.routers import DefaultRouter, SimpleRouter
1816
from rest_framework.schemas import (
@@ -67,25 +65,25 @@ class ExampleViewSet(ModelViewSet):
6765
filter_backends = [filters.OrderingFilter]
6866
serializer_class = ExampleSerializer
6967

70-
@detail_route(methods=['post'], serializer_class=AnotherSerializer)
68+
@action(methods=['post'], detail=True, serializer_class=AnotherSerializer)
7169
def custom_action(self, request, pk):
7270
"""
7371
A description of custom action.
7472
"""
7573
return super(ExampleSerializer, self).retrieve(self, request)
7674

77-
@detail_route(methods=['post'], serializer_class=AnotherSerializerWithListFields)
75+
@action(methods=['post'], detail=True, serializer_class=AnotherSerializerWithListFields)
7876
def custom_action_with_list_fields(self, request, pk):
7977
"""
8078
A custom action using both list field and list serializer in the serializer.
8179
"""
8280
return super(ExampleSerializer, self).retrieve(self, request)
8381

84-
@list_route()
82+
@action(detail=False)
8583
def custom_list_action(self, request):
8684
return super(ExampleViewSet, self).list(self, request)
8785

88-
@list_route(methods=['post', 'get'], serializer_class=EmptySerializer)
86+
@action(methods=['post', 'get'], detail=False, serializer_class=EmptySerializer)
8987
def custom_list_action_multiple_methods(self, request):
9088
return super(ExampleViewSet, self).list(self, request)
9189

@@ -865,11 +863,11 @@ class NamingCollisionViewSet(GenericViewSet):
865863
"""
866864
permision_class = ()
867865

868-
@list_route()
866+
@action(detail=False)
869867
def detail(self, request):
870868
return {}
871869

872-
@list_route(url_path='detail/export')
870+
@action(detail=False, url_path='detail/export')
873871
def detail_export(self, request):
874872
return {}
875873

@@ -1049,7 +1047,7 @@ def options(self, request, *args, **kwargs):
10491047

10501048
class AViewSet(ModelViewSet):
10511049

1052-
@detail_route(methods=['options', 'get'])
1050+
@action(methods=['options', 'get'], detail=True)
10531051
def custom_action(self, request, pk):
10541052
pass
10551053

tests/test_viewsets.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.test import TestCase, override_settings
44

55
from rest_framework import status
6-
from rest_framework.decorators import detail_route, list_route
6+
from rest_framework.decorators import action
77
from rest_framework.response import Response
88
from rest_framework.routers import SimpleRouter
99
from rest_framework.test import APIRequestFactory
@@ -39,19 +39,19 @@ def list(self, request, *args, **kwargs):
3939
def retrieve(self, request, *args, **kwargs):
4040
pass
4141

42-
@list_route()
42+
@action(detail=False)
4343
def list_action(self, request, *args, **kwargs):
4444
pass
4545

46-
@list_route(url_name='list-custom')
46+
@action(detail=False, url_name='list-custom')
4747
def custom_list_action(self, request, *args, **kwargs):
4848
pass
4949

50-
@detail_route()
50+
@action(detail=True)
5151
def detail_action(self, request, *args, **kwargs):
5252
pass
5353

54-
@detail_route(url_name='detail-custom')
54+
@action(detail=True, url_name='detail-custom')
5555
def custom_detail_action(self, request, *args, **kwargs):
5656
pass
5757

0 commit comments

Comments
 (0)