Permalink
Browse files

add toolkit.get_endpoint

  • Loading branch information...
Your Name authored and tino097 committed Jul 22, 2018
1 parent 8760da8 commit b65d0ea3e288471aa35f7970095cb5d5d109da68
Showing with 39 additions and 4 deletions.
  1. +16 −4 CHANGELOG.rst
  2. +23 −0 ckan/plugins/toolkit.py
View
@@ -12,10 +12,22 @@ v.2.9.0 TBA
Deprecations:
* ``c.action`` and ``c.controller`` variables should be avoided.
Refer to either ``request.endpoint`` (generally it will be string in format
``BLUEPRINT_NAME.VIEW_NAME``, where blueprint is an alternative of
controller and view is an alternative of action) or ``request.url_rule``
(which is object with additional information about current rule).
``ckan.plugins.toolkit.get_endpoint`` can be used instead. This function
returns tuple of two items(depending on request handler):
1. Flask blueprint name / Pylons controller name
2. Flask view name / Pylons action name
In some cases, Flask blueprints have names that are differs from their
Pylons equivalents. For example, 'package' controller is divided between
'dataset' and 'resource' blueprints. For such cases you may need to perform
additional check of returned value:
>>> if toolkit.get_endpoint()[0] in ['dataset', 'package']:
>>> do_something()
In this code snippet, will be called if current request is handled via Flask's
dataset blueprint in CKAN>=2.9, and, in the same time, it's still working for
Pylons package controller in CKAN<2.9
v.2.8.0 2018-05-09
==================
View
@@ -44,6 +44,8 @@ class _Toolkit(object):
'literal',
# get logic action function
'get_action',
# get flask/pylons endpoint fragments
'get_endpoint',
# decorator for chained action
'chained_action',
# get navl schema converter
@@ -283,6 +285,7 @@ def _initialize(self):
t['add_ckan_admin_tab'] = self._add_ckan_admin_tabs
t['requires_ckan_version'] = self._requires_ckan_version
t['check_ckan_version'] = self._check_ckan_version
t['get_endpoint'] = self._get_endpoint
t['CkanVersionException'] = CkanVersionException
t['HelperError'] = HelperError
t['enqueue_job'] = enqueue_job
@@ -456,6 +459,26 @@ def _requires_ckan_version(cls, min_version, max_version=None):
)
raise CkanVersionException(error)
@classmethod
def _get_endpoint(cls):
"""Returns tuple in format: (controller|blueprint, action|view).
"""
import ckan.common as common
try:
# CKAN >= 2.8
endpoint = tuple(common.request.endpoint.split('.'))
except AttributeError:
try:
return common.c.controller, common.c.action
except AttributeError:
return (None, None)
# there are some routes('hello_world') that are not using blueprint
# For such case, let's assume that view function is a controller
# itself and action is None.
if len(endpoint) is 1:
return endpoint + (None,)
return endpoint
def __getattr__(self, name):
''' return the function/object requested '''
if not self._toolkit:

0 comments on commit b65d0ea

Please sign in to comment.