refactor: migrate templates and static files to self-contained modules
Templates Migration: - Migrate admin templates to modules (tenancy, billing, monitoring, marketplace, etc.) - Migrate vendor templates to modules (tenancy, billing, orders, messaging, etc.) - Migrate storefront templates to modules (catalog, customers, orders, cart, checkout, cms) - Migrate public templates to modules (billing, marketplace, cms) - Keep shared templates in app/templates/ (base.html, errors/, partials/, macros/) - Migrate letzshop partials to marketplace module Static Files Migration: - Migrate admin JS to modules: tenancy (23 files), core (5 files), monitoring (1 file) - Migrate vendor JS to modules: tenancy (4 files), core (2 files) - Migrate shared JS: vendor-selector.js to core, media-picker.js to cms - Migrate storefront JS: storefront-layout.js to core - Keep framework JS in static/ (api-client, utils, money, icons, log-config, lib/) - Update all template references to use module_static paths Naming Consistency: - Rename static/platform/ to static/public/ - Rename app/templates/platform/ to app/templates/public/ - Update all extends and static references Documentation: - Update module-system.md with shared templates documentation - Update frontend-structure.md with new module JS organization Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
241
app/modules/cms/routes/pages/public.py
Normal file
241
app/modules/cms/routes/pages/public.py
Normal file
@@ -0,0 +1,241 @@
|
||||
# app/modules/cms/routes/pages/public.py
|
||||
"""
|
||||
CMS Public Page Routes (HTML rendering).
|
||||
|
||||
Public (unauthenticated) pages for platform content:
|
||||
- Homepage
|
||||
- Generic content pages (/{slug} catch-all)
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.modules.billing.models import TIER_LIMITS, TierCode
|
||||
from app.modules.cms.services import content_page_service
|
||||
from app.modules.core.utils.page_context import get_public_context
|
||||
from app.templates_config import templates
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Route configuration - high priority so catch-all is registered last
|
||||
ROUTE_CONFIG = {
|
||||
"priority": 100,
|
||||
}
|
||||
|
||||
|
||||
def _get_tiers_data() -> list[dict]:
|
||||
"""Build tier data for display in templates."""
|
||||
tiers = []
|
||||
for tier_code, limits in TIER_LIMITS.items():
|
||||
tiers.append(
|
||||
{
|
||||
"code": tier_code.value,
|
||||
"name": limits["name"],
|
||||
"price_monthly": limits["price_monthly_cents"] / 100,
|
||||
"price_annual": (limits["price_annual_cents"] / 100)
|
||||
if limits.get("price_annual_cents")
|
||||
else None,
|
||||
"orders_per_month": limits.get("orders_per_month"),
|
||||
"products_limit": limits.get("products_limit"),
|
||||
"team_members": limits.get("team_members"),
|
||||
"features": limits.get("features", []),
|
||||
"is_popular": tier_code == TierCode.PROFESSIONAL,
|
||||
"is_enterprise": tier_code == TierCode.ENTERPRISE,
|
||||
}
|
||||
)
|
||||
return tiers
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# HOMEPAGE
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@router.get("/", response_class=HTMLResponse, name="platform_homepage")
|
||||
async def homepage(
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Homepage handler.
|
||||
|
||||
Handles two scenarios:
|
||||
1. Vendor on custom domain (vendor.com) -> Show vendor landing page or redirect to shop
|
||||
2. Platform marketing site -> Show platform homepage from CMS or default template
|
||||
|
||||
URL routing:
|
||||
- localhost:9999/ -> Main marketing site ('main' platform)
|
||||
- localhost:9999/platforms/oms/ -> OMS platform (middleware rewrites to /)
|
||||
- oms.lu/ -> OMS platform (domain-based)
|
||||
- shop.mycompany.com/ -> Vendor landing page (custom domain)
|
||||
"""
|
||||
# Get platform and vendor from middleware
|
||||
platform = getattr(request.state, "platform", None)
|
||||
vendor = getattr(request.state, "vendor", None)
|
||||
|
||||
# Scenario 1: Vendor detected (custom domain like vendor.com)
|
||||
if vendor:
|
||||
logger.debug(f"[HOMEPAGE] Vendor detected: {vendor.subdomain}")
|
||||
|
||||
# Get platform_id (use platform from context or default to 1 for OMS)
|
||||
platform_id = platform.id if platform else 1
|
||||
|
||||
# Try to find vendor landing page (slug='landing' or 'home')
|
||||
landing_page = content_page_service.get_page_for_vendor(
|
||||
db,
|
||||
platform_id=platform_id,
|
||||
slug="landing",
|
||||
vendor_id=vendor.id,
|
||||
include_unpublished=False,
|
||||
)
|
||||
|
||||
if not landing_page:
|
||||
landing_page = content_page_service.get_page_for_vendor(
|
||||
db,
|
||||
platform_id=platform_id,
|
||||
slug="home",
|
||||
vendor_id=vendor.id,
|
||||
include_unpublished=False,
|
||||
)
|
||||
|
||||
if landing_page:
|
||||
# Render landing page with selected template
|
||||
from app.modules.core.utils.page_context import get_storefront_context
|
||||
|
||||
template_name = landing_page.template or "default"
|
||||
template_path = f"cms/storefront/landing-{template_name}.html"
|
||||
|
||||
logger.info(f"[HOMEPAGE] Rendering vendor landing page: {template_path}")
|
||||
return templates.TemplateResponse(
|
||||
template_path,
|
||||
get_storefront_context(request, db=db, page=landing_page),
|
||||
)
|
||||
|
||||
# No landing page - redirect to shop
|
||||
vendor_context = getattr(request.state, "vendor_context", None)
|
||||
access_method = (
|
||||
vendor_context.get("detection_method", "unknown")
|
||||
if vendor_context
|
||||
else "unknown"
|
||||
)
|
||||
|
||||
if access_method == "path":
|
||||
full_prefix = (
|
||||
vendor_context.get("full_prefix", "/vendor/")
|
||||
if vendor_context
|
||||
else "/vendor/"
|
||||
)
|
||||
return RedirectResponse(
|
||||
url=f"{full_prefix}{vendor.subdomain}/storefront/", status_code=302
|
||||
)
|
||||
# Domain/subdomain - redirect to /storefront/
|
||||
return RedirectResponse(url="/storefront/", status_code=302)
|
||||
|
||||
# Scenario 2: Platform marketing site (no vendor)
|
||||
# Load platform homepage from CMS (slug='home')
|
||||
platform_id = platform.id if platform else 1
|
||||
|
||||
cms_homepage = content_page_service.get_platform_page(
|
||||
db, platform_id=platform_id, slug="home", include_unpublished=False
|
||||
)
|
||||
|
||||
if cms_homepage:
|
||||
# Use CMS-based homepage with template selection
|
||||
context = get_public_context(request, db)
|
||||
context["page"] = cms_homepage
|
||||
context["tiers"] = _get_tiers_data()
|
||||
|
||||
template_name = cms_homepage.template or "default"
|
||||
template_path = f"cms/public/homepage-{template_name}.html"
|
||||
|
||||
logger.info(f"[HOMEPAGE] Rendering CMS homepage with template: {template_path}")
|
||||
return templates.TemplateResponse(template_path, context)
|
||||
|
||||
# Fallback: Default wizamart homepage (no CMS content)
|
||||
logger.info("[HOMEPAGE] No CMS homepage found, using default wizamart template")
|
||||
context = get_public_context(request, db)
|
||||
context["tiers"] = _get_tiers_data()
|
||||
|
||||
# Add-ons (hardcoded for now, will come from DB)
|
||||
context["addons"] = [
|
||||
{
|
||||
"code": "domain",
|
||||
"name": "Custom Domain",
|
||||
"description": "Use your own domain (mydomain.com)",
|
||||
"price": 15,
|
||||
"billing_period": "year",
|
||||
"icon": "globe",
|
||||
},
|
||||
{
|
||||
"code": "ssl_premium",
|
||||
"name": "Premium SSL",
|
||||
"description": "EV certificate for trust badges",
|
||||
"price": 49,
|
||||
"billing_period": "year",
|
||||
"icon": "shield-check",
|
||||
},
|
||||
{
|
||||
"code": "email",
|
||||
"name": "Email Package",
|
||||
"description": "Professional email addresses",
|
||||
"price": 5,
|
||||
"billing_period": "month",
|
||||
"icon": "mail",
|
||||
"options": [
|
||||
{"quantity": 5, "price": 5},
|
||||
{"quantity": 10, "price": 9},
|
||||
{"quantity": 25, "price": 19},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"cms/public/homepage-wizamart.html",
|
||||
context,
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# GENERIC CONTENT PAGES (CMS)
|
||||
# ============================================================================
|
||||
# IMPORTANT: This route must be LAST as it catches all /{slug} URLs
|
||||
|
||||
|
||||
@router.get("/{slug}", response_class=HTMLResponse, name="platform_content_page")
|
||||
async def content_page(
|
||||
request: Request,
|
||||
slug: str,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Serve CMS content pages (about, contact, faq, privacy, terms, etc.).
|
||||
|
||||
This is a catch-all route for dynamic content pages managed via the admin CMS.
|
||||
Platform pages have vendor_id=None and is_platform_page=True.
|
||||
"""
|
||||
# Get platform from middleware (default to OMS platform_id=1)
|
||||
platform = getattr(request.state, "platform", None)
|
||||
platform_id = platform.id if platform else 1
|
||||
|
||||
# Load platform marketing page from database
|
||||
page = content_page_service.get_platform_page(
|
||||
db, platform_id=platform_id, slug=slug, include_unpublished=False
|
||||
)
|
||||
|
||||
if not page:
|
||||
raise HTTPException(status_code=404, detail=f"Page not found: {slug}")
|
||||
|
||||
context = get_public_context(request, db)
|
||||
context["page"] = page
|
||||
context["page_title"] = page.title
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"cms/public/content-page.html",
|
||||
context,
|
||||
)
|
||||
175
app/modules/cms/routes/pages/storefront.py
Normal file
175
app/modules/cms/routes/pages/storefront.py
Normal file
@@ -0,0 +1,175 @@
|
||||
# app/modules/cms/routes/pages/storefront.py
|
||||
"""
|
||||
CMS Storefront Page Routes (HTML rendering).
|
||||
|
||||
Storefront (customer shop) pages for CMS content:
|
||||
- Generic content pages (/{slug} catch-all)
|
||||
- Debug context endpoint
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_db
|
||||
from app.modules.cms.services import content_page_service
|
||||
from app.modules.core.utils.page_context import get_storefront_context
|
||||
from app.templates_config import templates
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Route configuration - high priority so catch-all is registered last
|
||||
ROUTE_CONFIG = {
|
||||
"priority": 100,
|
||||
}
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# DYNAMIC CONTENT PAGES (CMS)
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@router.get("/{slug}", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def generic_content_page(
|
||||
request: Request,
|
||||
slug: str = Path(..., description="Content page slug"),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
Generic content page handler (CMS).
|
||||
|
||||
Handles dynamic content pages like:
|
||||
- /about, /faq, /contact, /shipping, /returns, /privacy, /terms, etc.
|
||||
|
||||
Features:
|
||||
- Two-tier system: Vendor overrides take priority, fallback to platform defaults
|
||||
- Only shows published pages
|
||||
- Returns 404 if page not found
|
||||
|
||||
This route MUST be defined last in the router to avoid conflicts with
|
||||
specific routes (like /products, /cart, /account, etc.)
|
||||
"""
|
||||
logger.debug(
|
||||
"[CMS_STOREFRONT] generic_content_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"slug": slug,
|
||||
"vendor": getattr(request.state, "vendor", "NOT SET"),
|
||||
"context": getattr(request.state, "context_type", "NOT SET"),
|
||||
},
|
||||
)
|
||||
|
||||
vendor = getattr(request.state, "vendor", None)
|
||||
platform = getattr(request.state, "platform", None)
|
||||
vendor_id = vendor.id if vendor else None
|
||||
platform_id = platform.id if platform else 1 # Default to OMS
|
||||
|
||||
# Load content page from database (vendor override -> vendor default)
|
||||
page = content_page_service.get_page_for_vendor(
|
||||
db,
|
||||
platform_id=platform_id,
|
||||
slug=slug,
|
||||
vendor_id=vendor_id,
|
||||
include_unpublished=False,
|
||||
)
|
||||
|
||||
if not page:
|
||||
logger.warning(
|
||||
"[CMS_STOREFRONT] Content page not found",
|
||||
extra={
|
||||
"slug": slug,
|
||||
"vendor_id": vendor_id,
|
||||
"vendor_name": vendor.name if vendor else None,
|
||||
},
|
||||
)
|
||||
raise HTTPException(status_code=404, detail=f"Page not found: {slug}")
|
||||
|
||||
logger.info(
|
||||
"[CMS_STOREFRONT] Content page found",
|
||||
extra={
|
||||
"slug": slug,
|
||||
"page_id": page.id,
|
||||
"page_title": page.title,
|
||||
"is_vendor_override": page.vendor_id is not None,
|
||||
"vendor_id": vendor_id,
|
||||
},
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"cms/storefront/content-page.html",
|
||||
get_storefront_context(request, db=db, page=page),
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# DEBUG ENDPOINTS - For troubleshooting context issues
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@router.get("/debug/context", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def debug_context(request: Request):
|
||||
"""
|
||||
DEBUG ENDPOINT: Display request context.
|
||||
|
||||
Shows what's available in request.state.
|
||||
Useful for troubleshooting template variable issues.
|
||||
|
||||
URL: /storefront/debug/context
|
||||
"""
|
||||
import json
|
||||
|
||||
vendor = getattr(request.state, "vendor", None)
|
||||
theme = getattr(request.state, "theme", None)
|
||||
|
||||
debug_info = {
|
||||
"path": request.url.path,
|
||||
"host": request.headers.get("host", ""),
|
||||
"vendor": {
|
||||
"found": vendor is not None,
|
||||
"id": vendor.id if vendor else None,
|
||||
"name": vendor.name if vendor else None,
|
||||
"subdomain": vendor.subdomain if vendor else None,
|
||||
"is_active": vendor.is_active if vendor else None,
|
||||
},
|
||||
"theme": {
|
||||
"found": theme is not None,
|
||||
"name": theme.get("theme_name") if theme else None,
|
||||
},
|
||||
"clean_path": getattr(request.state, "clean_path", "NOT SET"),
|
||||
"context_type": str(getattr(request.state, "context_type", "NOT SET")),
|
||||
}
|
||||
|
||||
html_content = f"""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Debug Context</title>
|
||||
<style>
|
||||
body {{ font-family: monospace; margin: 20px; }}
|
||||
pre {{ background: #f0f0f0; padding: 20px; border-radius: 5px; }}
|
||||
.good {{ color: green; }}
|
||||
.bad {{ color: red; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Request Context Debug</h1>
|
||||
<pre>{json.dumps(debug_info, indent=2)}</pre>
|
||||
|
||||
<h2>Status</h2>
|
||||
<p class="{"good" if vendor else "bad"}">
|
||||
Vendor: {"Found" if vendor else "Not Found"}
|
||||
</p>
|
||||
<p class="{"good" if theme else "bad"}">
|
||||
Theme: {"Found" if theme else "Not Found"}
|
||||
</p>
|
||||
<p class="{"good" if str(getattr(request.state, "context_type", "NOT SET")) == "storefront" else "bad"}">
|
||||
Context Type: {str(getattr(request.state, "context_type", "NOT SET"))}
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
return HTMLResponse(content=html_content)
|
||||
@@ -13,7 +13,7 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_vendor_from_cookie_or_header, get_db
|
||||
from app.modules.cms.services import content_page_service
|
||||
from app.services.platform_settings_service import platform_settings_service # noqa: MOD-004 - shared platform service
|
||||
from app.modules.core.services.platform_settings_service import platform_settings_service # noqa: MOD-004 - shared platform service
|
||||
from app.templates_config import templates
|
||||
from models.database.user import User
|
||||
from models.database.vendor import Vendor
|
||||
|
||||
Reference in New Issue
Block a user