CLI - add users create command

This commit is contained in:
Sam 2023-04-12 09:57:41 +02:00
parent b04bcd0447
commit ecdb28ad2b

View File

@ -4,6 +4,7 @@ from typing import Optional
import click import click
from humanize import naturalsize from humanize import naturalsize
from fittrackee import db
from fittrackee.cli.app import app from fittrackee.cli.app import app
from fittrackee.users.exceptions import UserNotFoundException from fittrackee.users.exceptions import UserNotFoundException
from fittrackee.users.export_data import ( from fittrackee.users.export_data import (
@ -25,6 +26,32 @@ def users_cli() -> None:
pass pass
@users_cli.command('create')
@click.argument('username')
@click.option('--email', type=str, required=True, help='User email.')
@click.option(
'--password',
type=str,
help='User password. If not provided, a random password is generated.',
)
def create_user(username: str, email: str, password: Optional[str]) -> None:
"""Create an active user account."""
with app.app_context():
try:
user_manager_service = UserManagerService(username)
user, user_password = user_manager_service.create_user(
email=email, password=password
)
db.session.add(user)
db.session.commit()
user_manager_service.update(activate=True)
click.echo(f"User '{username}' created.")
if not password:
click.echo(f"The user password is: {user_password}")
except Exception as e:
click.echo(f'Error(s) occurred:\n{e}', err=True)
@users_cli.command('update') @users_cli.command('update')
@click.argument('username') @click.argument('username')
@click.option( @click.option(