Skip to content

Commit ca5fc71

Browse files
authored
Merge pull request #38 from 1vank1n/master
add support i18n_patterns
2 parents 15f6e73 + 18b55e7 commit ca5fc71

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

multiurl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def resolve(self, path):
5555

5656

5757
class MultiResolverMatch(object):
58-
def __init__(self, matches, exceptions, patterns_matched, path, route=None):
58+
def __init__(self, matches, exceptions, patterns_matched, path, route=''):
5959
self.matches = matches
6060
self.exceptions = exceptions
6161
self.patterns_matched = patterns_matched

tests.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
from django.conf import settings
66
from django.conf.urls import url
7-
7+
from django.conf.urls.i18n import i18n_patterns
88
from django.http import HttpResponse
9+
from django.urls import path
910

10-
from multiurl import multiurl, ContinueResolving
11+
from multiurl import ContinueResolving, multiurl
1112

1213
try:
1314
from django import urls as urlresolvers
@@ -33,6 +34,14 @@ def setUp(self):
3334
)
3435
])
3536

37+
self.patterns_pathall = urlresolvers.URLResolver(RegexPattern(r'^/'), i18n_patterns(
38+
multiurl(
39+
path('<str:name>/', brand, name='brand'),
40+
path('<str:name>/', model, name='model'),
41+
),
42+
))
43+
44+
3645
# Patterns with no "catch all" - last view could still raise ContinueResolving.
3746
self.patterns_no_fallthrough = urlresolvers.URLResolver(RegexPattern(r'^/'), [
3847
multiurl(
@@ -56,6 +65,16 @@ def test_resolve_match_last(self):
5665
response = m.func(request=None, *m.args, **m.kwargs)
5766
self.assertEqual(response.content, b"Thing: Bacon")
5867

68+
def test_resolve_match_path_brand(self):
69+
m = self.patterns_pathall.resolve('/en-us/bmw/')
70+
response = m.func(request=None, *m.args, **m.kwargs)
71+
self.assertEqual(response.content, b"Brand: BMW Series")
72+
73+
def test_resolve_match_path_model(self):
74+
m = self.patterns_pathall.resolve('/en-us/x5/')
75+
response = m.func(request=None, *m.args, **m.kwargs)
76+
self.assertEqual(response.content, b"Model: X5 2019")
77+
5978
def test_resolve_match_faillthrough(self):
6079
m = self.patterns_no_fallthrough.resolve('/bacon/')
6180
with self.assertRaises(urlresolvers.Resolver404):
@@ -103,6 +122,24 @@ def thing(request, name):
103122
return HttpResponse("Thing: " + name.title())
104123

105124

125+
def brand(request, name):
126+
brand = {
127+
'bmw': 'BMW Series',
128+
}
129+
if name in brand:
130+
return HttpResponse("Brand: " + brand[name])
131+
raise ContinueResolving
132+
133+
134+
def model(request, name):
135+
model = {
136+
'x5': 'X5 2019',
137+
}
138+
if name in model:
139+
return HttpResponse("Model: " + model[name])
140+
raise ContinueResolving
141+
142+
106143
if __name__ == '__main__':
107144
settings.configure()
108145
unittest.main()

0 commit comments

Comments
 (0)