mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
0148a9f8da
* View suffix already set by initializer * Add 'name' and 'description' attributes to ViewSet ViewSets may now provide their `name` and `description` attributes directly, instead of relying on view introspection to derive them. These attributes may also be provided with the view's initkwargs. The ViewSet `name` and `suffix` initkwargs are mutually exclusive. The `action` decorator now provides the `name` and `description` to the view's initkwargs. By default, these values are derived from the method name and its docstring. The `name` may be overridden by providing it as an argument to the decorator. The `get_view_name` and `get_view_description` hooks now provide the view instance to the handler, instead of the view class. The default implementations of these handlers now respect the `name`/`description`. * Add 'extra actions' to ViewSet & browsable APIs * Update simple router tests Removed old test logic around link/action decorators from `v2.3`. Also simplified the test by making the results explicit instead of computed. * Add method mapping to ViewSet actions * Document extra action method mapping
56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
from __future__ import unicode_literals
|
|
|
|
from django.urls import get_script_prefix, resolve
|
|
|
|
|
|
def get_breadcrumbs(url, request=None):
|
|
"""
|
|
Given a url returns a list of breadcrumbs, which are each a
|
|
tuple of (name, url).
|
|
"""
|
|
from rest_framework.reverse import preserve_builtin_query_params
|
|
from rest_framework.views import APIView
|
|
|
|
def breadcrumbs_recursive(url, breadcrumbs_list, prefix, seen):
|
|
"""
|
|
Add tuples of (name, url) to the breadcrumbs list,
|
|
progressively chomping off parts of the url.
|
|
"""
|
|
try:
|
|
(view, unused_args, unused_kwargs) = resolve(url)
|
|
except Exception:
|
|
pass
|
|
else:
|
|
# Check if this is a REST framework view,
|
|
# and if so add it to the breadcrumbs
|
|
cls = getattr(view, 'cls', None)
|
|
initkwargs = getattr(view, 'initkwargs', {})
|
|
if cls is not None and issubclass(cls, APIView):
|
|
# Don't list the same view twice in a row.
|
|
# Probably an optional trailing slash.
|
|
if not seen or seen[-1] != view:
|
|
c = cls(**initkwargs)
|
|
name = c.get_view_name()
|
|
insert_url = preserve_builtin_query_params(prefix + url, request)
|
|
breadcrumbs_list.insert(0, (name, insert_url))
|
|
seen.append(view)
|
|
|
|
if url == '':
|
|
# All done
|
|
return breadcrumbs_list
|
|
|
|
elif url.endswith('/'):
|
|
# Drop trailing slash off the end and continue to try to
|
|
# resolve more breadcrumbs
|
|
url = url.rstrip('/')
|
|
return breadcrumbs_recursive(url, breadcrumbs_list, prefix, seen)
|
|
|
|
# Drop trailing non-slash off the end and continue to try to
|
|
# resolve more breadcrumbs
|
|
url = url[:url.rfind('/') + 1]
|
|
return breadcrumbs_recursive(url, breadcrumbs_list, prefix, seen)
|
|
|
|
prefix = get_script_prefix().rstrip('/')
|
|
url = url[len(prefix):]
|
|
return breadcrumbs_recursive(url, [], prefix, [])
|