lots of changes

This commit is contained in:
2025-06-06 02:00:05 -05:00
parent 6cf2fdec61
commit 9daee50a3a
33 changed files with 1478 additions and 260 deletions

View File

@ -3,7 +3,9 @@
import os
import json
import glob
import importlib
import importlib.util
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
@ -11,11 +13,10 @@ from flask_login import LoginManager
from flask_wtf.csrf import CSRFProtect
from dotenv import load_dotenv
# Load environment variables from .env or system
load_dotenv()
# ----------------------------------------------------------------
# 1) Initialize core extensions
# ----------------------------------------------------------------
# ─── Initialize core extensions ─────────────────────────────────────────────────
db = SQLAlchemy()
migrate = Migrate()
login_manager = LoginManager()
@ -26,37 +27,43 @@ def create_app():
app = Flask(__name__)
app.config.from_object('app.config.Config')
# Initialize extensions with app
# ─── Initialize extensions with the app ───────────────────────────────────────
csrf.init_app(app)
db.init_app(app)
migrate.init_app(app, db)
login_manager.init_app(app)
login_manager.login_view = 'auth.login'
# ----------------------------------------------------------------
# 2) Register error handlers
# ----------------------------------------------------------------
# ─── Register user_loader for Flask-Login ───────────────────────────────────
from plugins.auth.models import User
@login_manager.user_loader
def load_user(user_id):
try:
return User.query.get(int(user_id))
except Exception:
return None
# ─── Register error handlers ─────────────────────────────────────────────────
from .errors import bp as errors_bp
app.register_blueprint(errors_bp)
# ----------------------------------------------------------------
# 3) Auto-load each plugins models.py so that SQLAlchemy metadata
# knows about every table (Plant, PlantOwnershipLog, PlantUpdate, etc.)
# ----------------------------------------------------------------
plugin_model_paths = glob.glob(os.path.join(os.path.dirname(__file__), '..', 'plugins', '*', 'models.py'))
# ─── 1) Autoimport plugin models by their package names ─────────────────────
# This ensures that every plugins/<plugin>/models.py is imported exactly once
plugin_model_paths = glob.glob(
os.path.join(os.path.dirname(__file__), '..', 'plugins', '*', 'models.py')
)
for path in plugin_model_paths:
module_name = path.replace("/", ".").replace(".py", "")
# path looks like ".../plugins/plant/models.py"
rel = path.split(os.sep)[-2] # e.g. "plant"
pkg = f"plugins.{rel}.models" # e.g. "plugins.plant.models"
try:
spec = importlib.util.spec_from_file_location(module_name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
print(f"✅ (Startup) Loaded: {module_name}")
importlib.import_module(pkg)
print(f"✅ (Startup) Loaded: {pkg}")
except Exception as e:
print(f"❌ (Startup) Failed to load {module_name}: {e}")
print(f"❌ (Startup) Failed to load {pkg}: {e}")
# ----------------------------------------------------------------
# 4) Auto-discover & register each plugins routes.py and CLI
# ----------------------------------------------------------------
# ─── 2) Autodiscover & register plugin routes, CLI, entrypoints ────────────
plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
for plugin in os.listdir(plugin_path):
if plugin.endswith('.noload'):
@ -67,7 +74,7 @@ def create_app():
if not os.path.isdir(plugin_dir):
continue
# --- (a) Register routes blueprint if present ---
# (a) Register routes.py
route_file = os.path.join(plugin_dir, 'routes.py')
if os.path.isfile(route_file):
try:
@ -80,7 +87,7 @@ def create_app():
except Exception as e:
print(f"❌ Failed to load routes from plugin '{plugin}': {e}")
# --- (b) Register CLI & entry point if present ---
# (b) Register CLI and entrypoint
init_file = os.path.join(plugin_dir, '__init__.py')
plugin_json = os.path.join(plugin_dir, 'plugin.json')
if os.path.isfile(init_file):
@ -99,6 +106,7 @@ def create_app():
except Exception as e:
print(f"❌ Failed to load CLI for plugin '{plugin}': {e}")
# ─── Inject current year into templates ────────────────────────────────────────
@app.context_processor
def inject_current_year():
from datetime import datetime