mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 17:39:48 +03:00
Remove detail_route/list_route
This commit is contained in:
parent
e0882a52e4
commit
11fe6a199f
|
@ -7,11 +7,9 @@ based views, as well as the `@detail_route` and `@list_route` decorators, which
|
||||||
used to annotate methods on viewsets that should be included by routers.
|
used to annotate methods on viewsets that should be included by routers.
|
||||||
"""
|
"""
|
||||||
import types
|
import types
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django.forms.utils import pretty_name
|
from django.forms.utils import pretty_name
|
||||||
|
|
||||||
from rest_framework import RemovedInDRF310Warning
|
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
|
|
||||||
|
@ -214,39 +212,3 @@ class MethodMapper(dict):
|
||||||
|
|
||||||
def trace(self, func):
|
def trace(self, func):
|
||||||
return self._map('trace', func)
|
return self._map('trace', func)
|
||||||
|
|
||||||
|
|
||||||
def detail_route(methods=None, **kwargs):
|
|
||||||
"""
|
|
||||||
Used to mark a method on a ViewSet that should be routed for detail requests.
|
|
||||||
"""
|
|
||||||
warnings.warn(
|
|
||||||
"`detail_route` is deprecated and will be removed in 3.10 in favor of "
|
|
||||||
"`action`, which accepts a `detail` bool. Use `@action(detail=True)` instead.",
|
|
||||||
RemovedInDRF310Warning, stacklevel=2
|
|
||||||
)
|
|
||||||
|
|
||||||
def decorator(func):
|
|
||||||
func = action(methods, detail=True, **kwargs)(func)
|
|
||||||
if 'url_name' not in kwargs:
|
|
||||||
func.url_name = func.url_path.replace('_', '-')
|
|
||||||
return func
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
|
|
||||||
def list_route(methods=None, **kwargs):
|
|
||||||
"""
|
|
||||||
Used to mark a method on a ViewSet that should be routed for list requests.
|
|
||||||
"""
|
|
||||||
warnings.warn(
|
|
||||||
"`list_route` is deprecated and will be removed in 3.10 in favor of "
|
|
||||||
"`action`, which accepts a `detail` bool. Use `@action(detail=False)` instead.",
|
|
||||||
RemovedInDRF310Warning, stacklevel=2
|
|
||||||
)
|
|
||||||
|
|
||||||
def decorator(func):
|
|
||||||
func = action(methods, detail=False, **kwargs)(func)
|
|
||||||
if 'url_name' not in kwargs:
|
|
||||||
func.url_name = func.url_path.replace('_', '-')
|
|
||||||
return func
|
|
||||||
return decorator
|
|
||||||
|
|
|
@ -22,9 +22,7 @@ from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.urls import NoReverseMatch
|
from django.urls import NoReverseMatch
|
||||||
from django.utils.deprecation import RenameMethodsBase
|
from django.utils.deprecation import RenameMethodsBase
|
||||||
|
|
||||||
from rest_framework import (
|
from rest_framework import RemovedInDRF311Warning, views
|
||||||
RemovedInDRF310Warning, RemovedInDRF311Warning, views
|
|
||||||
)
|
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
from rest_framework.schemas import SchemaGenerator
|
from rest_framework.schemas import SchemaGenerator
|
||||||
|
@ -36,28 +34,6 @@ Route = namedtuple('Route', ['url', 'mapping', 'name', 'detail', 'initkwargs'])
|
||||||
DynamicRoute = namedtuple('DynamicRoute', ['url', 'name', 'detail', 'initkwargs'])
|
DynamicRoute = namedtuple('DynamicRoute', ['url', 'name', 'detail', 'initkwargs'])
|
||||||
|
|
||||||
|
|
||||||
class DynamicDetailRoute:
|
|
||||||
def __new__(cls, url, name, initkwargs):
|
|
||||||
warnings.warn(
|
|
||||||
"`DynamicDetailRoute` is deprecated and will be removed in 3.10 "
|
|
||||||
"in favor of `DynamicRoute`, which accepts a `detail` boolean. Use "
|
|
||||||
"`DynamicRoute(url, name, True, initkwargs)` instead.",
|
|
||||||
RemovedInDRF310Warning, stacklevel=2
|
|
||||||
)
|
|
||||||
return DynamicRoute(url, name, True, initkwargs)
|
|
||||||
|
|
||||||
|
|
||||||
class DynamicListRoute:
|
|
||||||
def __new__(cls, url, name, initkwargs):
|
|
||||||
warnings.warn(
|
|
||||||
"`DynamicListRoute` is deprecated and will be removed in 3.10 in "
|
|
||||||
"favor of `DynamicRoute`, which accepts a `detail` boolean. Use "
|
|
||||||
"`DynamicRoute(url, name, False, initkwargs)` instead.",
|
|
||||||
RemovedInDRF310Warning, stacklevel=2
|
|
||||||
)
|
|
||||||
return DynamicRoute(url, name, False, initkwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def escape_curly_brackets(url_path):
|
def escape_curly_brackets(url_path):
|
||||||
"""
|
"""
|
||||||
Double brackets in regex of url_path for escape string formatting
|
Double brackets in regex of url_path for escape string formatting
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
import pytest
|
import pytest
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from rest_framework import RemovedInDRF310Warning, status
|
from rest_framework import status
|
||||||
from rest_framework.authentication import BasicAuthentication
|
from rest_framework.authentication import BasicAuthentication
|
||||||
from rest_framework.decorators import (
|
from rest_framework.decorators import (
|
||||||
action, api_view, authentication_classes, detail_route, list_route,
|
action, api_view, authentication_classes, parser_classes,
|
||||||
parser_classes, permission_classes, renderer_classes, schema,
|
permission_classes, renderer_classes, schema, throttle_classes
|
||||||
throttle_classes
|
|
||||||
)
|
)
|
||||||
from rest_framework.parsers import JSONParser
|
from rest_framework.parsers import JSONParser
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
@ -285,39 +284,3 @@ class ActionDecoratorTestCase(TestCase):
|
||||||
@test_action.mapping.post
|
@test_action.mapping.post
|
||||||
def test_action():
|
def test_action():
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def test_detail_route_deprecation(self):
|
|
||||||
with pytest.warns(RemovedInDRF310Warning) as record:
|
|
||||||
@detail_route()
|
|
||||||
def view(request):
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
assert len(record) == 1
|
|
||||||
assert str(record[0].message) == (
|
|
||||||
"`detail_route` is deprecated and will be removed in "
|
|
||||||
"3.10 in favor of `action`, which accepts a `detail` bool. Use "
|
|
||||||
"`@action(detail=True)` instead."
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_list_route_deprecation(self):
|
|
||||||
with pytest.warns(RemovedInDRF310Warning) as record:
|
|
||||||
@list_route()
|
|
||||||
def view(request):
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
assert len(record) == 1
|
|
||||||
assert str(record[0].message) == (
|
|
||||||
"`list_route` is deprecated and will be removed in "
|
|
||||||
"3.10 in favor of `action`, which accepts a `detail` bool. Use "
|
|
||||||
"`@action(detail=False)` instead."
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_route_url_name_from_path(self):
|
|
||||||
# pre-3.8 behavior was to base the `url_name` off of the `url_path`
|
|
||||||
with pytest.warns(RemovedInDRF310Warning):
|
|
||||||
@list_route(url_path='foo_bar')
|
|
||||||
def view(request):
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
assert view.url_path == 'foo_bar'
|
|
||||||
assert view.url_name == 'foo-bar'
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user