11
11
12
12
from rest_framework import permissions , serializers , viewsets
13
13
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
15
15
from rest_framework .response import Response
16
16
from rest_framework .routers import DefaultRouter , SimpleRouter
17
17
from rest_framework .test import APIRequestFactory
@@ -67,12 +67,12 @@ def get_object(self, *args, **kwargs):
67
67
68
68
69
69
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})' )
71
71
def regex_url_path_list (self , request , * args , ** kwargs ):
72
72
kwarg = self .kwargs .get ('kwarg' , '' )
73
73
return Response ({'kwarg' : kwarg })
74
74
75
- @detail_route ( url_path = 'detail/(?P<kwarg>[0-9]{4})' )
75
+ @action ( detail = True , url_path = 'detail/(?P<kwarg>[0-9]{4})' )
76
76
def regex_url_path_detail (self , request , * args , ** kwargs ):
77
77
pk = self .kwargs .get ('pk' , '' )
78
78
kwarg = self .kwargs .get ('kwarg' , '' )
@@ -112,23 +112,23 @@ class BasicViewSet(viewsets.ViewSet):
112
112
def list (self , request , * args , ** kwargs ):
113
113
return Response ({'method' : 'list' })
114
114
115
- @detail_route (methods = ['post' ])
115
+ @action (methods = ['post' ], detail = True )
116
116
def action1 (self , request , * args , ** kwargs ):
117
117
return Response ({'method' : 'action1' })
118
118
119
- @detail_route (methods = ['post' ])
119
+ @action (methods = ['post' ], detail = True )
120
120
def action2 (self , request , * args , ** kwargs ):
121
121
return Response ({'method' : 'action2' })
122
122
123
- @detail_route (methods = ['post' , 'delete' ])
123
+ @action (methods = ['post' , 'delete' ], detail = True )
124
124
def action3 (self , request , * args , ** kwargs ):
125
125
return Response ({'method' : 'action2' })
126
126
127
- @detail_route ( )
127
+ @action ( detail = True )
128
128
def link1 (self , request , * args , ** kwargs ):
129
129
return Response ({'method' : 'link1' })
130
130
131
- @detail_route ( )
131
+ @action ( detail = True )
132
132
def link2 (self , request , * args , ** kwargs ):
133
133
return Response ({'method' : 'link2' })
134
134
@@ -297,7 +297,7 @@ def setUp(self):
297
297
class TestViewSet (viewsets .ModelViewSet ):
298
298
permission_classes = []
299
299
300
- @detail_route (methods = ['post' ], permission_classes = [permissions .AllowAny ])
300
+ @action (methods = ['post' ], detail = True , permission_classes = [permissions .AllowAny ])
301
301
def custom (self , request , * args , ** kwargs ):
302
302
return Response ({
303
303
'permission_classes' : self .permission_classes
@@ -315,14 +315,14 @@ def test_action_kwargs(self):
315
315
316
316
class TestActionAppliedToExistingRoute (TestCase ):
317
317
"""
318
- Ensure `@detail_route ` decorator raises an except when applied
318
+ Ensure `@action ` decorator raises an except when applied
319
319
to an existing route
320
320
"""
321
321
322
322
def test_exception_raised_when_action_applied_to_existing_route (self ):
323
323
class TestViewSet (viewsets .ModelViewSet ):
324
324
325
- @detail_route (methods = ['post' ])
325
+ @action (methods = ['post' ], detail = True )
326
326
def retrieve (self , request , * args , ** kwargs ):
327
327
return Response ({
328
328
'hello' : 'world'
@@ -339,27 +339,27 @@ class DynamicListAndDetailViewSet(viewsets.ViewSet):
339
339
def list (self , request , * args , ** kwargs ):
340
340
return Response ({'method' : 'list' })
341
341
342
- @list_route (methods = ['post' ])
342
+ @action (methods = ['post' ], detail = False )
343
343
def list_route_post (self , request , * args , ** kwargs ):
344
344
return Response ({'method' : 'action1' })
345
345
346
- @detail_route (methods = ['post' ])
346
+ @action (methods = ['post' ], detail = True )
347
347
def detail_route_post (self , request , * args , ** kwargs ):
348
348
return Response ({'method' : 'action2' })
349
349
350
- @list_route ( )
350
+ @action ( detail = False )
351
351
def list_route_get (self , request , * args , ** kwargs ):
352
352
return Response ({'method' : 'link1' })
353
353
354
- @detail_route ( )
354
+ @action ( detail = True )
355
355
def detail_route_get (self , request , * args , ** kwargs ):
356
356
return Response ({'method' : 'link2' })
357
357
358
- @list_route ( url_path = "list_custom-route" )
358
+ @action ( detail = False , url_path = "list_custom-route" )
359
359
def list_custom_route_get (self , request , * args , ** kwargs ):
360
360
return Response ({'method' : 'link1' })
361
361
362
- @detail_route ( url_path = "detail_custom-route" )
362
+ @action ( detail = True , url_path = "detail_custom-route" )
363
363
def detail_custom_route_get (self , request , * args , ** kwargs ):
364
364
return Response ({'method' : 'link2' })
365
365
0 commit comments