a ton of fun happened, refactored alot
This commit is contained in:
195
app/__init__.py
195
app/__init__.py
@ -1,120 +1,145 @@
|
||||
# app/__init__.py
|
||||
# File: app/__init__.py
|
||||
|
||||
import os
|
||||
import json
|
||||
import glob
|
||||
import importlib
|
||||
import importlib.util
|
||||
import time
|
||||
|
||||
from flask import Flask,request
|
||||
from flask import Flask, request
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_migrate import Migrate
|
||||
from flask_login import LoginManager
|
||||
from flask_wtf.csrf import CSRFProtect
|
||||
from dotenv import load_dotenv
|
||||
from datetime import datetime
|
||||
from dotenv import load_dotenv, find_dotenv
|
||||
|
||||
# ─── Load .env ────────────────────────────────────────────────────────────────
|
||||
dotenv_path = find_dotenv()
|
||||
if dotenv_path:
|
||||
load_dotenv(dotenv_path, override=True)
|
||||
|
||||
# Load environment variables from .env or system
|
||||
load_dotenv()
|
||||
|
||||
# ─── Initialize core extensions ─────────────────────────────────────────────────
|
||||
# ─── Core extensions ───────────────────────────────────────────────────────────
|
||||
db = SQLAlchemy()
|
||||
migrate = Migrate()
|
||||
login_manager = LoginManager()
|
||||
csrf = CSRFProtect()
|
||||
|
||||
from plugins.media.routes import generate_image_url # Import it here
|
||||
|
||||
# ─── Template helper (still in core) ──────────────────────────────────────────
|
||||
from plugins.media.routes import generate_image_url # noqa: E402
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
# ─── Configure Flask ────────────────────────────────────────────────────────
|
||||
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
||||
app = Flask(
|
||||
__name__,
|
||||
static_folder=os.path.join(project_root, 'static'),
|
||||
static_url_path='/static'
|
||||
)
|
||||
app.config.from_object('app.config.Config')
|
||||
|
||||
# ─── Initialize extensions with the app ───────────────────────────────────────
|
||||
# ─── Init extensions ───────────────────────────────────────────────────────
|
||||
csrf.init_app(app)
|
||||
db.init_app(app)
|
||||
migrate.init_app(app, db)
|
||||
login_manager.init_app(app)
|
||||
login_manager.login_view = 'auth.login'
|
||||
|
||||
# ─── 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
|
||||
# ─── Core routes & errors ───────────────────────────────────────────────────
|
||||
from .errors import bp as errors_bp # noqa: E402
|
||||
app.register_blueprint(errors_bp)
|
||||
from .routes import init_app as register_core_routes # noqa: E402
|
||||
register_core_routes(app)
|
||||
app.logger.info("✔️ Registered core routes")
|
||||
|
||||
# ─── 1) Auto‐import 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:
|
||||
# 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"
|
||||
# ─── JSON‐driven plugin loader ──────────────────────────────────────────────
|
||||
plugins_dir = os.path.join(project_root, 'plugins')
|
||||
for name in sorted(os.listdir(plugins_dir)):
|
||||
plugin_path = os.path.join(plugins_dir, name)
|
||||
manifest = os.path.join(plugin_path, 'plugin.json')
|
||||
if not os.path.isfile(manifest):
|
||||
continue
|
||||
|
||||
errors = []
|
||||
try:
|
||||
importlib.import_module(pkg)
|
||||
print(f"✅ (Startup) Loaded: {pkg}")
|
||||
meta = json.load(open(manifest))
|
||||
except Exception as e:
|
||||
print(f"❌ (Startup) Failed to load {pkg}: {e}")
|
||||
|
||||
# ─── 2) Auto‐discover & register plugin routes, CLI, entry‐points ────────────
|
||||
plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
|
||||
for plugin in os.listdir(plugin_path):
|
||||
if plugin.endswith('.noload'):
|
||||
print(f"[⏭] Skipping plugin '{plugin}' (marked as .noload)")
|
||||
print(f"Plugin '{name}' 🛑 manifest load failed: {e}")
|
||||
continue
|
||||
|
||||
plugin_dir = os.path.join(plugin_path, plugin)
|
||||
if not os.path.isdir(plugin_dir):
|
||||
continue
|
||||
|
||||
# (a) Register routes.py
|
||||
route_file = os.path.join(plugin_dir, 'routes.py')
|
||||
if os.path.isfile(route_file):
|
||||
# 1) Import models
|
||||
for model_path in meta.get('models', []):
|
||||
try:
|
||||
spec = importlib.util.spec_from_file_location(f"plugins.{plugin}.routes", route_file)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
if hasattr(mod, 'bp'):
|
||||
app.register_blueprint(mod.bp, strict_slashes=False)
|
||||
print(f"✔️ Registered routes for plugin '{plugin}'")
|
||||
importlib.import_module(model_path)
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to load routes from plugin '{plugin}': {e}")
|
||||
errors.append(f"model import ({model_path}): {e}")
|
||||
|
||||
# (b) Register CLI and entry‐point
|
||||
init_file = os.path.join(plugin_dir, '__init__.py')
|
||||
plugin_json = os.path.join(plugin_dir, 'plugin.json')
|
||||
if os.path.isfile(init_file):
|
||||
# 1.b) user_loader hook
|
||||
ul = meta.get('user_loader')
|
||||
if ul:
|
||||
try:
|
||||
cli_module = importlib.import_module(f"plugins.{plugin}")
|
||||
if hasattr(cli_module, 'register_cli'):
|
||||
cli_module.register_cli(app)
|
||||
print(f"✔️ Registered CLI for plugin '{plugin}'")
|
||||
if os.path.isfile(plugin_json):
|
||||
with open(plugin_json, 'r') as f:
|
||||
meta = json.load(f)
|
||||
entry = meta.get('entry_point')
|
||||
if entry and hasattr(cli_module, entry):
|
||||
getattr(cli_module, entry)(app)
|
||||
print(f"✔️ Ran entry point '{entry}' for plugin '{plugin}'")
|
||||
mod = importlib.import_module(ul['module'])
|
||||
fn = getattr(mod, ul['callable'])
|
||||
fn(app)
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to load CLI for plugin '{plugin}': {e}")
|
||||
errors.append(f"user_loader ({ul['module']}:{ul['callable']}): {e}")
|
||||
|
||||
# ─── Inject current year into templates ────────────────────────────────────────
|
||||
# 2) Register routes
|
||||
routes_cfg = meta.get('routes')
|
||||
if routes_cfg:
|
||||
try:
|
||||
mod = importlib.import_module(routes_cfg['module'])
|
||||
bp_obj = getattr(mod, routes_cfg['blueprint'])
|
||||
prefix = routes_cfg.get('url_prefix')
|
||||
app.register_blueprint(bp_obj, url_prefix=prefix, strict_slashes=False)
|
||||
except Exception as e:
|
||||
errors.append(f"routes ({routes_cfg['module']}): {e}")
|
||||
|
||||
# 3) Register CLI commands
|
||||
cli_cfg = meta.get('cli')
|
||||
if cli_cfg:
|
||||
try:
|
||||
mod = importlib.import_module(cli_cfg['module'])
|
||||
fn = getattr(mod, cli_cfg['callable'])
|
||||
app.cli.add_command(fn)
|
||||
except Exception as e:
|
||||
errors.append(f"cli ({cli_cfg['module']}:{cli_cfg['callable']}): {e}")
|
||||
|
||||
# 4) Template globals
|
||||
for tg in meta.get('template_globals', []):
|
||||
try:
|
||||
mod_name, fn_name = tg['callable'].rsplit('.', 1)
|
||||
mod = importlib.import_module(mod_name)
|
||||
fn = getattr(mod, fn_name)
|
||||
app.jinja_env.globals[tg['name']] = fn
|
||||
except Exception as e:
|
||||
errors.append(f"template_global ({tg}): {e}")
|
||||
|
||||
# 5) Subplugins (models + routes)
|
||||
for sp in meta.get('subplugins', []):
|
||||
for mp in sp.get('models', []):
|
||||
try:
|
||||
importlib.import_module(mp)
|
||||
except Exception as e:
|
||||
errors.append(f"subplugin model ({mp}): {e}")
|
||||
sp_rt = sp.get('routes')
|
||||
if sp_rt:
|
||||
try:
|
||||
mod = importlib.import_module(sp_rt['module'])
|
||||
bp_obj = getattr(mod, sp_rt['blueprint'])
|
||||
prefix = sp_rt.get('url_prefix')
|
||||
app.register_blueprint(bp_obj, url_prefix=prefix, strict_slashes=False)
|
||||
except Exception as e:
|
||||
errors.append(f"subplugin routes ({sp_rt['module']}): {e}")
|
||||
|
||||
# Final status
|
||||
if errors:
|
||||
print(f"Plugin '{name}' 🛑 failed to load: {'; '.join(errors)}")
|
||||
else:
|
||||
print(f"Plugin '{name}' ✔️ Loaded Successfully.")
|
||||
|
||||
# ─── Context processors, analytics, teardown ───────────────────────────────
|
||||
@app.context_processor
|
||||
def inject_current_year():
|
||||
from datetime import datetime
|
||||
return {'current_year': datetime.now().year}
|
||||
|
||||
@app.context_processor
|
||||
@ -127,17 +152,16 @@ def create_app():
|
||||
|
||||
@app.after_request
|
||||
def log_analytics(response):
|
||||
# import here to avoid circular at module‐load time
|
||||
from plugins.admin.models import AnalyticsEvent
|
||||
from plugins.admin.models import AnalyticsEvent # noqa: E402
|
||||
try:
|
||||
duration = time.time() - getattr(request, '_start_time', time.time())
|
||||
ev = AnalyticsEvent(
|
||||
method=request.method,
|
||||
path=request.path,
|
||||
status_code=response.status_code,
|
||||
response_time=duration,
|
||||
user_agent=request.headers.get('User-Agent'),
|
||||
referer=request.headers.get('Referer'),
|
||||
method = request.method,
|
||||
path = request.path,
|
||||
status_code = response.status_code,
|
||||
response_time = duration,
|
||||
user_agent = request.headers.get('User-Agent'),
|
||||
referer = request.headers.get('Referer'),
|
||||
accept_language=request.headers.get('Accept-Language'),
|
||||
)
|
||||
db.session.add(ev)
|
||||
@ -146,6 +170,11 @@ def create_app():
|
||||
db.session.rollback()
|
||||
return response
|
||||
|
||||
@app.teardown_appcontext
|
||||
def shutdown_session(exception=None):
|
||||
db.session.remove()
|
||||
|
||||
# ─── Keep the template helper exposed ──────────────────────────────────────
|
||||
app.jinja_env.globals['generate_image_url'] = generate_image_url
|
||||
|
||||
return app
|
||||
|
Reference in New Issue
Block a user