Breadcrumbs play nicely when app not installed at root URL. Fixes #211

This commit is contained in:
Tom Christie 2012-09-27 13:46:20 +01:00
parent f741bab709
commit 689d2afd97
5 changed files with 117 additions and 7 deletions

View File

@ -1,11 +1,12 @@
from django.core.urlresolvers import resolve from django.core.urlresolvers import resolve, get_script_prefix
def get_breadcrumbs(url): def get_breadcrumbs(url):
"""Given a url returns a list of breadcrumbs, which are each a tuple of (name, url).""" """Given a url returns a list of breadcrumbs, which are each a tuple of (name, url)."""
from djangorestframework.views import View from djangorestframework.views import View
def breadcrumbs_recursive(url, breadcrumbs_list): def breadcrumbs_recursive(url, breadcrumbs_list, prefix):
"""Add tuples of (name, url) to the breadcrumbs list, progressively chomping off parts of the url.""" """Add tuples of (name, url) to the breadcrumbs list, progressively chomping off parts of the url."""
try: try:
@ -15,7 +16,7 @@ def get_breadcrumbs(url):
else: else:
# Check if this is a REST framework view, and if so add it to the breadcrumbs # Check if this is a REST framework view, and if so add it to the breadcrumbs
if isinstance(getattr(view, 'cls_instance', None), View): if isinstance(getattr(view, 'cls_instance', None), View):
breadcrumbs_list.insert(0, (view.cls_instance.get_name(), url)) breadcrumbs_list.insert(0, (view.cls_instance.get_name(), prefix + url))
if url == '': if url == '':
# All done # All done
@ -23,10 +24,11 @@ def get_breadcrumbs(url):
elif url.endswith('/'): elif url.endswith('/'):
# Drop trailing slash off the end and continue to try to resolve more breadcrumbs # Drop trailing slash off the end and continue to try to resolve more breadcrumbs
return breadcrumbs_recursive(url.rstrip('/'), breadcrumbs_list) return breadcrumbs_recursive(url.rstrip('/'), breadcrumbs_list, prefix)
# Drop trailing non-slash off the end and continue to try to resolve more breadcrumbs # Drop trailing non-slash off the end and continue to try to resolve more breadcrumbs
return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list) return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list, prefix)
return breadcrumbs_recursive(url, [])
prefix = get_script_prefix().rstrip('/')
url = url[len(prefix):]
return breadcrumbs_recursive(url, [], prefix)

View File

@ -0,0 +1,19 @@
Metadata-Version: 1.0
Name: rest-framework
Version: 2.0.0
Summary: A lightweight REST framework for Django.
Home-page: http://django-rest-framework.org
Author: Tom Christie
Author-email: tom@tomchristie.com
License: BSD
Download-URL: http://pypi.python.org/pypi/rest_framework/
Description: UNKNOWN
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Topic :: Internet :: WWW/HTTP

View File

@ -0,0 +1,81 @@
MANIFEST.in
setup.py
rest_framework/__init__.py
rest_framework/authentication.py
rest_framework/compat.py
rest_framework/decorators.py
rest_framework/exceptions.py
rest_framework/fields.py
rest_framework/generics.py
rest_framework/mixins.py
rest_framework/models.py
rest_framework/negotiation.py
rest_framework/parsers.py
rest_framework/permissions.py
rest_framework/renderers.py
rest_framework/request.py
rest_framework/resources.py
rest_framework/response.py
rest_framework/reverse.py
rest_framework/serializers.py
rest_framework/settings.py
rest_framework/status.py
rest_framework/throttling.py
rest_framework/urlpatterns.py
rest_framework/urls.py
rest_framework/views.py
rest_framework.egg-info/PKG-INFO
rest_framework.egg-info/SOURCES.txt
rest_framework.egg-info/dependency_links.txt
rest_framework.egg-info/top_level.txt
rest_framework/authtoken/__init__.py
rest_framework/authtoken/models.py
rest_framework/authtoken/views.py
rest_framework/authtoken/migrations/0001_initial.py
rest_framework/authtoken/migrations/__init__.py
rest_framework/runtests/__init__.py
rest_framework/runtests/runcoverage.py
rest_framework/runtests/runtests.py
rest_framework/runtests/settings.py
rest_framework/runtests/urls.py
rest_framework/static/rest_framework/css/bootstrap-tweaks.css
rest_framework/static/rest_framework/css/bootstrap.min.css
rest_framework/static/rest_framework/css/default.css
rest_framework/static/rest_framework/css/prettify.css
rest_framework/static/rest_framework/img/glyphicons-halflings-white.png
rest_framework/static/rest_framework/img/glyphicons-halflings.png
rest_framework/static/rest_framework/js/bootstrap.min.js
rest_framework/static/rest_framework/js/default.js
rest_framework/static/rest_framework/js/jquery-1.8.1-min.js
rest_framework/static/rest_framework/js/prettify-min.js
rest_framework/templates/rest_framework/api.html
rest_framework/templates/rest_framework/base.html
rest_framework/templates/rest_framework/login.html
rest_framework/templatetags/__init__.py
rest_framework/templatetags/rest_framework.py
rest_framework/tests/__init__.py
rest_framework/tests/authentication.py
rest_framework/tests/breadcrumbs.py
rest_framework/tests/decorators.py
rest_framework/tests/description.py
rest_framework/tests/files.py
rest_framework/tests/methods.py
rest_framework/tests/mixins.py
rest_framework/tests/models.py
rest_framework/tests/modelviews.py
rest_framework/tests/oauthentication.py
rest_framework/tests/parsers.py
rest_framework/tests/renderers.py
rest_framework/tests/request.py
rest_framework/tests/response.py
rest_framework/tests/reverse.py
rest_framework/tests/serializer.py
rest_framework/tests/status.py
rest_framework/tests/testcases.py
rest_framework/tests/throttling.py
rest_framework/tests/validators.py
rest_framework/tests/views.py
rest_framework/utils/__init__.py
rest_framework/utils/breadcrumbs.py
rest_framework/utils/encoders.py
rest_framework/utils/mediatypes.py

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,7 @@
rest_framework/authtoken
rest_framework/utils
rest_framework/tests
rest_framework/runtests
rest_framework/templatetags
rest_framework
rest_framework/authtoken/migrations