Skip to content

Commit

Permalink
Call the i18n Middleware first before asking the apps
Browse files Browse the repository at this point in the history
The i18n will remove the locale part of the environ['PATH_INFO']
(eg `fr` in `/fr/dataset`) and set the necessary lang vars in the
environ. We need to remove that before asking the apps if they can
handle a request, otherwise localized URLs won't get recognized.
  • Loading branch information
amercader committed May 25, 2016
1 parent a5b6efe commit 85d50cd
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions ckan/config/middleware.py
Expand Up @@ -12,7 +12,7 @@
import sqlalchemy as sa
from beaker.middleware import CacheMiddleware, SessionMiddleware
from paste.cascade import Cascade
from paste.registry import RegistryManager, Registry
from paste.registry import RegistryManager
from paste.urlparser import StaticURLParser
from paste.deploy.converters import asbool
from routes import request_config as routes_request_config
Expand Down Expand Up @@ -73,13 +73,11 @@ def custom_charset__set(self, charset):

def make_app(conf, full_stack=True, static_files=True, **app_conf):

# :::TODO::: like the flask app, make the pylons app respond to invites at
# /__invite__/, and handle can_handle_request requests.

pylons_app = make_pylons_stack(conf, full_stack, static_files, **app_conf)
flask_app = make_flask_stack(conf, **app_conf)

app = AskAppDispatcherMiddleware({'pylons_app': pylons_app, 'flask_app': flask_app})
app = AskAppDispatcherMiddleware({'pylons_app': pylons_app,
'flask_app': flask_app})

return app

Expand Down Expand Up @@ -188,8 +186,6 @@ def make_pylons_stack(conf, full_stack=True, static_files=True, **app_conf):
# Establish the Registry for this application
app = RegistryManager(app)

app = I18nMiddleware(app, config)

if asbool(static_files):
# Serve static files
static_max_age = None if not asbool(config.get('ckan.cache_enabled')) \
Expand Down Expand Up @@ -330,8 +326,6 @@ def hello_world_post():

# Start other middleware

app = I18nMiddleware(app, config)

# Initialize repoze.who
who_parser = WhoConfig(conf['here'])
who_parser.parse(open(app_conf['who.config_file']))
Expand Down Expand Up @@ -459,6 +453,8 @@ def __init__(self, apps=None, invites=(), ignore_missing_services=False):

self.send_invitations(apps)

self.i18n_middleware = I18nMiddleware()

def send_invitations(self, apps):
'''Call each app at the invite route to establish a partyline. Called
on init.'''
Expand All @@ -474,7 +470,10 @@ def send_invitations(self, apps):
def __call__(self, environ, start_response):
'''Determine which app to call by asking each app if it can handle the
url and method defined on the eviron'''
# :::TODO::: Enforce order of precedence for dispatching to apps here.

# Handle the i18n first, otherwise localized URLs (eg `/jp/about`)
# won't get recognized by the app route mappers
self.i18n_middleware(environ, start_response)

app_name = 'pylons_app' # currently defaulting to pylons app
answers = self.ask_around('can_handle_request', environ)
Expand Down Expand Up @@ -545,8 +544,7 @@ def __call__(self, environ, start_response):
class I18nMiddleware(object):
"""I18n Middleware selects the language based on the url
eg /fr/home is French"""
def __init__(self, app, config):
self.app = app
def __init__(self):
self.default_locale = config.get('ckan.locale_default', 'en')
self.local_list = get_locales_from_config()

Expand All @@ -559,7 +557,6 @@ def __call__(self, environ, start_response):

# We only update once for a request so we can keep
# the language and original url which helps with 404 pages etc
# import ipdb; ipdb.set_trace()
if 'CKAN_LANG' not in environ:
path_parts = environ['PATH_INFO'].split('/')
if len(path_parts) > 1 and path_parts[1] in self.local_list:
Expand All @@ -577,7 +574,8 @@ def __call__(self, environ, start_response):
# Current application url
path_info = environ['PATH_INFO']
# sort out weird encodings
path_info = '/'.join(urllib.quote(pce, '') for pce in path_info.split('/'))
path_info = '/'.join(urllib.quote(pce, '') for pce
in path_info.split('/'))

qs = environ.get('QUERY_STRING')

Expand All @@ -588,8 +586,6 @@ def __call__(self, environ, start_response):
else:
environ['CKAN_CURRENT_URL'] = path_info

return self.app(environ, start_response)


class PageCacheMiddleware(object):
''' A simple page cache that can store and serve pages. It uses
Expand Down

0 comments on commit 85d50cd

Please sign in to comment.