Fixes for format preservation

This commit is contained in:
Tom Christie 2015-05-19 15:49:37 +01:00
parent f32fff43f0
commit ec8e7f27e7
3 changed files with 28 additions and 8 deletions

View File

@ -586,7 +586,7 @@ class BrowsableAPIRenderer(BaseRenderer):
return view.get_view_description(html=True)
def get_breadcrumbs(self, request):
return get_breadcrumbs(request.path)
return get_breadcrumbs(request.path, request)
def get_context(self, data, accepted_media_type, renderer_context):
"""

View File

@ -10,6 +10,27 @@ from rest_framework.settings import api_settings
from rest_framework.utils.urls import replace_query_param
def preserve_builtin_query_params(url, request=None):
"""
Given an incoming request, and an outgoing URL representation,
append the value of any built-in query parameters.
"""
if request is None:
return url
overrides = [
api_settings.URL_FORMAT_OVERRIDE,
api_settings.URL_ACCEPT_OVERRIDE
]
for param in overrides:
if param and (param in request.GET):
value = request.GET[param]
url = replace_query_param(url, param, value)
return url
def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
"""
If versioning is being used then we pass any `reverse` calls through
@ -27,10 +48,8 @@ def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra
else:
url = _reverse(viewname, args, kwargs, request, format, **extra)
FORMAT_OVERRIDE = api_settings.URL_FORMAT_OVERRIDE
if FORMAT_OVERRIDE and (FORMAT_OVERRIDE in request.query_params):
return replace_query_param(url, FORMAT_OVERRIDE, request.query_params[FORMAT_OVERRIDE])
return url
return preserve_builtin_query_params(url, request)
def _reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
"""

View File

@ -2,12 +2,12 @@ from __future__ import unicode_literals
from django.core.urlresolvers import resolve, get_script_prefix
def get_breadcrumbs(url):
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.settings import api_settings
from rest_framework.views import APIView
@ -33,7 +33,8 @@ def get_breadcrumbs(url):
if not seen or seen[-1] != view:
suffix = getattr(view, 'suffix', None)
name = view_name_func(cls, suffix)
breadcrumbs_list.insert(0, (name, prefix + url))
insert_url = preserve_builtin_query_params(prefix + url, request)
breadcrumbs_list.insert(0, (name, insert_url))
seen.append(view)
if url == '':