Skip to content

Commit

Permalink
monkey patch the resource_delete action and throw a validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuckley committed Jul 23, 2018
1 parent 0fa6e06 commit 9b95ab1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions ckanext/protected_resources/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import ckan.plugins.toolkit as tk
import ckan.logic as logic
from ckan.logic.auth import get_resource_object, get_package_object
from ckan.logic.action.delete import resource_delete as resource_delete_core
from ckan import model
from ckan.lib.base import BaseController, c

log = logging.getLogger(__name__)

log = logging.getLogger(__name__)

class ProtectedResourceController(BaseController):

Expand Down Expand Up @@ -164,6 +165,15 @@ def package_has_protected_resource(package_dict):
protected_resources = [r for r in package_dict.get('resources', []) if r.get('is_protected', False)]
return len(protected_resources) != 0

def resource_delete_override(context, data_dict):

resource = p.toolkit.get_action('resource_show')(context, data_dict)

if resource.get('is_protected', None):
raise p.toolkit.ValidationError(['A protected resource can never be deleted'])
return resource_delete_core(context, data_dict)


class Protected_ResourcesPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
p.implements(p.IAuthFunctions)
p.implements(p.IActions)
Expand All @@ -190,7 +200,8 @@ def update_config(self, config):
def get_actions(self):
return {
'protected_resource_lock': protected_resource_lock,
'protected_resource_unlock': protected_resource_unlock
'protected_resource_unlock': protected_resource_unlock,
'resource_delete': resource_delete_override
}

# IAuthFunctions
Expand Down

0 comments on commit 9b95ab1

Please sign in to comment.