37 lines
1.0 KiB
Python
37 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="创建管理员用户")
|
|
def create_user(username, email, password, admin):
|
|
"""创建新用户"""
|
|
if User.query.filter_by(email=email).first():
|
|
print(f"错误: 邮箱 {email} 已存在")
|
|
return
|
|
if User.query.filter_by(username=username).first():
|
|
print(f"错误: 用户名 {username} 已存在")
|
|
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"用户 {username} 创建成功")
|
|
if admin:
|
|
print("角色: 管理员")
|