Skip to content

Commit

Permalink
Fix sysadmin-add command
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jan 8, 2020
1 parent fac2a16 commit 9029e43
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 3 additions & 4 deletions ckan/cli/sysadmin.py
Expand Up @@ -43,16 +43,15 @@ def list_sysadmins():
@sysadmin.command(help=u"Convert user into a sysadmin.")
@click.argument(u"username")
@click.argument(u"args", nargs=-1)
def add(username, args):

@click.pass_context
def add(ctx, username, args):
user = model.User.by_name(text_type(username))
if not user:
click.secho(u'User "%s" not found' % username, fg=u"red")
if click.confirm(
u"Create new user: %s?" % username, default=True, abort=True
):
# TODO: once, cli.user is merged, invoke `user.add_user` instead
add_user([username] + list(args))
ctx.forward(add_user)
user = model.User.by_name(text_type(username))

user.sysadmin = True
Expand Down
9 changes: 7 additions & 2 deletions ckan/cli/user.py
Expand Up @@ -25,7 +25,7 @@ def user():
@click.argument(u'username')
@click.argument(u'args', nargs=-1)
@click.pass_context
def add_user(username, args):
def add_user(ctx, username, args):
u'''Add new user if we use ckan sysadmin add
or ckan user add
'''
Expand Down Expand Up @@ -70,11 +70,16 @@ def add_user(username, args):
u'ignore_auth': True,
u'user': site_user['name'],
}
user_dict = logic.get_action(u'user_create')(context, data_dict)
flask_app = ctx.meta['flask_app']
# Current user is tested agains sysadmin role during model
# dictization, thus we need request context
with flask_app.test_request_context():
user_dict = logic.get_action(u'user_create')(context, data_dict)
click.secho(u"Successfully created user: %s" % user_dict['name'],
fg=u'green', bold=True)
except logic.ValidationError as e:
error_shout(e)
raise click.Abort()


def get_user_str(user):
Expand Down

0 comments on commit 9029e43

Please sign in to comment.