Skip to content

Commit 5207a3f

Browse files
author
Ryan P Kilby
committed
Add 'extra actions' to ViewSet & browsable API
1 parent 41d9998 commit 5207a3f

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

rest_framework/decorators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,12 @@ def action(methods=None, detail=None, name=None, url_path=None, url_name=None, *
148148
def decorator(func):
149149
func.bind_to_methods = methods
150150
func.detail = detail
151+
func.name = name or pretty_name(func.__name__)
151152
func.url_path = url_path or func.__name__
152153
func.url_name = url_name or func.__name__.replace('_', '-')
153154
func.kwargs = kwargs
154155
func.kwargs.update({
155-
'name': name or pretty_name(func.__name__),
156+
'name': func.name,
156157
'description': func.__doc__ or None
157158
})
158159
return func

rest_framework/renderers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,11 @@ def get_description(self, view, status_code):
612612
def get_breadcrumbs(self, request):
613613
return get_breadcrumbs(request.path, request)
614614

615+
def get_extra_actions(self, view):
616+
if hasattr(view, 'get_extra_action_url_map'):
617+
return view.get_extra_action_url_map()
618+
return None
619+
615620
def get_filter_form(self, data, view, request):
616621
if not hasattr(view, 'get_queryset') or not hasattr(view, 'filter_backends'):
617622
return
@@ -698,6 +703,8 @@ def get_context(self, data, accepted_media_type, renderer_context):
698703
'delete_form': self.get_rendered_html_form(data, view, 'DELETE', request),
699704
'options_form': self.get_rendered_html_form(data, view, 'OPTIONS', request),
700705

706+
'extra_actions': self.get_extra_actions(view),
707+
701708
'filter_form': self.get_filter_form(data, view, request),
702709

703710
'raw_data_put_form': raw_data_put_form,

rest_framework/templates/rest_framework/base.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ <h4 class="text-center">Are you sure you want to delete this {{ name }}?</h4>
128128
</div>
129129
{% endif %}
130130

131+
{% if extra_actions %}
132+
<div class="dropdown" style="float: right; margin-right: 10px">
133+
<button class="btn btn-default" id="extra-actions-menu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
134+
{% trans "Extra Actions" %}
135+
<span class="caret"></span>
136+
</button>
137+
<ul class="dropdown-menu" aria-labelledby="extra-actions-menu">
138+
{% for action_name, url in extra_actions|items %}
139+
<li><a href="{{ url }}">{{ action_name }}</a></li>
140+
{% endfor %}
141+
</ul>
142+
</div>
143+
{% endif %}
144+
131145
{% if filter_form %}
132146
<button style="float: right; margin-right: 10px" data-toggle="modal" data-target="#filtersModal" class="btn btn-default">
133147
<span class="glyphicon glyphicon-wrench" aria-hidden="true"></span>

rest_framework/viewsets.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
"""
1919
from __future__ import unicode_literals
2020

21+
from collections import OrderedDict
2122
from functools import update_wrapper
2223
from inspect import getmembers
2324

25+
from django.urls import NoReverseMatch
2426
from django.utils.decorators import classonlymethod
2527
from django.views.decorators.csrf import csrf_exempt
2628

@@ -148,6 +150,33 @@ def get_extra_actions(cls):
148150
"""
149151
return [method for _, method in getmembers(cls, _is_extra_action)]
150152

153+
def get_extra_action_url_map(self):
154+
"""
155+
Build a map of actions: urls for the extra actions. This requires the
156+
`detail` attribute to have been provided.
157+
"""
158+
action_map = OrderedDict()
159+
160+
# exit early if `detail` has not been provided
161+
if self.detail is None:
162+
return action_map
163+
164+
# filter for the relevant extra actions
165+
actions = [
166+
action for action in self.get_extra_actions()
167+
if action.detail == self.detail
168+
]
169+
170+
for action in actions:
171+
try:
172+
url_name = '%s-%s' % (self.basename, action.url_name)
173+
url = reverse(url_name, self.args, self.kwargs, request=self.request)
174+
action_map[action.name] = url
175+
except NoReverseMatch:
176+
pass # do nothing, URL requires additionalargs to reverse
177+
178+
return action_map
179+
151180

152181
class ViewSet(ViewSetMixin, views.APIView):
153182
"""

0 commit comments

Comments
 (0)