Ticket #20757: patch_commit_f8ce5686938b.patch

File patch_commit_f8ce5686938b.patch, 4.8 KB (added by Flavio Curella, 11 years ago)

use namedtuples

  • django/core/urlresolvers.py

    diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
    index 14fc2822815cc8d6675f6b43bbdf091fd60b7393..2bbe0efe49a2ad9f030517eb3024d1c4e85379eb 100644
    a b a string) and returns a tuple in this format:  
    88"""
    99from __future__ import unicode_literals
    1010
     11from collections import namedtuple
    1112from importlib import import_module
    1213import re
    1314from threading import local
    _prefixes = local()  
    3637# Overridden URLconfs for each thread are stored here.
    3738_urlconfs = local()
    3839
     40URLEntry = namedtuple('URLEntry', ['urlbits', 'p_pattern', 'default_args'])
     41URLBit = namedtuple('URLBit', ['format', 'argument_names'])
     42
    3943
    4044class ResolverMatch(object):
    4145    def __init__(self, func, args, kwargs, url_name=None, app_name=None, namespaces=None):
    class RegexURLResolver(LocaleRegexProvider):  
    269273                    if pattern.app_name:
    270274                        apps.setdefault(pattern.app_name, []).append(pattern.namespace)
    271275                else:
    272                     parent = normalize(pattern.regex.pattern)
     276                    parent = [URLBit(*p) for p in normalize(pattern.regex.pattern)]
    273277                    for name in pattern.reverse_dict:
    274278                        for matches, pat, defaults in pattern.reverse_dict.getlist(name):
    275279                            new_matches = []
    276280                            for piece, p_args in parent:
    277                                 new_matches.extend((piece + suffix, p_args + args) for (suffix, args) in matches)
    278                             lookups.appendlist(name, (new_matches, p_pattern + pat, dict(defaults, **pattern.default_kwargs)))
     281                                new_matches.extend([URLBit(piece + suffix, p_args + args) for (suffix, args) in matches])
     282                            lookups.appendlist(name, URLEntry(new_matches, p_pattern + pat, dict(defaults, **pattern.default_kwargs)))
    279283                    for namespace, (prefix, sub_pattern) in pattern.namespace_dict.items():
    280284                        namespaces[namespace] = (p_pattern + prefix, sub_pattern)
    281285                    for app_name, namespace_list in pattern.app_dict.items():
    282286                        apps.setdefault(app_name, []).extend(namespace_list)
    283287            else:
    284                 bits = normalize(p_pattern)
    285                 lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
     288                bits = [URLBit(*p) for p in normalize(p_pattern)]
     289                lookups.appendlist(pattern.callback, URLEntry(bits, p_pattern, pattern.default_args))
    286290                if pattern.name is not None:
    287                     lookups.appendlist(pattern.name, (bits, p_pattern, pattern.default_args))
     291                    lookups.appendlist(pattern.name, URLEntry(bits, p_pattern, pattern.default_args))
    288292        self._reverse_dict[language_code] = lookups
    289293        self._namespace_dict[language_code] = namespaces
    290294        self._app_dict[language_code] = apps
    def is_valid_path(path, urlconf=None):  
    575579        return True
    576580    except Resolver404:
    577581        return False
     582
     583def get_urlformat(urlname):
     584    """
     585    Given a URL name, returns the URL as a string suitable for string.format.
     586
     587    Example::
     588
     589    urlpatterns = patterns('',
     590        url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url2"),
     591    )
     592
     593    >>> get_urlformat('named-url2')
     594    '/extra/%(extra)s/'
     595    """
     596    urlconf = get_urlconf()
     597    resolver = get_resolver(urlconf)
     598    urlbit = resolver.reverse_dict[urlname].urlbits[0]
     599    return "%s%s" % (get_script_prefix(), urlbit.format)
  • tests/urlpatterns_reverse/tests.py

    diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py
    index 4d77dc946d2f9f0c7fda043fc707235c786eb9f9..9c08356afa485e4c4e15008913ec0a9ee1c37648 100644
    a b from django.conf import settings  
    99from django.contrib.auth.models import User
    1010from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
    1111from django.core.urlresolvers import (reverse, reverse_lazy, resolve, get_callable,
    12     get_resolver, NoReverseMatch, Resolver404, ResolverMatch, RegexURLResolver,
    13     RegexURLPattern)
     12    get_resolver, get_urlformat, NoReverseMatch, Resolver404, ResolverMatch,
     13    RegexURLResolver, RegexURLPattern)
    1414from django.http import HttpRequest, HttpResponseRedirect, HttpResponsePermanentRedirect
    1515from django.shortcuts import redirect
    1616from django.test import TestCase
    class ViewLoadingTests(TestCase):  
    657657        self.assertRaises(AttributeError, get_callable,
    658658            'urlpatterns_reverse.views_broken.i_am_broken')
    659659
     660
     661class URLObjects(TestCase):
     662    urls = 'urlpatterns_reverse.named_urls'
     663
     664    def test_URL_objects(self):
     665        format = get_urlformat('named-url2')
     666        self.assertEqual(format, '/extra/%(extra)s/')
Back to Top