Skip to content

Commit

Permalink
Allow chaining off core actions
Browse files Browse the repository at this point in the history
Attempt to find an action from another plugin to override first, but failing that, override from the core actions.
  • Loading branch information
alycejenni committed Oct 17, 2018
1 parent e70f6e0 commit d857724
Show file tree
Hide file tree
Showing 2 changed files with 24 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
21 changes: 20 additions & 1 deletion ckanext/datastore/tests/test_chained_action.py
Expand Up @@ -4,12 +4,18 @@
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 +28,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 +81,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 d857724

Please sign in to comment.