52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
|
|
import os
|
||
|
|
import sys
|
||
|
|
|
||
|
|
# 添加项目根目录到Python路径
|
||
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
|
|
||
|
|
# 设置数据库URL为SQLite
|
||
|
|
os.environ['DATABASE_URL'] = 'sqlite:///kamaxitong.db'
|
||
|
|
|
||
|
|
from app import create_app, db
|
||
|
|
from app.models.admin import Admin
|
||
|
|
from werkzeug.security import generate_password_hash
|
||
|
|
|
||
|
|
# 创建应用实例
|
||
|
|
app = create_app()
|
||
|
|
|
||
|
|
with app.app_context():
|
||
|
|
# 检查是否已有管理员数据
|
||
|
|
admin_count = Admin.query.count()
|
||
|
|
if admin_count > 0:
|
||
|
|
print(f"Database already contains {admin_count} admin users. Skipping data insertion.")
|
||
|
|
sys.exit(0)
|
||
|
|
|
||
|
|
# 创建默认管理员账号
|
||
|
|
print("Creating default admin users...")
|
||
|
|
|
||
|
|
# 超级管理员
|
||
|
|
admin = Admin(
|
||
|
|
username='admin',
|
||
|
|
email='admin@kamaxitong.com',
|
||
|
|
role=1, # 超级管理员
|
||
|
|
status=1 # 正常
|
||
|
|
)
|
||
|
|
admin.set_password('admin123')
|
||
|
|
db.session.add(admin)
|
||
|
|
|
||
|
|
# 普通管理员
|
||
|
|
test_admin = Admin(
|
||
|
|
username='test_admin',
|
||
|
|
email='test@kamaxitong.com',
|
||
|
|
role=0, # 普通管理员
|
||
|
|
status=1 # 正常
|
||
|
|
)
|
||
|
|
test_admin.set_password('test123')
|
||
|
|
db.session.add(test_admin)
|
||
|
|
|
||
|
|
# 提交更改
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
print("✓ Created admin users:")
|
||
|
|
print(" - Super admin: admin / admin123")
|
||
|
|
print(" - Normal admin: test_admin / test123")
|