113 lines
3.5 KiB
Python
113 lines
3.5 KiB
Python
# run.py
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
import click
|
|
from dotenv import load_dotenv
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent
|
|
DEFAULT_ENV_FILE = BASE_DIR / '.env'
|
|
|
|
|
|
def configure_env_file(argv):
|
|
args = list(argv)
|
|
env_file = DEFAULT_ENV_FILE
|
|
cleaned_args = []
|
|
index = 0
|
|
|
|
while index < len(args):
|
|
current_arg = args[index]
|
|
|
|
if current_arg in ('--env-file', '-e'):
|
|
if index + 1 >= len(args):
|
|
raise click.UsageError('选项 --env-file 需要提供文件路径。')
|
|
env_file = Path(args[index + 1])
|
|
index += 2
|
|
continue
|
|
|
|
if current_arg.startswith('--env-file='):
|
|
env_file = Path(current_arg.split('=', 1)[1])
|
|
index += 1
|
|
continue
|
|
|
|
cleaned_args.append(current_arg)
|
|
index += 1
|
|
|
|
if not env_file.is_absolute():
|
|
env_file = BASE_DIR / env_file
|
|
|
|
env_file = env_file.resolve()
|
|
|
|
if not env_file.is_file():
|
|
raise click.UsageError(f'未找到 env 文件:{env_file}')
|
|
|
|
os.environ['APP_ENV_FILE'] = str(env_file)
|
|
load_dotenv(env_file, override=True)
|
|
return cleaned_args, env_file
|
|
|
|
|
|
sys.argv[1:], SELECTED_ENV_FILE = configure_env_file(sys.argv[1:])
|
|
|
|
from app import create_app, db
|
|
from app.models import User, Role
|
|
|
|
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
|
|
|
|
@app.shell_context_processor
|
|
def make_shell_context():
|
|
return {'db': db, 'User': User, 'Role': Role}
|
|
|
|
# --- 从这里开始修改 ---
|
|
|
|
# 将原来的 create-admin 命令修改为更通用的 create-user
|
|
@app.cli.command("create-user")
|
|
@click.argument("username")
|
|
@click.argument("email")
|
|
@click.argument("password")
|
|
@click.option('--role', default='user', help='为用户设置角色 (可选值:admin, user)。默认为 "user"。')
|
|
def create_user(username, email, password, role):
|
|
"""
|
|
通过命令行创建一个新用户。
|
|
可以指定角色,默认为普通用户。
|
|
"""
|
|
if role not in ['user', 'admin']:
|
|
print(f"错误:无效的角色 '{role}'。角色必须是 'user' 或 'admin'。")
|
|
return
|
|
|
|
# 检查用户名或邮箱是否已存在
|
|
if User.query.filter((User.username == username) | (User.email == email)).first():
|
|
print(f"错误:用户名 '{username}' 或邮箱 '{email}' 已存在。")
|
|
return
|
|
|
|
user = User(username=username, email=email)
|
|
user.set_password(password)
|
|
|
|
# 如果角色是 admin,添加 Administrator 角色
|
|
if role == 'admin':
|
|
admin_role = Role.query.filter_by(name='Administrator').first()
|
|
if admin_role:
|
|
user.roles.append(admin_role)
|
|
else:
|
|
print("警告:Administrator 角色不存在,将创建该角色")
|
|
admin_role = Role(name='Administrator', description='System Administrator')
|
|
db.session.add(admin_role)
|
|
db.session.commit()
|
|
user.roles.append(admin_role)
|
|
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
print(f"角色为 '{role}' 的用户 '{username}' 创建成功。")
|
|
|
|
# --- 修改结束 ---
|
|
|
|
if __name__ == '__main__':
|
|
host = os.environ.get('SERVER_HOST', '127.0.0.1')
|
|
port = int(os.environ.get('SERVER_PORT', 5000))
|
|
app.run(host=host, port=port)
|
|
'''
|
|
# 格式:flask create-user <用户名> <邮箱> <密码>
|
|
flask create-user testuser test@example.com asecurepassword
|
|
# 格式:flask create-user <用户名> <邮箱> <密码> --role <角色>
|
|
flask create-user mainadmin admin@myproject.com anotherpassword --role admin
|
|
'''
|