diff --git a/VERSION b/VERSION index a04d3ca4..960403c0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.00.0 (2012-08-07 16:53:00) dev +Version 2.00.0 (2012-08-07 19:39:42) dev diff --git a/gluon/rewrite.py b/gluon/rewrite.py index 25b2250a..da5f92a3 100644 --- a/gluon/rewrite.py +++ b/gluon/rewrite.py @@ -43,6 +43,7 @@ def _router_default(): default_language = None, languages = None, root_static = ['favicon.ico', 'robots.txt'], + map_static = None, domains = None, exclusive_domain = False, map_hyphen = False, @@ -1083,6 +1084,7 @@ class MapUrlOut(object): self.env = env self.application = application self.controller = controller + self.is_static = (controller == 'static' or controller.startswith('static/')) self.function = function self.args = args self.other = other @@ -1188,7 +1190,7 @@ class MapUrlOut(object): # handle static as a special case # (easier for external static handling) # - if self.controller == 'static' or self.controller.startswith('static/'): + if self.is_static: if not self.map_static: self.omit_application = False if self.language: @@ -1206,10 +1208,14 @@ class MapUrlOut(object): self.function = self.function.replace('_', '-') if not self.omit_application: acf += '/' + self.application - if not self.omit_language: - acf += '/' + self.language - if not self.omit_controller: - acf += '/' + self.controller + # handle case of flipping lang/static/file to static/lang/file for external rewrite + if self.is_static and self.map_static is False and not self.omit_language: + acf += '/' + self.controller + '/' + self.language + else: + if not self.omit_language: + acf += '/' + self.language + if not self.omit_controller: + acf += '/' + self.controller if not self.omit_function: acf += '/' + self.function if self.path_prefix: @@ -1249,9 +1255,21 @@ def map_url_in(request, env, app=False): root_static_file = map.map_root_static() # handle root-static files if root_static_file: return (root_static_file, map.env) - map.map_language() - map.map_controller() + # handle mapping of lang/static to static/lang in externally-rewritten URLs + # in case we have to handle them ourselves + if map.languages and map.map_static is False and map.arg0 == 'static' and map.args(1) in map.languages: + if 'es' in map.languages: + print 'handle static/lang %s' % map.args(1) + map.map_controller() + map.map_language() + else: + if 'es' in map.languages: + print 'NO handle static/lang %s' % map.args(1) + map.map_language() + map.map_controller() static_file = map.map_static() + if 'es' in map.languages: + print 'static_file=%s' % static_file if static_file: return (static_file, map.env) map.map_function() diff --git a/gluon/tests/test_router.py b/gluon/tests/test_router.py index d3822919..5cf7b1aa 100644 --- a/gluon/tests/test_router.py +++ b/gluon/tests/test_router.py @@ -542,7 +542,7 @@ class TestRouter(unittest.TestCase): self.assertEqual(filter_url('https://domain.com/init/static/file', out=True), "/static/file") self.assertEqual(filter_url('https://domain.com/init/static/index', out=True), "/static/index") - router_out['init']['map_static'] = False + router_out['init']['map_static'] = None load(rdict=router_out) self.assertEqual(filter_url('https://domain.com/init/static/file', out=True), "/init/static/file") self.assertEqual(filter_url('https://domain.com/init/static/index', out=True), "/init/static/index") @@ -744,6 +744,28 @@ class TestRouter(unittest.TestCase): self.assertEqual(filter_url('https://domain.com/welcome/ctr/fcn', lang='it', out=True), "/welcome/ctr/fcn") self.assertEqual(filter_url('https://domain.com/welcome/ctr/fcn', lang='es', out=True), "/welcome/ctr/fcn") + router_lang['admin']['map_static'] = False + router_lang['examples']['map_static'] = False + load(rdict=router_lang) + self.assertEqual(filter_url('https://domain.com/admin/ctr/fcn', lang='en', out=True), "/ctr/fcn") + self.assertEqual(filter_url('https://domain.com/admin/ctr/fcn', lang='it', out=True), "/it/ctr/fcn") + self.assertEqual(filter_url('https://domain.com/admin/ctr/fcn', lang='it-it', out=True), "/it-it/ctr/fcn") + self.assertEqual(filter_url('https://domain.com/admin/static/file', lang='en', out=True), "/admin/static/en/file") + self.assertEqual(filter_url('https://domain.com/admin/static/file', lang='it', out=True), "/admin/static/it/file") + self.assertEqual(filter_url('https://domain.com/admin/static/file', lang='it-it', out=True), "/admin/static/it-it/file") + self.assertEqual(filter_url('https://domain.com/welcome/ctr/fcn', lang='it', out=True), "/welcome/ctr/fcn") + self.assertEqual(filter_url('https://domain.com/welcome/ctr/fcn', lang='es', out=True), "/welcome/ctr/fcn") + self.assertEqual(filter_url('http://domain.com/static/file'), "%s/applications/admin/static/file" % root) + self.assertEqual(filter_url('http://domain.com/en/static/file'), "%s/applications/admin/static/file" % root) + self.assertEqual(filter_url('http://domain.com/examples/en/static/file'), "%s/applications/examples/static/en/file" % root) + self.assertEqual(filter_url('http://domain.com/examples/static/file'), "%s/applications/examples/static/en/file" % root) + self.assertEqual(filter_url('http://domain.com/examples/it/static/file'), "%s/applications/examples/static/it/file" % root) + self.assertEqual(filter_url('http://domain.com/examples/it-it/static/file'), "%s/applications/examples/static/file" % root) + + self.assertEqual(filter_url('http://domain.com/examples/static/en/file'), "%s/applications/examples/static/en/file" % root) + self.assertEqual(filter_url('http://domain.com/examples/static/it/file'), "%s/applications/examples/static/it/file" % root) + self.assertEqual(filter_url('http://domain.com/examples/static/it-it/file'), "%s/applications/examples/static/it-it/file" % root) + def test_router_get_effective(self): ''' Test get_effective_router diff --git a/router.example.py b/router.example.py index 06710770..067b0e3e 100644 --- a/router.example.py +++ b/router.example.py @@ -61,8 +61,9 @@ # map_hyphen: If True (default is False), hyphens in incoming /a/c/f fields are converted # to underscores, and back to hyphens in outgoing URLs. # Language, args and the query string are not affected. -# map_static: By default, the default application is not stripped from static URLs. +# map_static: By default (None), the default application is not stripped from static URLs. # Set map_static=True to override this policy. +# Set map_static=False to map lang/static/file to static/lang/file # acfe_match: regex for valid application, controller, function, extension /a/c/f.e # file_match: regex for valid subpath (used for static file paths) # if file_match does not contain '/', it is uses to validate each element of a static file subpath, @@ -84,6 +85,7 @@ # default_language = None, # languages = None, # root_static = ['favicon.ico', 'robots.txt'], +# map_static = None, # domains = None, # map_hyphen = False, # acfe_match = r'\w+$', # legal app/ctlr/fcn/ext