Add underscore2dash

This commit is contained in:
jang 2019-08-25 19:13:32 +09:00
parent ec1b14174f
commit 09eeca136e
2 changed files with 13 additions and 1 deletions

View File

@ -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

View File

@ -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()