Files
KCGL/inventory-backend/app/__init__.py
2026-02-02 15:06:20 +08:00

80 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 文件路径: inventory-backend/app/__init__.py
from flask import Flask
from config import Config
from app.extensions import db, migrate, cors
def create_app():
app = Flask(__name__)
app.config.from_object(Config)
# 1. 初始化插件
db.init_app(app)
migrate.init_app(app, db)
# 确保跨域配置
cors.init_app(app, resources={r"/api/*": {"origins": "*"}})
# =========================================================
# 2. 注册蓝图 (Blueprints)
# =========================================================
# -----------------------------------------------------
# 2.1 注册入库聚合模块 (Inbound)
# -----------------------------------------------------
try:
# 指向聚合文件: app/api/v1/inbound/__init__.py
# 该文件里应该包含了 buy, semi, base, product 的聚合逻辑
from app.api.v1.inbound import inbound_bp
# 注册父蓝图,路由前缀为 /api/v1/inbound
# 最终路由效果:
# /api/v1/inbound + /buy/list -> /api/v1/inbound/buy/list
app.register_blueprint(inbound_bp, url_prefix='/api/v1/inbound')
print("✅ Inbound (Buy, Semi, Product, Base) 模块注册成功")
except ImportError as e:
print(f"❌ 错误: Inbound 模块导入失败: {e}")
# -----------------------------------------------------
# 2.2 注册通用打印模块 (Common Print) - [新增]
# -----------------------------------------------------
try:
from app.api.v1.common.print import print_bp
# 注册打印蓝图
# 前端请求地址: /common/print/preview
# 配合 baseURL=/api/v1最终对应后端: /api/v1/common/print/preview
app.register_blueprint(print_bp, url_prefix='/api/v1/common/print')
print("✅ Print (Label Printing) 模块注册成功")
except ImportError as e:
print(f"❌ 错误: Print 模块导入失败: {e}")
# =========================================================
# 3. 预加载数据模型 (解决 relationship 找不到模型的问题)
# =========================================================
with app.app_context():
try:
# 1. 基础物料
from app.models.base import MaterialBase
# 2. 采购入库
from app.models.inbound.buy import StockBuy
# 3. 半成品入库
from app.models.inbound.semi import StockSemi
# 4. 成品入库
from app.models.inbound.product import StockProduct
# 开发环境如果需要自动建表,可以取消注释
# db.create_all()
except ImportError as e:
# 建议打印错误,防止因为文件名拼写错误导致静默失败
print(f"⚠️ 模型预加载失败: {e}")
except Exception as e:
print(f"⚠️ 模型预加载发生未知错误: {e}")
return app