Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not ignore overridden View.get_view_name() in breadcrumbs #3273

Merged
merged 2 commits into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions rest_framework/utils/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ def get_breadcrumbs(url, request=None):
tuple of (name, url).
"""
from rest_framework.reverse import preserve_builtin_query_params
from rest_framework.settings import api_settings
from rest_framework.views import APIView

view_name_func = api_settings.VIEW_NAME_FUNCTION

def breadcrumbs_recursive(url, breadcrumbs_list, prefix, seen):
"""
Add tuples of (name, url) to the breadcrumbs list,
Expand All @@ -32,8 +29,7 @@ def breadcrumbs_recursive(url, breadcrumbs_list, prefix, seen):
# Don't list the same view twice in a row.
# Probably an optional trailing slash.
if not seen or seen[-1] != view:
suffix = getattr(view, 'suffix', None)
name = view_name_func(cls, suffix)
name = cls().get_view_name()
insert_url = preserve_builtin_query_params(prefix + url, request)
breadcrumbs_list.insert(0, (name, insert_url))
seen.append(view)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ class NestedResourceInstance(APIView):
pass


class CustomNameResourceInstance(APIView):
def get_view_name(self):
return "Foo"


urlpatterns = [
url(r'^$', Root.as_view()),
url(r'^resource/$', ResourceRoot.as_view()),
url(r'^resource/customname$', CustomNameResourceInstance.as_view()),
url(r'^resource/(?P<key>[0-9]+)$', ResourceInstance.as_view()),
url(r'^resource/(?P<key>[0-9]+)/$', NestedResourceRoot.as_view()),
url(r'^resource/(?P<key>[0-9]+)/(?P<other>[A-Za-z]+)$', NestedResourceInstance.as_view()),
Expand Down Expand Up @@ -75,6 +81,17 @@ def test_resource_instance_breadcrumbs(self):
]
)

def test_resource_instance_customname_breadcrumbs(self):
url = '/resource/customname'
self.assertEqual(
get_breadcrumbs(url),
[
('Root', '/'),
('Resource Root', '/resource/'),
('Foo', '/resource/customname')
]
)

def test_nested_resource_breadcrumbs(self):
url = '/resource/123/'
self.assertEqual(
Expand Down