diff -r bdf2b79d7dec django/core/urlresolvers.py
a
|
b
|
|
8 | 8 | """ |
9 | 9 | |
10 | 10 | import re |
| 11 | from itertools import chain |
11 | 12 | from threading import local |
12 | 13 | |
13 | 14 | from django.http import Http404 |
… |
… |
|
34 | 35 | |
35 | 36 | |
36 | 37 | class 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): |
38 | 39 | self.func = func |
39 | 40 | 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())]) |
41 | 44 | self.app_name = app_name |
42 | 45 | if namespaces: |
43 | 46 | self.namespaces = [x for x in namespaces if x] |
… |
… |
|
193 | 196 | args = () |
194 | 197 | else: |
195 | 198 | args = match.groups() |
196 | | # In both cases, pass any extra_kwargs as **kwargs. |
197 | | kwargs.update(self.default_args) |
198 | 199 | |
199 | | return ResolverMatch(self.callback, args, kwargs, self.name) |
| 200 | return ResolverMatch(self.callback, args, kwargs, self.name, default_kwargs=self.default_args) |
200 | 201 | |
201 | 202 | @property |
202 | 203 | def callback(self): |
… |
… |
|
297 | 298 | else: |
298 | 299 | if sub_match: |
299 | 300 | 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) |
304 | 309 | tried.append([pattern]) |
305 | 310 | raise Resolver404({'tried': tried, 'path': new_path}) |
306 | 311 | raise Resolver404({'path' : path}) |
diff -r bdf2b79d7dec tests/regressiontests/i18n/patterns/tests.py
a
|
b
|
|
3 | 3 | import os |
4 | 4 | |
5 | 5 | from django.core.exceptions import ImproperlyConfigured |
6 | | from django.core.urlresolvers import reverse, clear_url_caches |
| 6 | from django.core.urlresolvers import reverse, resolve, clear_url_caches |
7 | 7 | from django.test import TestCase |
8 | 8 | from django.test.utils import override_settings |
9 | 9 | from django.template import Template, Context |
… |
… |
|
246 | 246 | |
247 | 247 | class URLTagTests(URLTestCaseBase): |
248 | 248 | """ |
249 | | Test if the language tag works. |
| 249 | Tests if the language tag works. |
250 | 250 | """ |
251 | 251 | def test_strings_only(self): |
252 | 252 | t = Template("""{% load i18n %} |
… |
… |
|
283 | 283 | {% language 'pt-br' %}{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}""") |
284 | 284 | self.assertEqual(tpl.render(Context({})).strip().split(), |
285 | 285 | [u'/vertaald/apo/', u'/traduzidos/apo/']) |
| 286 | |
| 287 | |
| 288 | class 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), |
diff -r bdf2b79d7dec tests/regressiontests/i18n/patterns/urls/default.py
a
|
b
|
|
9 | 9 | urlpatterns = patterns('', |
10 | 10 | url(r'^not-prefixed/$', view, name='not-prefixed'), |
11 | 11 | 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'), |
13 | 13 | ) |
14 | 14 | |
15 | 15 | urlpatterns += i18n_patterns('', |
diff -r bdf2b79d7dec tests/regressiontests/urlpatterns_reverse/tests.py
a
|
b
|
|
481 | 481 | self.assertRaises(ViewDoesNotExist, self.client.get, '/missing_outer/') |
482 | 482 | self.assertRaises(ViewDoesNotExist, self.client.get, '/uncallable/') |
483 | 483 | |
| 484 | |
| 485 | class 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/') |
diff -r bdf2b79d7dec tests/regressiontests/urlpatterns_reverse/urls.py
a
|
b
|
|
36 | 36 | url(r'^windows_path/(?P<drive_name>[A-Z]):\\(?P<path>.+)/$', empty_view, |
37 | 37 | name="windows"), |
38 | 38 | url(r'^special_chars/(.+)/$', empty_view, name="special"), |
| 39 | url(r'^rereverse/(?P<arg>\d+)/$', empty_view, {'extra': True}, name='rereverse'), |
39 | 40 | url(r'^(?P<name>.+)/\d+/$', empty_view, name="mixed"), |
40 | 41 | url(r'^repeats/a{1,2}/$', empty_view, name="repeats"), |
41 | 42 | url(r'^repeats/a{2,4}/$', empty_view, name="repeats2"), |