mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 11:30:12 +03:00
Add suffix support for actions
Removes the newly introduced `action.name` in favor of leveraging the View's `.get_view_name()` method, which supports both name and suffix.
This commit is contained in:
parent
20a7734dce
commit
7f1dd7563b
|
@ -131,7 +131,7 @@ def schema(view_inspector):
|
|||
return decorator
|
||||
|
||||
|
||||
def action(methods=None, detail=None, name=None, url_path=None, url_name=None, **kwargs):
|
||||
def action(methods=None, detail=None, url_path=None, url_name=None, **kwargs):
|
||||
"""
|
||||
Mark a ViewSet method as a routable action.
|
||||
|
||||
|
@ -145,18 +145,22 @@ def action(methods=None, detail=None, name=None, url_path=None, url_name=None, *
|
|||
"@action() missing required argument: 'detail'"
|
||||
)
|
||||
|
||||
# name and suffix are mutually exclusive
|
||||
if 'name' in kwargs and 'suffix' in kwargs:
|
||||
raise TypeError("`name` and `suffix` are mutually exclusive arguments.")
|
||||
|
||||
def decorator(func):
|
||||
func.mapping = MethodMapper(func, methods)
|
||||
|
||||
func.detail = detail
|
||||
func.name = name if name else pretty_name(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
|
||||
func.kwargs.update({
|
||||
'name': func.name,
|
||||
'description': func.__doc__ or None
|
||||
})
|
||||
|
||||
# Set descriptive arguments for viewsets
|
||||
if 'name' not in kwargs and 'suffix' not in kwargs:
|
||||
func.kwargs['name'] = pretty_name(func.__name__)
|
||||
func.kwargs['description'] = func.__doc__ or None
|
||||
|
||||
return func
|
||||
return decorator
|
||||
|
|
|
@ -183,7 +183,8 @@ class ViewSetMixin(object):
|
|||
try:
|
||||
url_name = '%s-%s' % (self.basename, action.url_name)
|
||||
url = reverse(url_name, self.args, self.kwargs, request=self.request)
|
||||
action_urls[action.name] = url
|
||||
view = self.__class__(**action.kwargs)
|
||||
action_urls[view.get_view_name()] = url
|
||||
except NoReverseMatch:
|
||||
pass # URL requires additional arguments, ignore
|
||||
|
||||
|
|
|
@ -179,7 +179,6 @@ class ActionDecoratorTestCase(TestCase):
|
|||
|
||||
assert test_action.mapping == {'get': 'test_action'}
|
||||
assert test_action.detail is True
|
||||
assert test_action.name == 'Test action'
|
||||
assert test_action.url_path == 'test_action'
|
||||
assert test_action.url_name == 'test-action'
|
||||
assert test_action.kwargs == {
|
||||
|
@ -223,7 +222,7 @@ class ActionDecoratorTestCase(TestCase):
|
|||
raise NotImplementedError
|
||||
|
||||
# The secondary handler methods should not have the action attributes
|
||||
for name in ['mapping', 'detail', 'name', 'url_path', 'url_name', 'kwargs']:
|
||||
for name in ['mapping', 'detail', 'url_path', 'url_name', 'kwargs']:
|
||||
assert hasattr(test_action, name) and not hasattr(test_action_post, name)
|
||||
|
||||
def test_method_mapping_already_mapped(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user