Ticket #17551: 17551.diff

File 17551.diff, 6.4 KB (added by gnosek, 3 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): 
    245245                p_pattern = p_pattern[1:]
    246246            if isinstance(pattern, RegexURLResolver):
    247247                if pattern.namespace:
    248                     namespaces[pattern.namespace] = (p_pattern, pattern)
     248                    namespaces.setdefault(pattern.namespace, []).append((p_pattern, pattern))
    249249                    if pattern.app_name:
    250250                        apps.setdefault(pattern.app_name, []).append(pattern.namespace)
    251251                else:
    class RegexURLResolver(LocaleRegexProvider): 
    256256                            for piece, p_args in parent:
    257257                                new_matches.extend([(piece + suffix, p_args + args) for (suffix, args) in matches])
    258258                            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))
    261262                    for app_name, namespace_list in pattern.app_dict.items():
    262263                        apps.setdefault(app_name, []).extend(namespace_list)
    263264            else:
    def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current 
    459460                pass
    460461
    461462            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
    463471                resolved_path.append(ns)
    464                 ns_pattern = ns_pattern + extra
    465472            except KeyError, key:
    466473                if resolved_path:
    467474                    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 
    66
    77
    88class 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
    1017        self.app_name = app_name
    1118        self.namespace = namespace
    1219
    1320    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
    1922    urls = property(urls)
    2023
    2124testobj1 = URLObject('testapp', 'test-ns1')
    default_testobj = URLObject('testapp', 'testapp') 
    2528otherobj1 = URLObject('nodefault', 'other-ns1')
    2629otherobj2 = URLObject('nodefault', 'other-ns2')
    2730
     31double_nested1 = URLObject('randomapp', 'nested',
     32    patterns('', url(r'^f1/$', 'empty_view', name='view_f1'))
     33)
     34
     35double_nested2 = URLObject('randomapp', 'nested',
     36    patterns('', url(r'^f2/$', 'empty_view', name='view_f2'))
     37)
     38
     39first_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)
     44first = URLObject('first', 'inc-some', first_patterns)
     45second_patterns = patterns('',
     46    url(r'^$', 'empty_view', name='second'),
     47)
     48second = URLObject('second', 'inc-some', second_patterns)
     49
    2850urlpatterns = patterns('regressiontests.urlpatterns_reverse.views',
    2951    url(r'^normal/$', 'empty_view', name='normal-view'),
    3052    url(r'^normal/(?P<arg1>\d+)/(?P<arg2>\d+)/$', 'empty_view', name='normal-view'),
    urlpatterns = patterns('regressiontests.urlpatterns_reverse.views', 
    5577    (r'^ns-outer/(?P<outer>\d+)/', include('regressiontests.urlpatterns_reverse.included_namespace_urls', namespace='inc-outer')),
    5678
    5779    (r'^\+\\\$\*/', include('regressiontests.urlpatterns_reverse.namespace_urls', namespace='special')),
     80
     81    url(r'^first/', include(first.urls)),
     82    url(r'^second/', include(second.urls)),
    5883)
  • 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): 
    392392        self.assertEqual('/inc70/', reverse('inc-ns5:inner-nothing', args=['70']))
    393393        self.assertEqual('/inc78/extra/foobar/', reverse('inc-ns5:inner-extra', args=['78','foobar']))
    394394
     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
    395402class RequestURLconfTests(TestCase):
    396403    def setUp(self):
    397404        self.root_urlconf = settings.ROOT_URLCONF
Back to Top