Skip to content

Commit

Permalink
Add global properties controller and action
Browse files Browse the repository at this point in the history
There are a bunch of extensions that relies on this props. This PR
is attempting to add them inside `before_each_request` hook. If
url.endpoint contains dot, than part before dot will be considered
as controller and part after the dot - as action. otherwise(routes
without dot, like test /hello route), view name is used for both,
controller and action name
  • Loading branch information
smotornyuk committed Jul 3, 2018
1 parent 9dd6b2c commit 444fe18
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ckan/config/middleware/flask_app.py
Expand Up @@ -32,6 +32,7 @@
from ckan.views import (identify_user,
set_cors_headers_for_response,
check_session_cookie,
set_controller_and_action
)


Expand Down Expand Up @@ -279,6 +280,10 @@ def ckan_before_request():
# Sets g.user and g.userobj
identify_user()

# Provide g.controller and g.action for backward compatibility
# with extensions
set_controller_and_action()


def ckan_after_request(response):
u'''Common handler executed after all Flask requests'''
Expand Down
9 changes: 9 additions & 0 deletions ckan/views/__init__.py
Expand Up @@ -179,3 +179,12 @@ def _get_user_for_apikey():
query = model.Session.query(model.User)
user = query.filter_by(apikey=apikey).first()
return user


def set_controller_and_action():
try:
controller, action = request.endpoint.split('.')
except ValueError:
log.debug('Endpoint does not contain dot: {}'.format(request.endpoint))
controller = action = request.endpoint
g.controller, g.action = controller, action

0 comments on commit 444fe18

Please sign in to comment.