feat: implement module-driven context providers for dynamic page context
Introduces a module-driven context provider system that allows modules to dynamically contribute template context variables without hardcoding imports. Key changes: - Add context_providers field to ModuleDefinition in app/modules/base.py - Create unified get_context_for_frontend() that queries enabled modules only - Add context providers to CMS module (PLATFORM, STOREFRONT) - Add context providers to billing module (PLATFORM) - Fix SQLAlchemy cross-module relationship resolution (Order, AdminMenuConfig, MarketplaceImportJob) by ensuring models are imported before referencing - Document the entire system in docs/architecture/module-system.md Benefits: - Zero coupling: adding/removing modules requires no route handler changes - Lazy loading: module code only imported when that module is enabled - Per-platform customization: each platform loads only what it needs - Graceful degradation: one failing module doesn't break entire page Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,9 +12,100 @@ This is a self-contained module with:
|
||||
- Templates: app.modules.cms.templates (namespaced as cms/)
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from app.modules.base import MenuItemDefinition, MenuSectionDefinition, ModuleDefinition, PermissionDefinition
|
||||
from app.modules.enums import FrontendType
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Context Providers
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def _get_platform_context(request: Any, db: Any, platform: Any) -> dict[str, Any]:
|
||||
"""
|
||||
Provide CMS context for platform/marketing pages.
|
||||
|
||||
Returns header and footer navigation pages for the marketing site.
|
||||
"""
|
||||
from app.modules.cms.services import content_page_service
|
||||
|
||||
platform_id = platform.id if platform else 1
|
||||
|
||||
header_pages = []
|
||||
footer_pages = []
|
||||
|
||||
try:
|
||||
header_pages = content_page_service.list_platform_pages(
|
||||
db, platform_id=platform_id, header_only=True, include_unpublished=False
|
||||
)
|
||||
footer_pages = content_page_service.list_platform_pages(
|
||||
db, platform_id=platform_id, footer_only=True, include_unpublished=False
|
||||
)
|
||||
logger.debug(
|
||||
f"[CMS] Platform context: {len(header_pages)} header, {len(footer_pages)} footer pages"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"[CMS] Failed to load platform navigation pages: {e}")
|
||||
|
||||
return {
|
||||
"header_pages": header_pages,
|
||||
"footer_pages": footer_pages,
|
||||
"legal_pages": [], # TODO: Add legal pages support if needed
|
||||
}
|
||||
|
||||
|
||||
def _get_storefront_context(request: Any, db: Any, platform: Any) -> dict[str, Any]:
|
||||
"""
|
||||
Provide CMS context for storefront (customer shop) pages.
|
||||
|
||||
Returns header and footer navigation pages for the vendor's shop.
|
||||
"""
|
||||
from app.modules.cms.services import content_page_service
|
||||
|
||||
vendor = getattr(request.state, "vendor", None)
|
||||
platform_id = platform.id if platform else 1
|
||||
|
||||
header_pages = []
|
||||
footer_pages = []
|
||||
|
||||
if vendor:
|
||||
try:
|
||||
header_pages = content_page_service.list_pages_for_vendor(
|
||||
db,
|
||||
platform_id=platform_id,
|
||||
vendor_id=vendor.id,
|
||||
header_only=True,
|
||||
include_unpublished=False,
|
||||
)
|
||||
footer_pages = content_page_service.list_pages_for_vendor(
|
||||
db,
|
||||
platform_id=platform_id,
|
||||
vendor_id=vendor.id,
|
||||
footer_only=True,
|
||||
include_unpublished=False,
|
||||
)
|
||||
logger.debug(
|
||||
f"[CMS] Storefront context for vendor {vendor.id}: "
|
||||
f"{len(header_pages)} header, {len(footer_pages)} footer pages"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"[CMS] Failed to load storefront navigation pages: {e}")
|
||||
|
||||
return {
|
||||
"header_pages": header_pages,
|
||||
"footer_pages": footer_pages,
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Router Lazy Imports
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def _get_admin_router():
|
||||
"""Lazy import of admin router to avoid circular imports."""
|
||||
@@ -139,6 +230,11 @@ cms_module = ModuleDefinition(
|
||||
],
|
||||
},
|
||||
is_core=True, # CMS is a core module - content management is fundamental
|
||||
# Context providers for dynamic page context
|
||||
context_providers={
|
||||
FrontendType.PLATFORM: _get_platform_context,
|
||||
FrontendType.STOREFRONT: _get_storefront_context,
|
||||
},
|
||||
# Self-contained module configuration
|
||||
is_self_contained=True,
|
||||
services_path="app.modules.cms.services",
|
||||
|
||||
Reference in New Issue
Block a user