[1.11.x] Fixed #27874 -- Fixed URL namespace warning (urls.W005) for nested namespaces.

Backport of 8d4885ede5 from master
This commit is contained in:
Chris Lamb 2017-02-24 23:13:48 +08:00 committed by Tim Graham
parent a86ec78fe6
commit 0417bf47a6
2 changed files with 12 additions and 4 deletions

View File

@ -53,17 +53,18 @@ def check_url_namespaces_unique(app_configs, **kwargs):
return errors
def _load_all_namespaces(resolver):
def _load_all_namespaces(resolver, parents=()):
"""
Recursively load all namespaces from URL patterns.
"""
url_patterns = getattr(resolver, 'url_patterns', [])
namespaces = [
url.namespace for url in url_patterns
':'.join(parents + (url.namespace,)) for url in url_patterns
if getattr(url, 'namespace', None) is not None
]
for pattern in url_patterns:
namespaces.extend(_load_all_namespaces(pattern))
current = parents + (getattr(pattern, 'namespace', ()),)
namespaces.extend(_load_all_namespaces(pattern, current))
return namespaces

View File

@ -5,7 +5,14 @@ common_url_patterns = ([
url(r'^app-url/', include([])),
], 'common')
nested_url_patterns = ([
url(r'^common/', include(common_url_patterns, namespace='nested')),
], 'nested')
urlpatterns = [
url(r'^app-ns1-0/', include(common_url_patterns, namespace='app-include-1')),
url(r'^app-ns1-1/', include(common_url_patterns, namespace='app-include-2'))
url(r'^app-ns1-1/', include(common_url_patterns, namespace='app-include-2')),
# 'nested' is included twice but namespaced by nested-1 and nested-2.
url(r'^app-ns1-2/', include(nested_url_patterns, namespace='nested-1')),
url(r'^app-ns1-3/', include(nested_url_patterns, namespace='nested-2')),
]