49 lines
No EOL
1.2 KiB
Python
49 lines
No EOL
1.2 KiB
Python
"""
|
|
CLI commands for the NetViz application.
|
|
"""
|
|
import click
|
|
from flask.cli import with_appcontext
|
|
from werkzeug.security import generate_password_hash
|
|
|
|
from app.extensions import db
|
|
from app.models.user import User
|
|
|
|
|
|
def register_commands(app):
|
|
"""
|
|
Register CLI commands with the Flask application.
|
|
|
|
Args:
|
|
app: The Flask application
|
|
"""
|
|
@app.cli.command("create-admin")
|
|
@click.argument("username")
|
|
@click.argument("email")
|
|
@click.password_option()
|
|
@with_appcontext
|
|
def create_admin(username, email, password):
|
|
"""Create an admin user."""
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
if user:
|
|
click.echo(f"User {username} already exists.")
|
|
return
|
|
|
|
user = User(
|
|
username=username,
|
|
email=email,
|
|
is_admin=True
|
|
)
|
|
user.set_password(password)
|
|
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
|
|
click.echo(f"Admin user {username} created successfully.")
|
|
|
|
@app.cli.command("init-db")
|
|
@with_appcontext
|
|
def init_db():
|
|
"""Initialize the database."""
|
|
db.create_all()
|
|
click.echo("Database initialized.") |