36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
import os
|
|
import click
|
|
from app import create_app, db
|
|
from app.models import User
|
|
|
|
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
|
|
|
|
@app.shell_context_processor
|
|
def make_shell_context():
|
|
return dict(db=db, User=User)
|
|
|
|
@app.cli.command("create-user")
|
|
@click.argument("username")
|
|
@click.argument("email")
|
|
@click.argument("password")
|
|
@click.option("--admin", is_flag=True, help="Flag to create an admin user.")
|
|
def create_user(username, email, password, admin):
|
|
"""Creates a new user."""
|
|
if User.query.filter_by(email=email).first():
|
|
print(f"Error: Email {email} already exists.")
|
|
return
|
|
if User.query.filter_by(username=username).first():
|
|
print(f"Error: Username {username} already exists.")
|
|
return
|
|
|
|
user = User(username=username, email=email, status='active')
|
|
user.set_password(password)
|
|
if admin:
|
|
user.role = 'admin'
|
|
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
print(f"User {username} created successfully.")
|
|
if admin:
|
|
print("Role: Admin")
|