Skip to content

Commit

Permalink
Merge pull request #4509 from alycejenni/chain-core-actions
Browse files Browse the repository at this point in the history
Allow chaining off core actions
  • Loading branch information
wardi committed Mar 28, 2019
2 parents 793f57f + ddbbeb0 commit c87d021
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 4 additions & 2 deletions ckan/logic/__init__.py
Expand Up @@ -430,11 +430,13 @@ def get_action(action):
auth_function.auth_audit_exempt = True
fetched_actions[name] = auth_function
for name, func_list in chained_actions.iteritems():
if name not in fetched_actions:
if name not in fetched_actions and name not in _actions:
# nothing to override from plugins or core
raise NotFound('The action %r is not found for chained action' % (
name))
for func in reversed(func_list):
prev_func = fetched_actions[name]
# try other plugins first, fall back to core
prev_func = fetched_actions.get(name, _actions.get(name))
fetched_actions[name] = functools.partial(func, prev_func)

# Use the updated ones in preference to the originals.
Expand Down
22 changes: 21 additions & 1 deletion ckanext/datastore/tests/test_chained_action.py
Expand Up @@ -4,12 +4,19 @@
import ckan.plugins as p
import ckan.tests.helpers as helpers
import ckan.tests.factories as factories
from ckan.logic.action.get import package_list as core_package_list

from ckanext.datastore.tests.helpers import DatastoreFunctionalTestBase

assert_equals = nose.tools.assert_equals
assert_raises = nose.tools.assert_raises

package_list_message = u'The content of this message is largely irrelevant'


class TestActionException(Exception):
pass


@p.toolkit.chained_action
def datastore_delete(up_func, context, data_dict):
Expand All @@ -22,11 +29,19 @@ def datastore_delete(up_func, context, data_dict):
return result


@p.toolkit.chained_action
def package_list(next_func, context, data_dict):
# check it's received the core function as the first arg
assert_equals(next_func, core_package_list)
raise TestActionException(package_list_message)


class ExampleDataStoreDeletedWithCountPlugin(p.SingletonPlugin):
p.implements(p.IActions)

def get_actions(self):
return ({u'datastore_delete': datastore_delete})
return ({u'datastore_delete': datastore_delete,
u'package_list': package_list})


class TestChainedAction(DatastoreFunctionalTestBase):
Expand Down Expand Up @@ -67,3 +82,8 @@ def _create_datastore_resource(self, records):
helpers.call_action(u'datastore_create', **data)

return resource

def test_chain_core_action(self):
with assert_raises(TestActionException) as raise_context:
helpers.call_action(u'package_list', {})
assert_equals(raise_context.exception.message, package_list_message)

0 comments on commit c87d021

Please sign in to comment.