mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 12:00:12 +03:00
Replace list/detail route decorators in tests
This commit is contained in:
parent
a591e16727
commit
151c0c94fd
|
@ -11,7 +11,7 @@ from django.urls import resolve
|
||||||
|
|
||||||
from rest_framework import permissions, serializers, viewsets
|
from rest_framework import permissions, serializers, viewsets
|
||||||
from rest_framework.compat import get_regex_pattern
|
from rest_framework.compat import get_regex_pattern
|
||||||
from rest_framework.decorators import detail_route, list_route
|
from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.routers import DefaultRouter, SimpleRouter
|
from rest_framework.routers import DefaultRouter, SimpleRouter
|
||||||
from rest_framework.test import APIRequestFactory
|
from rest_framework.test import APIRequestFactory
|
||||||
|
@ -67,12 +67,12 @@ class EmptyPrefixViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
|
|
||||||
class RegexUrlPathViewSet(viewsets.ViewSet):
|
class RegexUrlPathViewSet(viewsets.ViewSet):
|
||||||
@list_route(url_path='list/(?P<kwarg>[0-9]{4})')
|
@action(detail=False, url_path='list/(?P<kwarg>[0-9]{4})')
|
||||||
def regex_url_path_list(self, request, *args, **kwargs):
|
def regex_url_path_list(self, request, *args, **kwargs):
|
||||||
kwarg = self.kwargs.get('kwarg', '')
|
kwarg = self.kwargs.get('kwarg', '')
|
||||||
return Response({'kwarg': kwarg})
|
return Response({'kwarg': kwarg})
|
||||||
|
|
||||||
@detail_route(url_path='detail/(?P<kwarg>[0-9]{4})')
|
@action(detail=True, url_path='detail/(?P<kwarg>[0-9]{4})')
|
||||||
def regex_url_path_detail(self, request, *args, **kwargs):
|
def regex_url_path_detail(self, request, *args, **kwargs):
|
||||||
pk = self.kwargs.get('pk', '')
|
pk = self.kwargs.get('pk', '')
|
||||||
kwarg = self.kwargs.get('kwarg', '')
|
kwarg = self.kwargs.get('kwarg', '')
|
||||||
|
@ -112,23 +112,23 @@ class BasicViewSet(viewsets.ViewSet):
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'list'})
|
return Response({'method': 'list'})
|
||||||
|
|
||||||
@detail_route(methods=['post'])
|
@action(methods=['post'], detail=True)
|
||||||
def action1(self, request, *args, **kwargs):
|
def action1(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'action1'})
|
return Response({'method': 'action1'})
|
||||||
|
|
||||||
@detail_route(methods=['post'])
|
@action(methods=['post'], detail=True)
|
||||||
def action2(self, request, *args, **kwargs):
|
def action2(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'action2'})
|
return Response({'method': 'action2'})
|
||||||
|
|
||||||
@detail_route(methods=['post', 'delete'])
|
@action(methods=['post', 'delete'], detail=True)
|
||||||
def action3(self, request, *args, **kwargs):
|
def action3(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'action2'})
|
return Response({'method': 'action2'})
|
||||||
|
|
||||||
@detail_route()
|
@action(detail=True)
|
||||||
def link1(self, request, *args, **kwargs):
|
def link1(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'link1'})
|
return Response({'method': 'link1'})
|
||||||
|
|
||||||
@detail_route()
|
@action(detail=True)
|
||||||
def link2(self, request, *args, **kwargs):
|
def link2(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'link2'})
|
return Response({'method': 'link2'})
|
||||||
|
|
||||||
|
@ -297,7 +297,7 @@ class TestActionKeywordArgs(TestCase):
|
||||||
class TestViewSet(viewsets.ModelViewSet):
|
class TestViewSet(viewsets.ModelViewSet):
|
||||||
permission_classes = []
|
permission_classes = []
|
||||||
|
|
||||||
@detail_route(methods=['post'], permission_classes=[permissions.AllowAny])
|
@action(methods=['post'], detail=True, permission_classes=[permissions.AllowAny])
|
||||||
def custom(self, request, *args, **kwargs):
|
def custom(self, request, *args, **kwargs):
|
||||||
return Response({
|
return Response({
|
||||||
'permission_classes': self.permission_classes
|
'permission_classes': self.permission_classes
|
||||||
|
@ -315,14 +315,14 @@ class TestActionKeywordArgs(TestCase):
|
||||||
|
|
||||||
class TestActionAppliedToExistingRoute(TestCase):
|
class TestActionAppliedToExistingRoute(TestCase):
|
||||||
"""
|
"""
|
||||||
Ensure `@detail_route` decorator raises an except when applied
|
Ensure `@action` decorator raises an except when applied
|
||||||
to an existing route
|
to an existing route
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def test_exception_raised_when_action_applied_to_existing_route(self):
|
def test_exception_raised_when_action_applied_to_existing_route(self):
|
||||||
class TestViewSet(viewsets.ModelViewSet):
|
class TestViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
@detail_route(methods=['post'])
|
@action(methods=['post'], detail=True)
|
||||||
def retrieve(self, request, *args, **kwargs):
|
def retrieve(self, request, *args, **kwargs):
|
||||||
return Response({
|
return Response({
|
||||||
'hello': 'world'
|
'hello': 'world'
|
||||||
|
@ -339,27 +339,27 @@ class DynamicListAndDetailViewSet(viewsets.ViewSet):
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'list'})
|
return Response({'method': 'list'})
|
||||||
|
|
||||||
@list_route(methods=['post'])
|
@action(methods=['post'], detail=False)
|
||||||
def list_route_post(self, request, *args, **kwargs):
|
def list_route_post(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'action1'})
|
return Response({'method': 'action1'})
|
||||||
|
|
||||||
@detail_route(methods=['post'])
|
@action(methods=['post'], detail=True)
|
||||||
def detail_route_post(self, request, *args, **kwargs):
|
def detail_route_post(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'action2'})
|
return Response({'method': 'action2'})
|
||||||
|
|
||||||
@list_route()
|
@action(detail=False)
|
||||||
def list_route_get(self, request, *args, **kwargs):
|
def list_route_get(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'link1'})
|
return Response({'method': 'link1'})
|
||||||
|
|
||||||
@detail_route()
|
@action(detail=True)
|
||||||
def detail_route_get(self, request, *args, **kwargs):
|
def detail_route_get(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'link2'})
|
return Response({'method': 'link2'})
|
||||||
|
|
||||||
@list_route(url_path="list_custom-route")
|
@action(detail=False, url_path="list_custom-route")
|
||||||
def list_custom_route_get(self, request, *args, **kwargs):
|
def list_custom_route_get(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'link1'})
|
return Response({'method': 'link1'})
|
||||||
|
|
||||||
@detail_route(url_path="detail_custom-route")
|
@action(detail=True, url_path="detail_custom-route")
|
||||||
def detail_custom_route_get(self, request, *args, **kwargs):
|
def detail_custom_route_get(self, request, *args, **kwargs):
|
||||||
return Response({'method': 'link2'})
|
return Response({'method': 'link2'})
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,7 @@ from rest_framework import (
|
||||||
filters, generics, pagination, permissions, serializers
|
filters, generics, pagination, permissions, serializers
|
||||||
)
|
)
|
||||||
from rest_framework.compat import coreapi, coreschema, get_regex_pattern, path
|
from rest_framework.compat import coreapi, coreschema, get_regex_pattern, path
|
||||||
from rest_framework.decorators import (
|
from rest_framework.decorators import action, api_view, schema
|
||||||
api_view, detail_route, list_route, schema
|
|
||||||
)
|
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.routers import DefaultRouter, SimpleRouter
|
from rest_framework.routers import DefaultRouter, SimpleRouter
|
||||||
from rest_framework.schemas import (
|
from rest_framework.schemas import (
|
||||||
|
@ -67,25 +65,25 @@ class ExampleViewSet(ModelViewSet):
|
||||||
filter_backends = [filters.OrderingFilter]
|
filter_backends = [filters.OrderingFilter]
|
||||||
serializer_class = ExampleSerializer
|
serializer_class = ExampleSerializer
|
||||||
|
|
||||||
@detail_route(methods=['post'], serializer_class=AnotherSerializer)
|
@action(methods=['post'], detail=True, serializer_class=AnotherSerializer)
|
||||||
def custom_action(self, request, pk):
|
def custom_action(self, request, pk):
|
||||||
"""
|
"""
|
||||||
A description of custom action.
|
A description of custom action.
|
||||||
"""
|
"""
|
||||||
return super(ExampleSerializer, self).retrieve(self, request)
|
return super(ExampleSerializer, self).retrieve(self, request)
|
||||||
|
|
||||||
@detail_route(methods=['post'], serializer_class=AnotherSerializerWithListFields)
|
@action(methods=['post'], detail=True, serializer_class=AnotherSerializerWithListFields)
|
||||||
def custom_action_with_list_fields(self, request, pk):
|
def custom_action_with_list_fields(self, request, pk):
|
||||||
"""
|
"""
|
||||||
A custom action using both list field and list serializer in the serializer.
|
A custom action using both list field and list serializer in the serializer.
|
||||||
"""
|
"""
|
||||||
return super(ExampleSerializer, self).retrieve(self, request)
|
return super(ExampleSerializer, self).retrieve(self, request)
|
||||||
|
|
||||||
@list_route()
|
@action(detail=False)
|
||||||
def custom_list_action(self, request):
|
def custom_list_action(self, request):
|
||||||
return super(ExampleViewSet, self).list(self, request)
|
return super(ExampleViewSet, self).list(self, request)
|
||||||
|
|
||||||
@list_route(methods=['post', 'get'], serializer_class=EmptySerializer)
|
@action(methods=['post', 'get'], detail=False, serializer_class=EmptySerializer)
|
||||||
def custom_list_action_multiple_methods(self, request):
|
def custom_list_action_multiple_methods(self, request):
|
||||||
return super(ExampleViewSet, self).list(self, request)
|
return super(ExampleViewSet, self).list(self, request)
|
||||||
|
|
||||||
|
@ -865,11 +863,11 @@ class NamingCollisionViewSet(GenericViewSet):
|
||||||
"""
|
"""
|
||||||
permision_class = ()
|
permision_class = ()
|
||||||
|
|
||||||
@list_route()
|
@action(detail=False)
|
||||||
def detail(self, request):
|
def detail(self, request):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@list_route(url_path='detail/export')
|
@action(detail=False, url_path='detail/export')
|
||||||
def detail_export(self, request):
|
def detail_export(self, request):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
@ -1049,7 +1047,7 @@ def test_head_and_options_methods_are_excluded():
|
||||||
|
|
||||||
class AViewSet(ModelViewSet):
|
class AViewSet(ModelViewSet):
|
||||||
|
|
||||||
@detail_route(methods=['options', 'get'])
|
@action(methods=['options', 'get'], detail=True)
|
||||||
def custom_action(self, request, pk):
|
def custom_action(self, request, pk):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ from django.db import models
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
|
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.decorators import detail_route, list_route
|
from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.routers import SimpleRouter
|
from rest_framework.routers import SimpleRouter
|
||||||
from rest_framework.test import APIRequestFactory
|
from rest_framework.test import APIRequestFactory
|
||||||
|
@ -39,19 +39,19 @@ class ActionViewSet(GenericViewSet):
|
||||||
def retrieve(self, request, *args, **kwargs):
|
def retrieve(self, request, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@list_route()
|
@action(detail=False)
|
||||||
def list_action(self, request, *args, **kwargs):
|
def list_action(self, request, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@list_route(url_name='list-custom')
|
@action(detail=False, url_name='list-custom')
|
||||||
def custom_list_action(self, request, *args, **kwargs):
|
def custom_list_action(self, request, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@detail_route()
|
@action(detail=True)
|
||||||
def detail_action(self, request, *args, **kwargs):
|
def detail_action(self, request, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@detail_route(url_name='detail-custom')
|
@action(detail=True, url_name='detail-custom')
|
||||||
def custom_detail_action(self, request, *args, **kwargs):
|
def custom_detail_action(self, request, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user