Skip to content

Commit

Permalink
Deleting a dataset sends out IResourceController.before_delete events
Browse files Browse the repository at this point in the history
Although it doesn't actually delete the resources, because when a
sysadmin views the deleted dataset it should show with those resource,
and they wouldn't show up if we set their state=deleted.

This fixes #4705 by sending the IResourceController.before_delete events
necessary for Datastore and FileStore to get rid of the associated data.
  • Loading branch information
David Read committed Jul 12, 2019
1 parent 02c10a4 commit baa850a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
19 changes: 7 additions & 12 deletions ckan/logic/action/delete.py
Expand Up @@ -92,27 +92,22 @@ def package_delete(context, data_dict):
rev.author = user
rev.message = _(u'REST API: Delete Package: %s') % entity.name

# delete the dataset's resources
# notify IResourceControllers that the dataset's resources are essentially
# deleted, by virtue of the dataset being deleted
# BUT don't actually delete the resource (state=deleted) because would mean
# that when a sysadmin views the deleted dataset it would show with no
# resources
pkg_dict = _get_action('package_show')(context, {'id': id})
for res in pkg_dict['resources']:
for plugin in plugins.PluginImplementations(
plugins.IResourceController):
plugin.before_delete(context, res,
pkg_dict.get('resources', []))

def pop_resource(pkg_dict, res_id):
for res in pkg_dict.get('resources', []):
if res['id'] == res_id:
pkg_dict['resources'].remove(res)
return

pkg_dict = _get_action('package_show')(context, {'id': id})
for res in entity.resources:
pop_resource(pkg_dict, res.id)
res.delete()
for res in pkg_dict['resources']:
for plugin in plugins.PluginImplementations(
plugins.IResourceController):
plugin.after_delete(context, pkg_dict.get('resources', []))
plugin.after_delete(context, [])

# delete the dataset
for item in plugins.PluginImplementations(plugins.IPackageController):
Expand Down
10 changes: 6 additions & 4 deletions ckan/tests/logic/action/test_delete.py
Expand Up @@ -49,11 +49,13 @@ def test_with_resource(self):
dataset = helpers.call_action(
'package_show', {'user': sysadmin['name']}, id=dataset['id'])
assert_equals(dataset['state'], 'deleted')
# The resource is not shown though
assert_equals(dataset['resources'], [])
# The resource is still there but with state=deleted
# It's complete with resources
assert_equals([res['id'] for res in dataset['resources']],
[resource['id']])
# The resource is still there, not deleted, otherwise it wouldn't have
# shown up as part of the deleted dataset
res_obj = model.Resource.get(resource['id'])
assert_equals(res_obj.state, 'deleted')
assert_equals(res_obj.state, 'active')


class TestResourceDelete:
Expand Down

0 comments on commit baa850a

Please sign in to comment.