Code

Ticket #16406: t16406.diff

File t16406.diff, 6.4 KB (added by apollo13, 3 years ago)

simple patch demonstrating the feature

Line 
1diff -r bdf2b79d7dec django/core/urlresolvers.py
2--- a/django/core/urlresolvers.py       Mon Jul 04 16:20:29 2011 +0000
3+++ b/django/core/urlresolvers.py       Mon Jul 04 19:40:10 2011 +0200
4@@ -8,6 +8,7 @@
5 """
6 
7 import re
8+from itertools import chain
9 from threading import local
10 
11 from django.http import Http404
12@@ -34,10 +35,12 @@
13 
14 
15 class ResolverMatch(object):
16-    def __init__(self, func, args, kwargs, url_name=None, app_name=None, namespaces=None):
17+    def __init__(self, func, args, kwargs, url_name=None, app_name=None, namespaces=None, default_kwargs=None):
18         self.func = func
19         self.args = args
20-        self.kwargs = kwargs
21+        self.match_kwargs = kwargs
22+        self.default_kwargs = default_kwargs
23+        self.kwargs = dict([x for x in chain(kwargs.items(), default_kwargs.items())])
24         self.app_name = app_name
25         if namespaces:
26             self.namespaces = [x for x in namespaces if x]
27@@ -193,10 +196,8 @@
28                 args = ()
29             else:
30                 args = match.groups()
31-            # In both cases, pass any extra_kwargs as **kwargs.
32-            kwargs.update(self.default_args)
33 
34-            return ResolverMatch(self.callback, args, kwargs, self.name)
35+            return ResolverMatch(self.callback, args, kwargs, self.name, default_kwargs=self.default_args)
36 
37     @property
38     def callback(self):
39@@ -297,10 +298,14 @@
40                 else:
41                     if sub_match:
42                         sub_match_dict = dict([(smart_str(k), v) for k, v in match.groupdict().items()])
43-                        sub_match_dict.update(self.default_kwargs)
44-                        for k, v in sub_match.kwargs.iteritems():
45-                            sub_match_dict[smart_str(k)] = v
46-                        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)
47+                        sub_match_dict.update(sub_match.match_kwargs)
48+                        default_kwargs = sub_match.default_kwargs
49+                        default_kwargs.update(self.default_kwargs)
50+                        return ResolverMatch(sub_match.func, sub_match.args,
51+                            sub_match_dict, sub_match.url_name,
52+                            self.app_name or sub_match.app_name,
53+                            [self.namespace] + sub_match.namespaces,
54+                            default_kwargs)
55                     tried.append([pattern])
56             raise Resolver404({'tried': tried, 'path': new_path})
57         raise Resolver404({'path' : path})
58diff -r bdf2b79d7dec tests/regressiontests/i18n/patterns/tests.py
59--- a/tests/regressiontests/i18n/patterns/tests.py      Mon Jul 04 16:20:29 2011 +0000
60+++ b/tests/regressiontests/i18n/patterns/tests.py      Mon Jul 04 19:40:10 2011 +0200
61@@ -3,7 +3,7 @@
62 import os
63 
64 from django.core.exceptions import ImproperlyConfigured
65-from django.core.urlresolvers import reverse, clear_url_caches
66+from django.core.urlresolvers import reverse, resolve, clear_url_caches
67 from django.test import TestCase
68 from django.test.utils import override_settings
69 from django.template import Template, Context
70@@ -246,7 +246,7 @@
71 
72 class URLTagTests(URLTestCaseBase):
73     """
74-    Test if the language tag works.
75+    Tests if the language tag works.
76     """
77     def test_strings_only(self):
78         t = Template("""{% load i18n %}
79@@ -283,3 +283,21 @@
80             {% language 'pt-br' %}{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}""")
81         self.assertEqual(tpl.render(Context({})).strip().split(),
82                          [u'/vertaald/apo/', u'/traduzidos/apo/'])
83+
84+
85+class URLExampleTests(URLTestCaseBase):
86+    def test_demo(self):
87+        # User accesses an english url.
88+        with translation.override('en'):
89+            match = resolve('/translated/apo/')
90+        # Now we want provide links to the same page in other languages
91+        links = []
92+        langs = ['nl', 'pt-br']
93+        for lang in langs:
94+            with translation.override(lang):
95+                links.append(reverse(match.url_name, args=match.args, kwargs=match.match_kwargs))
96+        self.assertEqual(links, [u'/vertaald/apo/', u'/traduzidos/apo/'])
97+        # Now we can offer the user links to the other pages
98+        # print "View the page in: ",
99+        # for lang, link in zip(langs, links):
100+        #   print "<a href='%s'>%s</a> % (link, lang),
101diff -r bdf2b79d7dec tests/regressiontests/i18n/patterns/urls/default.py
102--- a/tests/regressiontests/i18n/patterns/urls/default.py       Mon Jul 04 16:20:29 2011 +0000
103+++ b/tests/regressiontests/i18n/patterns/urls/default.py       Mon Jul 04 19:40:10 2011 +0200
104@@ -9,7 +9,7 @@
105 urlpatterns = patterns('',
106     url(r'^not-prefixed/$', view, name='not-prefixed'),
107     url(_(r'^translated/$'), view, name='no-prefix-translated'),
108-    url(_(r'^translated/(?P<slug>[\w-]+)/$'), view, name='no-prefix-translated-slug'),
109+    url(_(r'^translated/(?P<slug>[\w-]+)/$'), view, {'extra': True}, name='no-prefix-translated-slug'),
110 )
111 
112 urlpatterns += i18n_patterns('',
113diff -r bdf2b79d7dec tests/regressiontests/urlpatterns_reverse/tests.py
114--- a/tests/regressiontests/urlpatterns_reverse/tests.py        Mon Jul 04 16:20:29 2011 +0000
115+++ b/tests/regressiontests/urlpatterns_reverse/tests.py        Mon Jul 04 19:40:10 2011 +0200
116@@ -481,3 +481,10 @@
117         self.assertRaises(ViewDoesNotExist, self.client.get, '/missing_outer/')
118         self.assertRaises(ViewDoesNotExist, self.client.get, '/uncallable/')
119 
120+
121+class TestRereverse(TestCase):
122+    urls = 'regressiontests.urlpatterns_reverse.urls'
123+
124+    def test_rereverse(self):
125+        match = resolve('/rereverse/12/')
126+        self.assertEqual(reverse(match.url_name, args=match.args, kwargs=match.match_kwargs), '/rereverse/12/')
127diff -r bdf2b79d7dec tests/regressiontests/urlpatterns_reverse/urls.py
128--- a/tests/regressiontests/urlpatterns_reverse/urls.py Mon Jul 04 16:20:29 2011 +0000
129+++ b/tests/regressiontests/urlpatterns_reverse/urls.py Mon Jul 04 19:40:10 2011 +0200
130@@ -36,6 +36,7 @@
131     url(r'^windows_path/(?P<drive_name>[A-Z]):\\(?P<path>.+)/$', empty_view,
132             name="windows"),
133     url(r'^special_chars/(.+)/$', empty_view, name="special"),
134+    url(r'^rereverse/(?P<arg>\d+)/$', empty_view, {'extra': True}, name='rereverse'),
135     url(r'^(?P<name>.+)/\d+/$', empty_view, name="mixed"),
136     url(r'^repeats/a{1,2}/$', empty_view, name="repeats"),
137     url(r'^repeats/a{2,4}/$', empty_view, name="repeats2"),