mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Fixes for format preservation
This commit is contained in:
parent
f32fff43f0
commit
ec8e7f27e7
|
@ -586,7 +586,7 @@ class BrowsableAPIRenderer(BaseRenderer):
|
||||||
return view.get_view_description(html=True)
|
return view.get_view_description(html=True)
|
||||||
|
|
||||||
def get_breadcrumbs(self, request):
|
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):
|
def get_context(self, data, accepted_media_type, renderer_context):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -10,6 +10,27 @@ from rest_framework.settings import api_settings
|
||||||
from rest_framework.utils.urls import replace_query_param
|
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):
|
def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
|
||||||
"""
|
"""
|
||||||
If versioning is being used then we pass any `reverse` calls through
|
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:
|
else:
|
||||||
url = _reverse(viewname, args, kwargs, request, format, **extra)
|
url = _reverse(viewname, args, kwargs, request, format, **extra)
|
||||||
|
|
||||||
FORMAT_OVERRIDE = api_settings.URL_FORMAT_OVERRIDE
|
return preserve_builtin_query_params(url, request)
|
||||||
if FORMAT_OVERRIDE and (FORMAT_OVERRIDE in request.query_params):
|
|
||||||
return replace_query_param(url, FORMAT_OVERRIDE, request.query_params[FORMAT_OVERRIDE])
|
|
||||||
return url
|
|
||||||
|
|
||||||
def _reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
|
def _reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -2,12 +2,12 @@ from __future__ import unicode_literals
|
||||||
from django.core.urlresolvers import resolve, get_script_prefix
|
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
|
Given a url returns a list of breadcrumbs, which are each a
|
||||||
tuple of (name, url).
|
tuple of (name, url).
|
||||||
"""
|
"""
|
||||||
|
from rest_framework.reverse import preserve_builtin_query_params
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
|
@ -33,7 +33,8 @@ def get_breadcrumbs(url):
|
||||||
if not seen or seen[-1] != view:
|
if not seen or seen[-1] != view:
|
||||||
suffix = getattr(view, 'suffix', None)
|
suffix = getattr(view, 'suffix', None)
|
||||||
name = view_name_func(cls, suffix)
|
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)
|
seen.append(view)
|
||||||
|
|
||||||
if url == '':
|
if url == '':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user