diff --git a/app/__init__.py b/app/__init__.py index 8ded18d..5a14aaa 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -147,10 +147,17 @@ def create_app(config_name=None): # ── Context processors ──────────────────────────────────────────────────── @app.context_processor def inject_globals(): + from flask import has_request_context from flask_login import current_user from app.models import SystemSetting unread = 0 - if current_user.is_authenticated: + # current_user is only resolvable inside an active request context. + # When render_template_string() is called from a background job (e.g. + # APScheduler email ingestion), there is no session to load from, so + # Flask-Login returns None — causing AttributeError on .is_authenticated. + # The has_request_context() guard makes the context processor safe for + # both web requests and background callers. + if has_request_context() and current_user.is_authenticated: from app.models import Notification unread = Notification.query.filter_by( user_id=current_user.id, is_read=False