- tb_user 用户表、tb_log 日志表 - Flask-Login 认证(login/logout/权限装饰器) - 用户管理页(admin 专有):增删改查、改密、角色设置 - 操作日志页:分页查询、按用户/类型筛选 - 测试操作区指令自动记录日志 - 所有页面加 @login_required 保护 - 默认管理员 admin/admin123(首次启动自动创建)
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
"""Flask 应用工厂"""
|
|
|
|
from flask import Flask
|
|
from app.config import Config
|
|
|
|
|
|
def create_app() -> Flask:
|
|
app = Flask(__name__)
|
|
app.config.from_object(Config)
|
|
|
|
# 初始化认证
|
|
from app.auth import init_auth, auth_bp
|
|
init_auth(app)
|
|
app.register_blueprint(auth_bp)
|
|
|
|
# 注册蓝图
|
|
from app.routes.devices import bp as devices_bp
|
|
from app.routes.test_op import bp as test_op_bp
|
|
from app.routes.test_data import bp as test_data_bp
|
|
from app.routes.users import bp as users_bp
|
|
from app.routes.logs import bp as logs_bp
|
|
|
|
app.register_blueprint(devices_bp)
|
|
app.register_blueprint(test_op_bp)
|
|
app.register_blueprint(test_data_bp)
|
|
app.register_blueprint(users_bp)
|
|
app.register_blueprint(logs_bp)
|
|
|
|
# 初始化默认管理员
|
|
_ensure_admin()
|
|
|
|
return app
|
|
|
|
|
|
def _ensure_admin():
|
|
"""如果没有任何用户,创建默认 admin/admin123"""
|
|
from app.models import get_conn
|
|
from werkzeug.security import generate_password_hash
|
|
conn = get_conn()
|
|
try:
|
|
with conn.cursor() as cur:
|
|
cur.execute("SELECT COUNT(*) as cnt FROM tb_user")
|
|
if cur.fetchone()["cnt"] == 0:
|
|
cur.execute(
|
|
"INSERT INTO tb_user (username, password_hash, role) VALUES (%s,%s,%s)",
|
|
("admin", generate_password_hash("admin123"), "admin"),
|
|
)
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|