Breadcrumb view name suffix fix (#4750)

* add failing testcase for breadcrumb suffixes missing when using ModelViewSets

* fix get_breadcrumbs to honor overridden get_view_name and keep viewset suffixes

* ensure suffixes are appended in breadcrumb util
This commit is contained in:
Matthew Medal 2016-12-20 14:19:00 -08:00 committed by Tom Christie
parent f38734ef98
commit 93fe531dea
2 changed files with 25 additions and 1 deletions

View File

@ -28,7 +28,9 @@ def get_breadcrumbs(url, request=None):
# Don't list the same view twice in a row.
# Probably an optional trailing slash.
if not seen or seen[-1] != view:
name = cls().get_view_name()
c = cls()
c.suffix = getattr(view, 'suffix', None)
name = c.get_view_name()
insert_url = preserve_builtin_query_params(prefix + url, request)
breadcrumbs_list.insert(0, (name, insert_url))
seen.append(view)

View File

@ -7,8 +7,11 @@ from django.utils import six
import rest_framework.utils.model_meta
from rest_framework.compat import _resolve_model
from rest_framework.routers import SimpleRouter
from rest_framework.serializers import ModelSerializer
from rest_framework.utils.breadcrumbs import get_breadcrumbs
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from tests.models import BasicModel
@ -37,6 +40,13 @@ class CustomNameResourceInstance(APIView):
return "Foo"
class ResourceViewSet(ModelViewSet):
serializer_class = ModelSerializer
queryset = BasicModel.objects.all()
router = SimpleRouter()
router.register(r'resources', ResourceViewSet)
urlpatterns = [
url(r'^$', Root.as_view()),
url(r'^resource/$', ResourceRoot.as_view()),
@ -45,6 +55,7 @@ urlpatterns = [
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()),
]
urlpatterns += router.urls
@override_settings(ROOT_URLCONF='tests.test_utils')
@ -123,6 +134,17 @@ class BreadcrumbTests(TestCase):
[('Root', '/')]
)
def test_modelviewset_resource_instance_breadcrumbs(self):
url = '/resources/1/'
self.assertEqual(
get_breadcrumbs(url),
[
('Root', '/'),
('Resource List', '/resources/'),
('Resource Instance', '/resources/1/')
]
)
class ResolveModelTests(TestCase):
"""