Fixed #26039 -- Unwrapped nested partials in URL reversal.

Prior to Python 3.5 nested partials need to be fully "unfolded"
to get the actual function.
This commit is contained in:
Grégory Starck 2016-01-05 16:47:48 -05:00 committed by Tim Graham
parent 632a9f21bc
commit 9f9921e89c
3 changed files with 7 additions and 5 deletions

View File

@ -135,7 +135,9 @@ class RegexURLPattern(LocaleRegexProvider):
'path.to.ClassBasedView').
"""
callback = self.callback
if isinstance(callback, functools.partial):
# Python 3.5 collapses nested partials, so can change "while" to "if"
# when it's the minimum supported version.
while isinstance(callback, functools.partial):
callback = callback.func
if not hasattr(callback, '__name__'):
return callback.__module__ + "." + callback.__class__.__name__

View File

@ -1,8 +1,8 @@
from django.conf.urls import include, url
from .views import (
absolute_kwargs_view, defaults_view, empty_view, empty_view_partial,
empty_view_wrapped, nested_view,
absolute_kwargs_view, defaults_view, empty_view, empty_view_nested_partial,
empty_view_partial, empty_view_wrapped, nested_view,
)
other_patterns = [
@ -62,6 +62,7 @@ urlpatterns = [
# Partials should be fine.
url(r'^partial/', empty_view_partial, name="partial"),
url(r'^partial_nested/', empty_view_nested_partial, name="partial_nested"),
url(r'^partial_wrapped/', empty_view_wrapped, name="partial_wrapped"),
# This is non-reversible, but we shouldn't blow up when parsing it.

View File

@ -55,8 +55,7 @@ def bad_view(request, *args, **kwargs):
empty_view_partial = partial(empty_view, template_name="template.html")
empty_view_nested_partial = partial(empty_view_partial, template_name="nested_partial.html")
empty_view_wrapped = update_wrapper(
partial(empty_view, template_name="template.html"), empty_view,
)