Skip to content

Commit

Permalink
Merge pull request #2633 from ckan/2631-group-purge
Browse files Browse the repository at this point in the history
group_purge fixes
  • Loading branch information
rossjones committed Sep 14, 2015
2 parents ed5fe6e + 147ebed commit c991cd6
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 416 deletions.
38 changes: 34 additions & 4 deletions ckan/logic/action/delete.py
@@ -1,9 +1,12 @@
'''API functions for deleting data from CKAN.'''

import sqlalchemy as sqla

import ckan.logic
import ckan.logic.action
import ckan.plugins as plugins
import ckan.lib.dictization.model_dictize as model_dictize
from ckan import authz

from ckan.common import _

Expand Down Expand Up @@ -440,12 +443,34 @@ def _group_or_org_purge(context, data_dict, is_org=False):
else:
_check_access('group_purge', context, data_dict)

members = model.Session.query(model.Member)
members = members.filter(model.Member.group_id == group.id)
if is_org:
# Clear the owner_org field
datasets = model.Session.query(model.Package) \
.filter_by(owner_org=group.id) \
.filter(model.Package.state != 'deleted') \
.count()
if datasets:
if not authz.check_config_permission('ckan.auth.create_unowned_dataset'):
raise ValidationError('Organization cannot be purged while it '
'still has datasets')
pkg_table = model.package_table
# using Core SQLA instead of the ORM should be faster
model.Session.execute(
pkg_table.update().where(
sqla.and_(pkg_table.c.owner_org == group.id,
pkg_table.c.state != 'deleted')
).values(owner_org=None)
)

# Delete related Memberships
members = model.Session.query(model.Member) \
.filter(sqla.or_(model.Member.group_id == group.id,
model.Member.table_id == group.id))
if members.count() > 0:
model.repo.new_revision()
# no need to do new_revision() because Member is not revisioned, nor
# does it cascade delete any revisioned objects
for m in members.all():
m.delete()
m.purge()
model.repo.commit_and_remove()

group = model.Group.get(id)
Expand All @@ -462,6 +487,8 @@ def group_purge(context, data_dict):
whereas deleting a group simply marks the group as deleted (it will no
longer show up in the frontend, but is still in the db).
Datasets in the organization will remain, just not in the purged group.
You must be authorized to purge the group.
:param id: the name or id of the group to be purged
Expand All @@ -480,6 +507,9 @@ def organization_purge(context, data_dict):
deleted (it will no longer show up in the frontend, but is still in the
db).
Datasets owned by the organization will remain, just not in an
organization any more.
You must be authorized to purge the organization.
:param id: the name or id of the organization to be purged
Expand Down

0 comments on commit c991cd6

Please sign in to comment.