Skip to content

Commit

Permalink
[#2842] Wrap Pylons requests in a Flask application context
Browse files Browse the repository at this point in the history
On this particular branch this is needed so the common CKAN config
object can forward config options to the Flask app config object, but
this will be required anyway as more Flask features need to be available
during a Pylons request (see 62f55d2).
  • Loading branch information
amercader committed Jul 13, 2016
1 parent 048632d commit 4c37728
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion ckan/config/middleware/__init__.py
Expand Up @@ -132,4 +132,13 @@ def __call__(self, environ, start_response):

log.debug('Serving request via {0} app'.format(app_name))
environ['ckan.app'] = app_name
return self.apps[app_name](environ, start_response)
if app_name == 'flask_app':
return self.apps[app_name](environ, start_response)
else:
# Although this request will be served by Pylons we still
# need an application context in order for the Flask URL
# builder to work and to be able to access the Flask config
flask_app = self.apps['flask_app']._wsgi_app

with flask_app.app_context():
return self.apps[app_name](environ, start_response)
5 changes: 4 additions & 1 deletion ckan/config/middleware/flask_app.py
Expand Up @@ -17,7 +17,7 @@ def make_flask_stack(conf, **app_conf):
""" This has to pass the flask app through all the same middleware that
Pylons used """

app = CKANFlask(__name__)
app = flask_app = CKANFlask(__name__)

# Update Flask config with the CKAN values
app.config.update(conf)
Expand All @@ -31,6 +31,9 @@ def hello_world():
def hello_world_post():
return 'Hello World, this was posted to Flask'

# Add a reference to the actual Flask app so it's easier to access
app._wsgi_app = flask_app

return app


Expand Down

0 comments on commit 4c37728

Please sign in to comment.