import click from flask.cli import with_appcontext from ..extensions import db from ..models import User, seed_categories from ..services.reporting import send_due_reports @click.command('create-user') @click.option('--email', prompt=True) @click.option('--name', prompt=True) @click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True) @click.option('--admin', is_flag=True, default=False) @with_appcontext def create_user(email, name, password, admin): user = User(email=email.lower(), full_name=name, role='admin' if admin else 'user', must_change_password=False) user.set_password(password) db.session.add(user) db.session.commit() click.echo(f'Created user {email}') @click.command('reset-password') @click.option('--email', prompt=True) @click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True) @with_appcontext def reset_password(email, password): user = User.query.filter_by(email=email.lower()).first() if not user: raise click.ClickException('User not found') user.set_password(password) user.must_change_password = True db.session.commit() click.echo(f'Password reset for {email}') @click.command('make-admin') @click.option('--email', prompt=True) @with_appcontext def make_admin(email): user = User.query.filter_by(email=email.lower()).first() if not user: raise click.ClickException('User not found') user.role = 'admin' db.session.commit() click.echo(f'Granted admin to {email}') @click.command('deactivate-user') @click.option('--email', prompt=True) @with_appcontext def deactivate_user(email): user = User.query.filter_by(email=email.lower()).first() if not user: raise click.ClickException('User not found') user.is_active_user = False db.session.commit() click.echo(f'Deactivated {email}') @click.command('send-reports') @with_appcontext def send_reports_command(): count = send_due_reports() click.echo(f'Sent {count} reports') @click.command('seed-categories') @with_appcontext def seed_categories_command(): seed_categories() click.echo('Categories seeded') def register_commands(app): app.cli.add_command(create_user) app.cli.add_command(reset_password) app.cli.add_command(make_admin) app.cli.add_command(deactivate_user) app.cli.add_command(send_reports_command) app.cli.add_command(seed_categories_command)