Document support for http.HTTPMethod in the @action decorator added in Python 3.11. (#9067)

* Implement tests for HTTPMethod from Python 3.11

* Update documentation to mention HTTPMethod support in @action
This commit is contained in:
Pavel Král 2023-08-15 07:50:02 +02:00 committed by GitHub
parent 2843b925f9
commit a47adbcd0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -178,6 +178,13 @@ The `action` decorator will route `GET` requests by default, but may also accept
def unset_password(self, request, pk=None):
...
Argument `methods` also supports HTTP methods defined as [HTTPMethod](https://docs.python.org/3/library/http.html#http.HTTPMethod). Example below is identical to the one above:
from http import HTTPMethod
@action(detail=True, methods=[HTTPMethod.POST, HTTPMethod.DELETE])
def unset_password(self, request, pk=None):
...
The decorator allows you to override any viewset-level configuration such as `permission_classes`, `serializer_class`, `filter_backends`...:

View File

@ -1,3 +1,5 @@
import sys
import pytest
from django.test import TestCase
@ -187,6 +189,20 @@ class ActionDecoratorTestCase(TestCase):
assert str(excinfo.value) == "@action() missing required argument: 'detail'"
@pytest.mark.skipif(sys.version_info < (3, 11), reason="HTTPMethod was added in Python 3.11")
def test_method_mapping_http_method(self):
from http import HTTPMethod
method_names = [getattr(HTTPMethod, name.upper()) for name in APIView.http_method_names]
@action(detail=False, methods=method_names)
def test_action():
raise NotImplementedError
expected_mapping = {name: test_action.__name__ for name in APIView.http_method_names}
assert test_action.mapping == expected_mapping
def test_method_mapping_http_methods(self):
# All HTTP methods should be mappable
@action(detail=False, methods=[])