Skip to content

Commit a47adbc

Browse files
authored
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
1 parent 2843b92 commit a47adbc

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

docs/api-guide/viewsets.md

+7
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ The `action` decorator will route `GET` requests by default, but may also accept
178178
def unset_password(self, request, pk=None):
179179
...
180180

181+
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:
182+
183+
from http import HTTPMethod
184+
185+
@action(detail=True, methods=[HTTPMethod.POST, HTTPMethod.DELETE])
186+
def unset_password(self, request, pk=None):
187+
...
181188

182189
The decorator allows you to override any viewset-level configuration such as `permission_classes`, `serializer_class`, `filter_backends`...:
183190

tests/test_decorators.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
import pytest
24
from django.test import TestCase
35

@@ -187,6 +189,20 @@ def test_action(request):
187189

188190
assert str(excinfo.value) == "@action() missing required argument: 'detail'"
189191

192+
@pytest.mark.skipif(sys.version_info < (3, 11), reason="HTTPMethod was added in Python 3.11")
193+
def test_method_mapping_http_method(self):
194+
from http import HTTPMethod
195+
196+
method_names = [getattr(HTTPMethod, name.upper()) for name in APIView.http_method_names]
197+
198+
@action(detail=False, methods=method_names)
199+
def test_action():
200+
raise NotImplementedError
201+
202+
expected_mapping = {name: test_action.__name__ for name in APIView.http_method_names}
203+
204+
assert test_action.mapping == expected_mapping
205+
190206
def test_method_mapping_http_methods(self):
191207
# All HTTP methods should be mappable
192208
@action(detail=False, methods=[])

0 commit comments

Comments
 (0)