Skip to content

Commit

Permalink
Generalise IResourceUpload into IResourceModification
Browse files Browse the repository at this point in the history
Instead of proliferating the interfaces with special resource
cases, by creating yet another resource interface for any resource
creation (not only uploads), IResourceModification incorporates
both uploads and links, but after_create (which replaces
after_upload) provides an additional parameter indicating whether
the resource was uploaded or not.

This entail renaming but also a slightly different workflow in
calling the interface since we must catch deletions before
creations.
  • Loading branch information
Tryggvi Bjorgvinsson committed Sep 12, 2014
1 parent 762a519 commit 4755cdd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
10 changes: 5 additions & 5 deletions ckan/logic/action/create.py
Expand Up @@ -294,12 +294,12 @@ def resource_create(context, data_dict):
pkg_dict = _get_action('package_show')(context, {'id': package_id})
resource = pkg_dict['resources'][-1]

if result == 'file uploaded':
for plugin in plugins.PluginImplementations(plugins.IResourceUpload):
plugin.after_upload(context, resource)
elif result == 'file deleted':
for plugin in plugins.PluginImplementations(plugins.IResourceUpload):
for plugin in plugins.PluginImplementations(plugins.IResourceModification):
if result == 'file deleted':
plugin.after_delete(context, resource)
else:
uploaded = (result == 'file uploaded')
plugin.after_create(context, resource, upload=uploaded)

return resource

Expand Down
10 changes: 5 additions & 5 deletions ckan/logic/action/update.py
Expand Up @@ -243,12 +243,12 @@ def resource_update(context, data_dict):

resource = _get_action('resource_show')(context, {'id': id})

if result == 'file uploaded':
for plugin in plugins.PluginImplementations(plugins.IResourceUpload):
plugin.after_upload(context, resource)
elif result == 'file deleted':
for plugin in plugins.PluginImplementations(plugins.IResourceUpload):
for plugin in plugins.PluginImplementations(plugins.IResourceModification):
if result == 'file deleted':
plugin.after_delete(context, resource)
else:
uploaded = (result == 'file uploaded')
plugin.after_create(context, resource, uploaded)

return resource

Expand Down
48 changes: 37 additions & 11 deletions ckan/plugins/interfaces.py
Expand Up @@ -12,7 +12,7 @@
'IOrganizationController',
'IPackageController', 'IPluginObserver',
'IConfigurable', 'IConfigurer',
'IActions', 'IResourceUrlChange', 'IDatasetForm',
'IActions', 'IResourceModification', 'IResourceUrlChange', 'IDatasetForm',
'IValidators', 'IConverters',
'IResourcePreview',
'IResourceView',
Expand All @@ -22,7 +22,6 @@
'ITemplateHelpers',
'IFacets',
'IAuthenticator',
'IResourceUpload',
]

from inspect import isclass
Expand Down Expand Up @@ -194,6 +193,42 @@ def notify(self, entity, operation):
pass


class IResourceModification(Interface):
"""
Add custom processing after a resource is created, updated or deleted.
"""

def after_create(self, context, resource, upload):
"""
Extensions will receive this after a resource is created.
:param context: The context object of the current request, this
includes for example access to the ``model`` and the ``user``.
:type context: dictionary
:param resource: An object representing the latest resource added
to the dataset (the one that was just created).
:type resource: dictionary
:param upload: Representation of whether the resource was
uploaded (True) or not (False).
:type upload: boolean
"""
pass

def after_delete(self, context, resource):
"""
Extensions will receive this after a previously created resource is
deleted.
:param context: The context object of the current request, this
includes for example access to the ``model`` and the ``user``.
:type context: dictionary
:param resource: An object representing the latest resource added
to the dataset (the one that was just deleted).
:type resource: dictionary
"""
pass


class IResourceUrlChange(Interface):
"""
Receives notification of changed urls.
Expand Down Expand Up @@ -1266,12 +1301,3 @@ def abort(self, status_code, detail, headers, comment):
'''called on abort. This allows aborts due to authorization issues
to be overriden'''
return (status_code, detail, headers, comment)


class IResourceUpload(Interface):

def after_upload(self, context, resource):
pass

def after_delete(self, context, resource):
pass

0 comments on commit 4755cdd

Please sign in to comment.