Skip to content

Commit

Permalink
[#2939] New action package_owner_org_update
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Oct 2, 2012
1 parent bc5db90 commit 85a7d23
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ckan/logic/action/update.py
Expand Up @@ -968,3 +968,56 @@ def user_role_bulk_update(context, data_dict):
'domain_object': data_dict['domain_object']}
user_role_update(context, uro_data_dict)
return _get_action('roles_show')(context, data_dict)


def package_owner_org_update(context, data_dict):
'''Update the owning organization of a dataset
:param id: the name or id of the dataset to update
:type id: string
:param organization_id: the name or id of the owning organization
:type id: string
'''
model = context['model']
name_or_id = data_dict.get('id')
organization_id = data_dict.get('organization_id')

# FIXME auth

pkg = model.Package.get(name_or_id)
if pkg is None:
raise NotFound(_('Package was not found.'))
if organization_id:
org = model.Group.get(organization_id)
if org is None or not org.is_organization:
raise NotFound(_('Organization was not found.'))

# FIXME check we are in that org
pkg.owner_org = org.id
else:
org = None
pkg.owner_org = None


members = model.Session.query(model.Member).filter(model.Member.table_id == pkg.id).filter(model.Member.capacity == 'organization')

need_update = True
for member_obj in members:
if member_obj.group_id == org.id:
need_update = False
else:
member_obj.state = 'deleted'
member_obj.save()

# add the organization to memeber table
if org and need_update:
member_obj = model.Member(table_id=pkg.id,
table_name='package',
group=org,
capacity='organization',
group_id=org.id,
state='active')
model.Session.add(member_obj)

model.Session.commit()

0 comments on commit 85a7d23

Please sign in to comment.