Ticket #16406: t16406.diff

File t16406.diff, 6.4 KB (added by Florian Apolloner, 13 years ago)

simple patch demonstrating the feature

  • django/core/urlresolvers.py

    diff -r bdf2b79d7dec django/core/urlresolvers.py
    a b  
    88"""
    99
    1010import re
     11from itertools import chain
    1112from threading import local
    1213
    1314from django.http import Http404
     
    3435
    3536
    3637class ResolverMatch(object):
    37     def __init__(self, func, args, kwargs, url_name=None, app_name=None, namespaces=None):
     38    def __init__(self, func, args, kwargs, url_name=None, app_name=None, namespaces=None, default_kwargs=None):
    3839        self.func = func
    3940        self.args = args
    40         self.kwargs = kwargs
     41        self.match_kwargs = kwargs
     42        self.default_kwargs = default_kwargs
     43        self.kwargs = dict([x for x in chain(kwargs.items(), default_kwargs.items())])
    4144        self.app_name = app_name
    4245        if namespaces:
    4346            self.namespaces = [x for x in namespaces if x]
     
    193196                args = ()
    194197            else:
    195198                args = match.groups()
    196             # In both cases, pass any extra_kwargs as **kwargs.
    197             kwargs.update(self.default_args)
    198199
    199             return ResolverMatch(self.callback, args, kwargs, self.name)
     200            return ResolverMatch(self.callback, args, kwargs, self.name, default_kwargs=self.default_args)
    200201
    201202    @property
    202203    def callback(self):
     
    297298                else:
    298299                    if sub_match:
    299300                        sub_match_dict = dict([(smart_str(k), v) for k, v in match.groupdict().items()])
    300                         sub_match_dict.update(self.default_kwargs)
    301                         for k, v in sub_match.kwargs.iteritems():
    302                             sub_match_dict[smart_str(k)] = v
    303                         return ResolverMatch(sub_match.func, sub_match.args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, [self.namespace] + sub_match.namespaces)
     301                        sub_match_dict.update(sub_match.match_kwargs)
     302                        default_kwargs = sub_match.default_kwargs
     303                        default_kwargs.update(self.default_kwargs)
     304                        return ResolverMatch(sub_match.func, sub_match.args,
     305                            sub_match_dict, sub_match.url_name,
     306                            self.app_name or sub_match.app_name,
     307                            [self.namespace] + sub_match.namespaces,
     308                            default_kwargs)
    304309                    tried.append([pattern])
    305310            raise Resolver404({'tried': tried, 'path': new_path})
    306311        raise Resolver404({'path' : path})
  • tests/regressiontests/i18n/patterns/tests.py

    diff -r bdf2b79d7dec tests/regressiontests/i18n/patterns/tests.py
    a b  
    33import os
    44
    55from django.core.exceptions import ImproperlyConfigured
    6 from django.core.urlresolvers import reverse, clear_url_caches
     6from django.core.urlresolvers import reverse, resolve, clear_url_caches
    77from django.test import TestCase
    88from django.test.utils import override_settings
    99from django.template import Template, Context
     
    246246
    247247class URLTagTests(URLTestCaseBase):
    248248    """
    249     Test if the language tag works.
     249    Tests if the language tag works.
    250250    """
    251251    def test_strings_only(self):
    252252        t = Template("""{% load i18n %}
     
    283283            {% language 'pt-br' %}{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}""")
    284284        self.assertEqual(tpl.render(Context({})).strip().split(),
    285285                         [u'/vertaald/apo/', u'/traduzidos/apo/'])
     286
     287
     288class URLExampleTests(URLTestCaseBase):
     289    def test_demo(self):
     290        # User accesses an english url.
     291        with translation.override('en'):
     292            match = resolve('/translated/apo/')
     293        # Now we want provide links to the same page in other languages
     294        links = []
     295        langs = ['nl', 'pt-br']
     296        for lang in langs:
     297            with translation.override(lang):
     298                links.append(reverse(match.url_name, args=match.args, kwargs=match.match_kwargs))
     299        self.assertEqual(links, [u'/vertaald/apo/', u'/traduzidos/apo/'])
     300        # Now we can offer the user links to the other pages
     301        # print "View the page in: ",
     302        # for lang, link in zip(langs, links):
     303        #   print "<a href='%s'>%s</a> % (link, lang),
  • tests/regressiontests/i18n/patterns/urls/default.py

    diff -r bdf2b79d7dec tests/regressiontests/i18n/patterns/urls/default.py
    a b  
    99urlpatterns = patterns('',
    1010    url(r'^not-prefixed/$', view, name='not-prefixed'),
    1111    url(_(r'^translated/$'), view, name='no-prefix-translated'),
    12     url(_(r'^translated/(?P<slug>[\w-]+)/$'), view, name='no-prefix-translated-slug'),
     12    url(_(r'^translated/(?P<slug>[\w-]+)/$'), view, {'extra': True}, name='no-prefix-translated-slug'),
    1313)
    1414
    1515urlpatterns += i18n_patterns('',
  • tests/regressiontests/urlpatterns_reverse/tests.py

    diff -r bdf2b79d7dec tests/regressiontests/urlpatterns_reverse/tests.py
    a b  
    481481        self.assertRaises(ViewDoesNotExist, self.client.get, '/missing_outer/')
    482482        self.assertRaises(ViewDoesNotExist, self.client.get, '/uncallable/')
    483483
     484
     485class TestRereverse(TestCase):
     486    urls = 'regressiontests.urlpatterns_reverse.urls'
     487
     488    def test_rereverse(self):
     489        match = resolve('/rereverse/12/')
     490        self.assertEqual(reverse(match.url_name, args=match.args, kwargs=match.match_kwargs), '/rereverse/12/')
  • tests/regressiontests/urlpatterns_reverse/urls.py

    diff -r bdf2b79d7dec tests/regressiontests/urlpatterns_reverse/urls.py
    a b  
    3636    url(r'^windows_path/(?P<drive_name>[A-Z]):\\(?P<path>.+)/$', empty_view,
    3737            name="windows"),
    3838    url(r'^special_chars/(.+)/$', empty_view, name="special"),
     39    url(r'^rereverse/(?P<arg>\d+)/$', empty_view, {'extra': True}, name='rereverse'),
    3940    url(r'^(?P<name>.+)/\d+/$', empty_view, name="mixed"),
    4041    url(r'^repeats/a{1,2}/$', empty_view, name="repeats"),
    4142    url(r'^repeats/a{2,4}/$', empty_view, name="repeats2"),
Back to Top