diff options
| author | Daniel Schadt <kingdread@gmx.de> | 2023-05-10 21:10:52 +0200 | 
|---|---|---|
| committer | Daniel Schadt <kingdread@gmx.de> | 2023-05-10 21:10:52 +0200 | 
| commit | 41e9ca178b1db3d89164ae7d56d749212d5de8d9 (patch) | |
| tree | 2c56142d7168f29ed0402a7bbce9b2281d3e91d2 | |
| parent | add18020a77d9c5c40fdfce854d52a81296bfdcf (diff) | |
| download | fietsboek-41e9ca178b1db3d89164ae7d56d749212d5de8d9.tar.gz fietsboek-41e9ca178b1db3d89164ae7d56d749212d5de8d9.tar.bz2 fietsboek-41e9ca178b1db3d89164ae7d56d749212d5de8d9.zip  | |
add a fietsctl user modify command
Currently, there is no way to make a user an admin after creating them -
so this changes that!
| -rw-r--r-- | fietsboek/scripts/fietsctl.py | 48 | 
1 files changed, 48 insertions, 0 deletions
diff --git a/fietsboek/scripts/fietsctl.py b/fietsboek/scripts/fietsctl.py index caca0f9..9bf8abe 100644 --- a/fietsboek/scripts/fietsctl.py +++ b/fietsboek/scripts/fietsctl.py @@ -16,6 +16,15 @@ EXIT_OKAY = 0  EXIT_FAILURE = 1 +def human_bool(value: bool) -> str: +    """Formats a boolean for CLI output. + +    :param value: The value to format. +    :return: The string representing the bool. +    """ +    return "yes" if value else "no" + +  def setup(config_path: str) -> AppEnvironment:      """Sets up the logging and app environment for the scripts. @@ -201,6 +210,45 @@ def cms_user_passwd(          click.echo(f"Changed password of {user.name} ({user.email})") +@cmd_user.command("modify") +@config_option +@click.option("--admin/--no-admin", help="make the user an admin", default=None) +@click.option("--verified/--no-verified", help="set the user verification status", default=None) +@optgroup.group("User selection", cls=RequiredMutuallyExclusiveOptionGroup) +@optgroup.option("--id", "-i", "id_", help="database ID of the user", type=int) +@optgroup.option("--email", "-e", help="email of the user") +@click.pass_context +def cms_user_modify( +    ctx: click.Context, +    config: str, +    admin: Optional[bool], +    verified: Optional[bool], +    id_: Optional[int], +    email: Optional[str], +): +    """Modify a user.""" +    env = setup(config) +    if id_ is not None: +        query = select(models.User).filter_by(id=id_) +    else: +        query = models.User.query_by_email(email) +    with env["request"].tm: +        dbsession = env["request"].dbsession +        user = dbsession.execute(query).scalar_one_or_none() +        if user is None: +            click.echo("Error: No such user found.", err=True) +            ctx.exit(EXIT_FAILURE) + +        if admin is not None: +            user.is_admin = admin +        if verified is not None: +            user.is_verified = verified + +        click.echo(f"{user.name} - {user.email}") +        click.echo(f"Is admin: {human_bool(user.is_admin)}") +        click.echo(f"Is verified: {human_bool(user.is_verified)}") + +  @cmd_user.command("hittekaart")  @config_option  @click.option(  | 
