From 09eeca136e0661fd08d465f2b4a7dc566fac654f Mon Sep 17 00:00:00 2001 From: jang Date: Sun, 25 Aug 2019 19:13:32 +0900 Subject: [PATCH] Add underscore2dash --- rest_framework/decorators.py | 7 ++++++- tests/test_decorators.py | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/rest_framework/decorators.py b/rest_framework/decorators.py index eb1cad9e4..720548835 100644 --- a/rest_framework/decorators.py +++ b/rest_framework/decorators.py @@ -120,7 +120,7 @@ def schema(view_inspector): return decorator -def action(methods=None, detail=None, url_path=None, url_name=None, **kwargs): +def action(methods=None, detail=None, url_path=None, url_name=None, underscore2dash=None, **kwargs): """ Mark a ViewSet method as a routable action. @@ -142,6 +142,11 @@ def action(methods=None, detail=None, url_path=None, url_name=None, **kwargs): func.mapping = MethodMapper(func, methods) func.detail = detail + if url_path: + func.url_path = url_path + else: + func.url_path = func.__name__.replace('_', '-') if underscore2dash else func.__name__ + func.url_path = url_path if url_path else func.__name__ func.url_name = url_name if url_name else func.__name__.replace('_', '-') func.kwargs = kwargs diff --git a/tests/test_decorators.py b/tests/test_decorators.py index e10f0e5c5..758001a7d 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -183,6 +183,13 @@ class ActionDecoratorTestCase(TestCase): 'description': 'Description', } + def test_underscore2dash(self): + @action(detail=True, underscore2dash=True) + def test_action(request): + """Description""" + + assert test_action.url_path == 'test-action' + def test_detail_required(self): with pytest.raises(AssertionError) as excinfo: @action()