Skip to content

Commit

Permalink
CLI: Reuse options in verdi user configure from setup
Browse files Browse the repository at this point in the history
This way it is guaranteed that the same types are being used, which were
actually different. The `--set-default` flag now also gets its default
from the current value on the selected user, just as is done for the
other properties.
  • Loading branch information
sphuber committed Nov 12, 2023
1 parent 8f8f558 commit 1c0b702
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 56 deletions.
67 changes: 12 additions & 55 deletions aiida/cmdline/commands/cmd_user.py
Expand Up @@ -8,31 +8,14 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""`verdi user` command."""
from functools import partial

import click

from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.cmdline.params import arguments, options, types
from aiida.cmdline.params.options.commands import setup as options_setup
from aiida.cmdline.utils import decorators, echo


def get_user_attribute_default(attribute, ctx):
"""Return the default value for the given attribute of the user passed in the context.
:param attribute: attribute for which to get the current value
:param ctx: click context which should contain the selected user
:return: user attribute default value if set, or None
"""
default = getattr(ctx.params['user'], attribute)

# None or empty string means there is no default
if not default:
return None

return default


@verdi.group('user')
def verdi_user():
"""Inspect and manage users."""
Expand Down Expand Up @@ -71,57 +54,31 @@ def user_list():
type=types.UserParamType(create=True),
cls=options.interactive.InteractiveOption
)
@click.option(
'--first-name',
prompt='First name',
help='First name of the user.',
type=click.STRING,
contextual_default=partial(get_user_attribute_default, 'first_name'),
cls=options.interactive.InteractiveOption
)
@click.option(
'--last-name',
prompt='Last name',
help='Last name of the user.',
type=click.STRING,
contextual_default=partial(get_user_attribute_default, 'last_name'),
cls=options.interactive.InteractiveOption
)
@click.option(
'--institution',
prompt='Institution',
help='Institution of the user.',
type=click.STRING,
contextual_default=partial(get_user_attribute_default, 'institution'),
cls=options.interactive.InteractiveOption
)
@options_setup.SETUP_USER_FIRST_NAME(contextual_default=lambda ctx: ctx.params['user'].first_name)
@options_setup.SETUP_USER_LAST_NAME(contextual_default=lambda ctx: ctx.params['user'].last_name)
@options_setup.SETUP_USER_INSTITUTION(contextual_default=lambda ctx: ctx.params['user'].institution)
@click.option(
'--set-default',
prompt='Set as default?',
help='Set the user as the default user for the current profile.',
is_flag=True,
cls=options.interactive.InteractiveOption
cls=options.interactive.InteractiveOption,
contextual_default=lambda ctx: ctx.params['user'].is_default
)
@click.pass_context
@decorators.with_dbenv()
def user_configure(ctx, user, first_name, last_name, institution, set_default):
def user_configure(ctx, user, first_name, last_name, institution, set_default): # pylint: disable=too-many-arguments
"""Configure a new or existing user.
An e-mail address is used as the user name.
"""
# pylint: disable=too-many-arguments
if first_name is not None:
user.first_name = first_name
if last_name is not None:
user.last_name = last_name
if institution is not None:
user.institution = institution

action = 'updated' if user.is_stored else 'created'

user.first_name = first_name
user.last_name = last_name
user.institution = institution
user.store()

echo.echo_success(f'{user.email} successfully {action}')
echo.echo_success(f'Successfully {action} `{user.email}`.')

if set_default:
ctx.invoke(user_set_default, user=user)
Expand All @@ -135,4 +92,4 @@ def user_set_default(ctx, user):
"""Set a user as the default user for the profile."""
from aiida.manage import get_manager
get_manager().set_default_user_email(ctx.obj.profile, user.email)
echo.echo_success(f'set `{user.email}` as the new default user for profile `{ctx.obj.profile.name}`')
echo.echo_success(f'Set `{user.email}` as the default user for profile `{ctx.obj.profile.name}.`')
2 changes: 1 addition & 1 deletion tests/cmdline/commands/test_user.py
Expand Up @@ -83,5 +83,5 @@ def test_set_default(run_cli_command, create_user):
assert orm.User.collection.get_default().email != new_user.email

result = run_cli_command(cmd_user.user_set_default, [new_user.email])
assert f'set `{new_user.email}` as the new default user' in result.output
assert f'Set `{new_user.email}` as the default user' in result.output
assert orm.User.collection.get_default().email == new_user.email

0 comments on commit 1c0b702

Please sign in to comment.