Ticket #17551: 17551.diff
File 17551.diff, 6.4 KB (added by , 13 years ago) |
---|
-
django/core/urlresolvers.py
commit ad80a813decc7809c7f8d7c0693ac3216f6ac655 Author: Grzegorz Nosek <root@localdomain.pl> Date: Sat Feb 4 18:13:52 2012 +0100 #17551 diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index b634b56..4a8ec2d 100644
a b class RegexURLResolver(LocaleRegexProvider): 245 245 p_pattern = p_pattern[1:] 246 246 if isinstance(pattern, RegexURLResolver): 247 247 if pattern.namespace: 248 namespaces [pattern.namespace] = (p_pattern, pattern)248 namespaces.setdefault(pattern.namespace, []).append((p_pattern, pattern)) 249 249 if pattern.app_name: 250 250 apps.setdefault(pattern.app_name, []).append(pattern.namespace) 251 251 else: … … class RegexURLResolver(LocaleRegexProvider): 256 256 for piece, p_args in parent: 257 257 new_matches.extend([(piece + suffix, p_args + args) for (suffix, args) in matches]) 258 258 lookups.appendlist(name, (new_matches, p_pattern + pat, dict(defaults, **pattern.default_kwargs))) 259 for namespace, (prefix, sub_pattern) in pattern.namespace_dict.items(): 260 namespaces[namespace] = (p_pattern + prefix, sub_pattern) 259 for namespace, sub_patterns in pattern.namespace_dict.items(): 260 for prefix, sub_pattern in sub_patterns: 261 namespaces.setdefault(namespace, []).append((p_pattern + prefix, sub_pattern)) 261 262 for app_name, namespace_list in pattern.app_dict.items(): 262 263 apps.setdefault(app_name, []).extend(namespace_list) 263 264 else: … … def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current 459 460 pass 460 461 461 462 try: 462 extra, resolver = resolver.namespace_dict[ns] 463 resolvers = resolver.namespace_dict[ns] 464 if len(resolvers) > 1: 465 ns_resolvers = [RegexURLResolver(extra, ns_resolver.url_patterns) 466 for extra, ns_resolver in resolvers] 467 resolver = RegexURLResolver('', ns_resolvers) 468 else: 469 extra, resolver = resolver.namespace_dict[ns][0] 470 ns_pattern = ns_pattern + extra 463 471 resolved_path.append(ns) 464 ns_pattern = ns_pattern + extra465 472 except KeyError, key: 466 473 if resolved_path: 467 474 raise NoReverseMatch( -
tests/regressiontests/urlpatterns_reverse/namespace_urls.py
diff --git a/tests/regressiontests/urlpatterns_reverse/namespace_urls.py b/tests/regressiontests/urlpatterns_reverse/namespace_urls.py index fa892a4..2d95a8f 100644
a b from .views import view_class_instance 6 6 7 7 8 8 class URLObject(object): 9 def __init__(self, app_name, namespace): 9 def __init__(self, app_name, namespace, _patterns=None): 10 if _patterns is None: 11 _patterns = patterns('', 12 url(r'^inner/$', 'empty_view', name='urlobject-view'), 13 url(r'^inner/(?P<arg1>\d+)/(?P<arg2>\d+)/$', 'empty_view', name='urlobject-view'), 14 url(r'^inner/\+\\\$\*/$', 'empty_view', name='urlobject-special-view'), 15 ) 16 self.patterns = _patterns 10 17 self.app_name = app_name 11 18 self.namespace = namespace 12 19 13 20 def urls(self): 14 return patterns('', 15 url(r'^inner/$', 'empty_view', name='urlobject-view'), 16 url(r'^inner/(?P<arg1>\d+)/(?P<arg2>\d+)/$', 'empty_view', name='urlobject-view'), 17 url(r'^inner/\+\\\$\*/$', 'empty_view', name='urlobject-special-view'), 18 ), self.app_name, self.namespace 21 return self.patterns, self.app_name, self.namespace 19 22 urls = property(urls) 20 23 21 24 testobj1 = URLObject('testapp', 'test-ns1') … … default_testobj = URLObject('testapp', 'testapp') 25 28 otherobj1 = URLObject('nodefault', 'other-ns1') 26 29 otherobj2 = URLObject('nodefault', 'other-ns2') 27 30 31 double_nested1 = URLObject('randomapp', 'nested', 32 patterns('', url(r'^f1/$', 'empty_view', name='view_f1')) 33 ) 34 35 double_nested2 = URLObject('randomapp', 'nested', 36 patterns('', url(r'^f2/$', 'empty_view', name='view_f2')) 37 ) 38 39 first_patterns = patterns('', 40 url(r'^$', 'empty_view', name='first'), 41 url(r'^nested1/', include(double_nested1.urls)), 42 url(r'^nested2/', include(double_nested2.urls)), 43 ) 44 first = URLObject('first', 'inc-some', first_patterns) 45 second_patterns = patterns('', 46 url(r'^$', 'empty_view', name='second'), 47 ) 48 second = URLObject('second', 'inc-some', second_patterns) 49 28 50 urlpatterns = patterns('regressiontests.urlpatterns_reverse.views', 29 51 url(r'^normal/$', 'empty_view', name='normal-view'), 30 52 url(r'^normal/(?P<arg1>\d+)/(?P<arg2>\d+)/$', 'empty_view', name='normal-view'), … … urlpatterns = patterns('regressiontests.urlpatterns_reverse.views', 55 77 (r'^ns-outer/(?P<outer>\d+)/', include('regressiontests.urlpatterns_reverse.included_namespace_urls', namespace='inc-outer')), 56 78 57 79 (r'^\+\\\$\*/', include('regressiontests.urlpatterns_reverse.namespace_urls', namespace='special')), 80 81 url(r'^first/', include(first.urls)), 82 url(r'^second/', include(second.urls)), 58 83 ) -
tests/regressiontests/urlpatterns_reverse/tests.py
diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py index a1c9244..53759cd 100644
a b class NamespaceTests(TestCase): 392 392 self.assertEqual('/inc70/', reverse('inc-ns5:inner-nothing', args=['70'])) 393 393 self.assertEqual('/inc78/extra/foobar/', reverse('inc-ns5:inner-extra', args=['78','foobar'])) 394 394 395 def test_namespaces_in_different_includes(self): 396 "Namespaces declared in different places: see #17551" 397 self.assertEqual('/first/', reverse('inc-some:first')) 398 self.assertEqual('/second/', reverse('inc-some:second')) 399 self.assertEqual('/first/nested1/f1/', reverse('inc-some:randomapp:view_f1')) 400 self.assertEqual('/first/nested2/f2/', reverse('inc-some:randomapp:view_f2')) 401 395 402 class RequestURLconfTests(TestCase): 396 403 def setUp(self): 397 404 self.root_urlconf = settings.ROOT_URLCONF