Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions ckanapi/cli/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def reply(action, error, response):
# do not send resource_views & datastore_fields to package actions
resource_views = {}
datastore_fields = {}
group_users = []
if thing == 'datasets' and obj.get('resources'):
for r in obj['resources']:
# NOTE: will only work with existing Resource IDs in the input,
Expand All @@ -220,10 +221,15 @@ def reply(action, error, response):
resource_views[r['id']] = r.pop('resource_views', [])
if arguments['--datastore-fields']:
datastore_fields[r['id']] = r.pop('datastore_fields', [])
if thing in ('groups', 'organizations') and obj.get('users'):
group_users = obj.pop('users', [])
if not arguments['--append-users']:
obj['users'] = group_users
if existing:
r = ckan.call_action(thing_update, obj,
requests_kwargs=requests_kwargs)
else:
# FIXME: add ignore_not_sysadmin to creator_user_id to ckan core....
r = ckan.call_action(thing_create, obj,
requests_kwargs=requests_kwargs)
if thing == 'datasets' and 'resources' in obj:
Expand All @@ -235,14 +241,16 @@ def reply(action, error, response):
_upload_resources(ckan, obj, arguments)
if arguments['--resource-views'] and resource_views: # check if it is needed to create resource views when creating/updating packages
created_views, updated_views, skipped_views = _load_resource_views(ckan, resource_views, arguments)
if thing in ['groups','organizations'] and 'image_display_url' in obj: # load images for groups and organizations
if arguments['--upload-logo']:
if thing in ('groups', 'organizations'):
if 'image_display_url' in obj and arguments['--upload-logo']: # load images for groups and organizations
users = obj['users']
obj = _upload_logo(ckan,obj)
obj.pop('image_upload')
obj['users'] = users
ckan.call_action(thing_update, obj,
requests_kwargs=requests_kwargs)
if arguments['--append-users'] and group_users: # check if it is needed to append group/org users
set_members = _load_group_members(ckan, group_users, arguments, thing, r['id'])
if thing == 'users' and arguments['--api-tokens'] and api_token_list: # check if it is needed to create user api tokens when creating/updating users
created_tokens = _load_user_api_tokens(ckan, api_token_list, arguments)
except ValidationError as e:
Expand All @@ -269,6 +277,11 @@ def reply(action, error, response):
log_obj['created_datastore_tables'] = created_tables
if skipped_tables:
log_obj['skipped_datastore_tables'] = skipped_tables
if thing in ('groups', 'organizations'):
if arguments['--append-users'] and group_users and set_members:
log_obj['set_members'] = set_members
elif group_users:
log_obj['set_members'] = ['%s[%s]' % (m['name'], m['capacity']) for m in group_users]
reply(act, None, log_obj)

def _worker_command_line(thing, arguments):
Expand All @@ -295,6 +308,7 @@ def b(name):
+ b('--api-tokens')
+ b('--datastore-fields')
+ b('--resource-views')
+ b('--append-users')
)


Expand Down Expand Up @@ -456,3 +470,27 @@ def _load_user_api_tokens(ckan, api_token_list, arguments):
requests_kwargs=requests_kwargs)
created_tokens.append(token['name'])
return created_tokens


def _load_group_members(ckan, user_list, arguments, thing, obj_id):
"""
Appends users as members for Groups/Organizations
"""
requests_kwargs = None
if arguments['--insecure']:
requests_kwargs = {'verify': False}
set_members = []
action = 'group_member_create' if thing == 'group' \
else 'organization_member_create'
for user in user_list:
# exceptions handled in load_things_worker
ckan.call_action(
action,
{
'id': obj_id,
'username': user['name'],
'role': user['capacity'],
},
requests_kwargs=requests_kwargs)
set_members.append('%s[%s]' % (user['name'], user['capacity']))
return set_members
4 changes: 3 additions & 1 deletion ckanapi/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
[[-c CONFIG] [-u USER] | -r SITE_URL [-a APIKEY] [--insecure]]
ckanapi load (groups | organizations)
[--upload-logo] [-I JSONL_INPUT] [-s START] [-m MAX]
[-p PROCESSES] [-l LOG_FILE] [-n | -o] [-qwzU]
[-p PROCESSES] [-l LOG_FILE] [-n | -o] [-qwzUA]
[[-c CONFIG] [-u USER] | -r SITE_URL [-a APIKEY] [--insecure]]
ckanapi load users
[-I JSONL_INPUT] [-s START] [-m MAX] [-p PROCESSES] [-l LOG_FILE]
Expand Down Expand Up @@ -92,6 +92,8 @@
-u --ckan-user=USER perform actions as user with this name, uses the
site sysadmin user when not specified
-U --include-users include users of a group/organization
-A --append-users adds users to a group/organization instead of truncating them.
Mutually exclusive with -U
--api-tokens export API Token information along with
user metadata as api_token_list lists (dump).
create API Tokens for users (load).
Expand Down
Loading
Loading