You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.7 KiB
55 lines
1.7 KiB
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from flask import Flask, g
|
|
|
|
from app.auth import load_current_account
|
|
from app.cli import register_commands
|
|
from app.config import CONFIG_MAPPING
|
|
from app.extensions import db, migrate
|
|
from app.routes.auth import auth_bp
|
|
from app.routes.admin import admin_bp
|
|
from app.routes.quick_entry import quick_entry_bp
|
|
from app.routes.health import health_bp
|
|
from app.routes.main import main_bp
|
|
from app.routes.csv import csv_bp
|
|
from app.routes.share import share_bp
|
|
from app.services import household_value_label
|
|
|
|
|
|
def create_app(config_name: str | None = None) -> Flask:
|
|
app = Flask(__name__, instance_relative_config=True)
|
|
|
|
resolved_config_name = config_name or os.getenv("HW_CONFIG", "development")
|
|
config_object = CONFIG_MAPPING.get(resolved_config_name, CONFIG_MAPPING["development"])
|
|
app.config.from_object(config_object)
|
|
|
|
validate_config = getattr(config_object, "validate", None)
|
|
if callable(validate_config):
|
|
validate_config()
|
|
|
|
Path(app.instance_path).mkdir(parents=True, exist_ok=True)
|
|
|
|
db.init_app(app)
|
|
migrate.init_app(app, db)
|
|
app.register_blueprint(auth_bp)
|
|
app.register_blueprint(admin_bp)
|
|
app.register_blueprint(main_bp)
|
|
app.register_blueprint(quick_entry_bp)
|
|
app.register_blueprint(csv_bp)
|
|
app.register_blueprint(share_bp)
|
|
app.register_blueprint(health_bp)
|
|
register_commands(app)
|
|
app.before_request(load_current_account)
|
|
|
|
def inject_template_context() -> dict[str, object]:
|
|
return {
|
|
"current_account": getattr(g, "current_account", None),
|
|
"household_value_label": household_value_label,
|
|
}
|
|
|
|
app.context_processor(inject_template_context)
|
|
|
|
return app
|
|
|