Skip to content

Commit

Permalink
[#3196] Fall back to app_globals on Flask g
Browse files Browse the repository at this point in the history
If a property is not found on `g`, check CKAN's own `app_globals`,
where stuff like `g.site_title` is kept.

The default behaviour for Flask's `g` is to raise an AttributeError if
a missing property is accessed. We have kept this behaviour here and
handled a couple of cases where `c` was accessed outside of a request.
This can be reverted to returning an empty string if necessary.
  • Loading branch information
amercader committed Aug 11, 2016
1 parent 4112496 commit bfb1ca7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ckan/authz.py
Expand Up @@ -118,6 +118,9 @@ def _get_user(username):
try:
if c.userobj and c.userobj.name == username:
return c.userobj
except AttributeError:
# c.userobj not set
pass
except TypeError:
# c is not available
pass
Expand Down
14 changes: 14 additions & 0 deletions ckan/config/middleware/flask_app.py
Expand Up @@ -15,6 +15,7 @@ def make_flask_stack(conf, **app_conf):
Pylons used """

app = flask_app = CKANFlask(__name__)
app.app_ctx_globals_class = CKAN_AppCtxGlobals

# Update Flask config with the CKAN values. We use the common config
# object as values might have been modified on `load_environment`
Expand All @@ -38,6 +39,19 @@ def hello_world_post():
return app


class CKAN_AppCtxGlobals(_AppCtxGlobals):

'''Custom Flask AppCtxGlobal class (flask.g).'''

def __getattr__(self, name):
'''
If flask.g doesn't have attribute `name`, fall back to CKAN's
app_globals object.
If the key is also not found in there, an AttributeError will be raised
'''
return getattr(app_globals.app_globals, name)


class CKANFlask(Flask):

'''Extend the Flask class with a special method called on incoming
Expand Down
3 changes: 3 additions & 0 deletions ckan/logic/__init__.py
Expand Up @@ -220,6 +220,9 @@ def _prepopulate_context(context):
context.setdefault('session', model.Session)
try:
context.setdefault('user', c.user)
except AttributeError:
# c.user not set
pass
except TypeError:
# c not registered
pass
Expand Down

0 comments on commit bfb1ca7

Please sign in to comment.