2.3权限管理,基本盘完成,下一步修改设备管理弹窗设计,完善工程师日志写入设计
This commit is contained in:
62
2_3banben/init_db.py
Normal file
62
2_3banben/init_db.py
Normal file
@ -0,0 +1,62 @@
|
||||
import os
|
||||
from app import create_app
|
||||
from extensions import db
|
||||
from models import User, Device, UserDevicePermission
|
||||
|
||||
# 创建应用实例
|
||||
app = create_app()
|
||||
|
||||
|
||||
def init_db():
|
||||
with app.app_context():
|
||||
# ==========================================
|
||||
# ⚠️ 警告:这会清空现有的数据库表结构并重建
|
||||
# 如果只想更新 User 表,可以注释掉 db.drop_all(),
|
||||
# 但因为增加了字段,直接重建是最稳妥的。
|
||||
# ==========================================
|
||||
print("正在清理旧数据库...")
|
||||
db.drop_all()
|
||||
|
||||
print("正在创建新表结构...")
|
||||
db.create_all()
|
||||
|
||||
print("✅ 数据库表结构创建完成 (devices.db 和 users.db)")
|
||||
|
||||
# ==========================================
|
||||
# 🟢 1. 创建超级管理员 (Root)
|
||||
# 即使代码里有后门,数据库里有一个对应的实体也是最好的
|
||||
# ==========================================
|
||||
admin = User(username='admin', role='admin')
|
||||
admin.set_password('licahk') # 设置密码
|
||||
db.session.add(admin)
|
||||
print(f"👤 用户创建: [admin] (角色: 超级管理员)")
|
||||
|
||||
# ==========================================
|
||||
# 🟡 2. 创建一个测试工程师 (可选)
|
||||
# ==========================================
|
||||
engineer = User(username='engineer01', role='engineer')
|
||||
engineer.set_password('123456')
|
||||
db.session.add(engineer)
|
||||
print(f"👤 用户创建: [engineer01] (角色: 工程师)")
|
||||
|
||||
# ==========================================
|
||||
# ⚪ 3. 创建一个测试普通客户 (可选)
|
||||
# ==========================================
|
||||
client = User(username='client01', role='client')
|
||||
client.set_password('123456')
|
||||
db.session.add(client)
|
||||
print(f"👤 用户创建: [client01] (角色: 客户)")
|
||||
|
||||
# 提交更改
|
||||
db.session.commit()
|
||||
print("\n🚀 初始化完成!请运行 run.py 启动服务器。")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 再次确认防止误删
|
||||
print("此操作会删除现有的 'users.db' 和 'devices.db' 中的数据并重建。")
|
||||
confirm = input("确认继续吗? (y/n): ")
|
||||
if confirm.lower() == 'y':
|
||||
init_db()
|
||||
else:
|
||||
print("操作已取消。")
|
||||
Reference in New Issue
Block a user