Skip to content

Commit

Permalink
[#2003] start separate file for patch actions
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Nov 3, 2014
1 parent cc720cf commit 20c44b6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion ckan/logic/__init__.py
Expand Up @@ -360,7 +360,7 @@ def get_action(action):
# Rather than writing them out in full will use __import__
# to load anything from ckan.logic.action that looks like it might
# be an action
for action_module_name in ['get', 'create', 'update', 'delete']:
for action_module_name in ['get', 'create', 'update', 'delete', 'patch']:
module_path = 'ckan.logic.action.' + action_module_name
module = __import__(module_path)
for part in module_path.split('.')[1:]:
Expand Down
32 changes: 32 additions & 0 deletions ckan/logic/action/patch.py
@@ -0,0 +1,32 @@
'''API functions for partial updates of existing data in CKAN'''

import ckan.logic.action.update as _update
from ckan.logic import get_action as _get_action

def package_patch(context, data_dict):
'''Patch a dataset (package).
The difference between the update and patch methods is that the patch will
perform an update of the provided parameters, while leaving all other
parameters unchanged, whereas the update methods deletes all parameters
not explicitly provided in the data_dict
You must be authorized to edit the dataset and the groups that it belongs
to.
'''

_check_access('package_patch', context, data_dict)

name_or_id = data_dict.get("name") or _get_or_bust(data_dict, "id")
show_context = {
'model': context['model'],
'session': context['session'],
'user': context['user'],
'auth_user_obj': context['auth_user_obj'],
}

package_dict = _get_action('package_show')(show_context, {'id': name_or_id})

patched = dict(package_dict.items() + data_dict.items())
return _update.package_update(context, patched)
29 changes: 0 additions & 29 deletions ckan/logic/action/update.py
Expand Up @@ -455,35 +455,6 @@ def package_update(context, data_dict):

return output

def package_patch(context, data_dict):
'''Patch a dataset (package).
The difference between the update and patch methods is that the patch will
perform an update of the provided parameters, while leaving all other
parameters unchanged, whereas the update methods deletes all parameters
not explicitly provided in the data_dict
You must be authorized to edit the dataset and the groups that it belongs
to.
'''

_check_access('package_patch', context, data_dict)

name_or_id = data_dict.get("name") or _get_or_bust(data_dict, "id")
show_context = {
'model': context['model'],
'session': context['session'],
'user': context['user'],
'auth_user_obj': context['auth_user_obj'],
}

package_dict = _get_action('package_show')(show_context, {'id': name_or_id})

patched = dict(package_dict.items() + data_dict.items())
return package_update(context, patched)


def package_resource_reorder(context, data_dict):
'''Reorder resources against datasets. If only partial resource ids are
supplied then these are assumed to be first and the other resources will
Expand Down
4 changes: 4 additions & 0 deletions ckan/logic/auth/patch.py
@@ -0,0 +1,4 @@
from ckan import logic
import ckan.logic.auth.update as _update

package_patch = _update.package_update
4 changes: 0 additions & 4 deletions ckan/logic/auth/update.py
Expand Up @@ -48,10 +48,6 @@ def package_update(context, data_dict):

return {'success': True}

@logic.auth_allow_anonymous_access
def package_patch(context, data_dict):
return package_update(context, data_dict)

def package_resource_reorder(context, data_dict):
## the action function runs package update so no need to run it twice
return {'success': True}
Expand Down
2 changes: 1 addition & 1 deletion ckan/new_authz.py
Expand Up @@ -44,7 +44,7 @@ def _build(self):

module_root = 'ckan.logic.auth'

for auth_module_name in ['get', 'create', 'update', 'delete']:
for auth_module_name in ['get', 'create', 'update', 'delete', 'patch']:
module_path = '%s.%s' % (module_root, auth_module_name,)
try:
module = __import__(module_path)
Expand Down

0 comments on commit 20c44b6

Please sign in to comment.