Skip to content

Commit 144c55d

Browse files
author
Ryan P Kilby
committed
Add 'extra actions' to ViewSet & browsable API
1 parent 7dd969d commit 144c55d

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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: 30 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,34 @@ 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 {names: urls} for the extra actions.
156+
157+
This method will noop if `detail` was not provided as a view initkwarg.
158+
"""
159+
action_urls = OrderedDict()
160+
161+
# exit early if `detail` has not been provided
162+
if self.detail is None:
163+
return action_urls
164+
165+
# filter for the relevant extra actions
166+
actions = [
167+
action for action in self.get_extra_actions()
168+
if action.detail == self.detail
169+
]
170+
171+
for action in actions:
172+
try:
173+
url_name = '%s-%s' % (self.basename, action.url_name)
174+
url = reverse(url_name, self.args, self.kwargs, request=self.request)
175+
action_urls[action.name] = url
176+
except NoReverseMatch:
177+
pass # URL requires additional arguments, ignore
178+
179+
return action_urls
180+
151181

152182
class ViewSet(ViewSetMixin, views.APIView):
153183
"""

0 commit comments

Comments
 (0)