Files
012-kaopeilian/backend/create_user_simple.py
111 998211c483 feat: 初始化考培练系统项目
- 从服务器拉取完整代码
- 按框架规范整理项目结构
- 配置 Drone CI 测试环境部署
- 包含后端(FastAPI)、前端(Vue3)、管理端

技术栈: Vue3 + TypeScript + FastAPI + MySQL
2026-01-24 19:33:28 +08:00

115 lines
4.2 KiB
Python

#!/usr/bin/env python3
"""
简单创建用户脚本
"""
import asyncio
import sys
import os
# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# 加载本地配置
try:
import local_config
print("✅ 本地配置已加载")
except ImportError:
print("⚠️ 未找到local_config.py")
async def create_user_simple():
"""简单创建用户"""
try:
from app.core.database import AsyncSessionLocal
from app.core.security import get_password_hash
from sqlalchemy import text
print("🔧 创建测试用户...")
async with AsyncSessionLocal() as session:
# 创建一个测试用户
test_username = "test_dify"
test_password = "password123"
hashed_password = get_password_hash(test_password)
# 检查用户是否存在
result = await session.execute(
text("SELECT id FROM users WHERE username = :username"),
{"username": test_username}
)
existing = result.fetchone()
if existing:
print(f"⚠️ 用户 '{test_username}' 已存在")
else:
# 插入新用户
await session.execute(
text("""
INSERT INTO users (username, email, password_hash, role, is_active, full_name, created_at, updated_at)
VALUES (:username, :email, :password_hash, :role, :is_active, :full_name, NOW(), NOW())
"""),
{
"username": test_username,
"email": "test_dify@example.com",
"password_hash": hashed_password,
"role": "trainee",
"is_active": 1,
"full_name": "Test Dify User"
}
)
await session.commit()
print(f"\n✅ 用户创建成功:")
print(f" 用户名: {test_username}")
print(f" 密码: {test_password}")
print(f" 角色: trainee")
print(f" 邮箱: test_dify@example.com")
# 创建管理员用户
admin_username = "admin"
admin_password = "admin123"
admin_hashed_password = get_password_hash(admin_password)
# 检查管理员是否存在
result = await session.execute(
text("SELECT id FROM users WHERE username = :username"),
{"username": admin_username}
)
existing = result.fetchone()
if existing:
print(f"\n⚠️ 用户 '{admin_username}' 已存在")
else:
# 插入管理员
await session.execute(
text("""
INSERT INTO users (username, email, password_hash, role, is_active, full_name, is_superuser, created_at, updated_at)
VALUES (:username, :email, :password_hash, :role, :is_active, :full_name, :is_superuser, NOW(), NOW())
"""),
{
"username": admin_username,
"email": "admin@kaopeilian.com",
"password_hash": admin_hashed_password,
"role": "admin",
"is_active": 1,
"full_name": "系统管理员",
"is_superuser": 1
}
)
await session.commit()
print(f"\n✅ 管理员创建成功:")
print(f" 用户名: {admin_username}")
print(f" 密码: {admin_password}")
print(f" 角色: admin")
print(f" 邮箱: admin@kaopeilian.com")
print("\n✅ 用户创建完成!")
except Exception as e:
print(f"❌ 创建用户失败: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(create_user_simple())