middleware fix for path-based vendor url
This commit is contained in:
@@ -344,4 +344,4 @@ class ErrorPageRenderer:
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
return HTMLResponse(content=html_content, status_code=status_code)
|
||||
return HTMLResponse(content=html_content, status_code=status_code)
|
||||
|
||||
@@ -29,6 +29,7 @@ Routes:
|
||||
- GET /shop/account/addresses → Address management (auth required)
|
||||
"""
|
||||
|
||||
import logging
|
||||
from fastapi import APIRouter, Request, Depends, Path
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
@@ -40,59 +41,144 @@ from models.database.user import User
|
||||
router = APIRouter()
|
||||
templates = Jinja2Templates(directory="app/templates")
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# HELPER: Build Shop Template Context
|
||||
# ============================================================================
|
||||
|
||||
def get_shop_context(request: Request, **extra_context) -> dict:
|
||||
"""
|
||||
Build template context for shop pages.
|
||||
|
||||
Automatically includes vendor and theme from middleware request.state.
|
||||
Additional context can be passed as keyword arguments.
|
||||
|
||||
Args:
|
||||
request: FastAPI request object with vendor/theme in state
|
||||
**extra_context: Additional variables for template (user, product_id, etc.)
|
||||
|
||||
Returns:
|
||||
Dictionary with request, vendor, theme, and extra context
|
||||
|
||||
Example:
|
||||
# Simple usage
|
||||
get_shop_context(request)
|
||||
|
||||
# With extra data
|
||||
get_shop_context(request, user=current_user, product_id=123)
|
||||
"""
|
||||
# Extract from middleware state
|
||||
vendor = getattr(request.state, 'vendor', None)
|
||||
theme = getattr(request.state, 'theme', None)
|
||||
clean_path = getattr(request.state, 'clean_path', request.url.path)
|
||||
|
||||
if vendor is None:
|
||||
logger.warning(
|
||||
"[SHOP_CONTEXT] Vendor not found in request.state",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"host": request.headers.get("host", ""),
|
||||
"has_vendor": False,
|
||||
}
|
||||
)
|
||||
|
||||
context = {
|
||||
"request": request,
|
||||
"vendor": vendor,
|
||||
"theme": theme,
|
||||
"clean_path": clean_path,
|
||||
}
|
||||
|
||||
# Add any extra context (user, product_id, category_slug, etc.)
|
||||
if extra_context:
|
||||
context.update(extra_context)
|
||||
|
||||
logger.debug(
|
||||
f"[SHOP_CONTEXT] Context built",
|
||||
extra={
|
||||
"vendor_id": vendor.id if vendor else None,
|
||||
"vendor_name": vendor.name if vendor else None,
|
||||
"has_theme": theme is not None,
|
||||
"extra_keys": list(extra_context.keys()) if extra_context else [],
|
||||
}
|
||||
)
|
||||
|
||||
return context
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# PUBLIC SHOP ROUTES (No Authentication Required)
|
||||
# ============================================================================
|
||||
|
||||
@router.get("/shop/", response_class=HTMLResponse, include_in_schema=False)
|
||||
@router.get("/shop/products", response_class=HTMLResponse, include_in_schema=False)
|
||||
@router.get("/", response_class=HTMLResponse, include_in_schema=False)
|
||||
@router.get("/products", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_products_page(request: Request):
|
||||
"""
|
||||
Render shop homepage / product catalog.
|
||||
Shows featured products and categories.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/products.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@router.get("/shop/products/{product_id}", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_product_detail_page(
|
||||
request: Request,
|
||||
product_id: int = Path(..., description="Product ID")
|
||||
request: Request,
|
||||
product_id: int = Path(..., description="Product ID")
|
||||
):
|
||||
"""
|
||||
Render product detail page.
|
||||
Shows product information, images, reviews, and buy options.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/product.html",
|
||||
{
|
||||
"request": request,
|
||||
"product_id": product_id,
|
||||
}
|
||||
get_shop_context(request, product_id=product_id)
|
||||
)
|
||||
|
||||
|
||||
@router.get("/shop/categories/{category_slug}", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_category_page(
|
||||
request: Request,
|
||||
category_slug: str = Path(..., description="Category slug")
|
||||
request: Request,
|
||||
category_slug: str = Path(..., description="Category slug")
|
||||
):
|
||||
"""
|
||||
Render category products page.
|
||||
Shows all products in a specific category.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/category.html",
|
||||
{
|
||||
"request": request,
|
||||
"category_slug": category_slug,
|
||||
}
|
||||
get_shop_context(request, category_slug=category_slug)
|
||||
)
|
||||
|
||||
|
||||
@@ -102,11 +188,18 @@ async def shop_cart_page(request: Request):
|
||||
Render shopping cart page.
|
||||
Shows cart items and allows quantity updates.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/cart.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -116,11 +209,18 @@ async def shop_checkout_page(request: Request):
|
||||
Render checkout page.
|
||||
Handles shipping, payment, and order confirmation.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/checkout.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -130,11 +230,18 @@ async def shop_search_page(request: Request):
|
||||
Render search results page.
|
||||
Shows products matching search query.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/search.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -148,11 +255,18 @@ async def shop_register_page(request: Request):
|
||||
Render customer registration page.
|
||||
No authentication required.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/register.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -162,11 +276,18 @@ async def shop_login_page(request: Request):
|
||||
Render customer login page.
|
||||
No authentication required.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/login.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -176,11 +297,18 @@ async def shop_forgot_password_page(request: Request):
|
||||
Render forgot password page.
|
||||
Allows customers to reset their password.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/forgot-password.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -193,148 +321,198 @@ async def shop_account_root():
|
||||
"""
|
||||
Redirect /shop/account/ to dashboard.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return RedirectResponse(url="/shop/account/dashboard", status_code=302)
|
||||
|
||||
|
||||
@router.get("/shop/account/dashboard", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_account_dashboard_page(
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Render customer account dashboard.
|
||||
Shows account overview, recent orders, and quick links.
|
||||
Requires customer authentication.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/dashboard.html",
|
||||
{
|
||||
"request": request,
|
||||
"user": current_user,
|
||||
}
|
||||
get_shop_context(request, user=current_user)
|
||||
)
|
||||
|
||||
|
||||
@router.get("/shop/account/orders", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_orders_page(
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Render customer orders history page.
|
||||
Shows all past and current orders.
|
||||
Requires customer authentication.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/orders.html",
|
||||
{
|
||||
"request": request,
|
||||
"user": current_user,
|
||||
}
|
||||
get_shop_context(request, user=current_user)
|
||||
)
|
||||
|
||||
|
||||
@router.get("/shop/account/orders/{order_id}", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_order_detail_page(
|
||||
request: Request,
|
||||
order_id: int = Path(..., description="Order ID"),
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
request: Request,
|
||||
order_id: int = Path(..., description="Order ID"),
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Render customer order detail page.
|
||||
Shows detailed order information and tracking.
|
||||
Requires customer authentication.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/order-detail.html",
|
||||
{
|
||||
"request": request,
|
||||
"user": current_user,
|
||||
"order_id": order_id,
|
||||
}
|
||||
get_shop_context(request, user=current_user, order_id=order_id)
|
||||
)
|
||||
|
||||
|
||||
@router.get("/shop/account/profile", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_profile_page(
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Render customer profile page.
|
||||
Edit personal information and preferences.
|
||||
Requires customer authentication.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/profile.html",
|
||||
{
|
||||
"request": request,
|
||||
"user": current_user,
|
||||
}
|
||||
get_shop_context(request, user=current_user)
|
||||
)
|
||||
|
||||
|
||||
@router.get("/shop/account/addresses", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_addresses_page(
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Render customer addresses management page.
|
||||
Manage shipping and billing addresses.
|
||||
Requires customer authentication.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/addresses.html",
|
||||
{
|
||||
"request": request,
|
||||
"user": current_user,
|
||||
}
|
||||
get_shop_context(request, user=current_user)
|
||||
)
|
||||
|
||||
|
||||
@router.get("/shop/account/wishlist", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_wishlist_page(
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Render customer wishlist page.
|
||||
View and manage saved products.
|
||||
Requires customer authentication.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/wishlist.html",
|
||||
{
|
||||
"request": request,
|
||||
"user": current_user,
|
||||
}
|
||||
get_shop_context(request, user=current_user)
|
||||
)
|
||||
|
||||
|
||||
@router.get("/shop/account/settings", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def shop_settings_page(
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
request: Request,
|
||||
current_user: User = Depends(get_current_customer_from_cookie_or_header),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Render customer account settings page.
|
||||
Configure notifications, privacy, and preferences.
|
||||
Requires customer authentication.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/account/settings.html",
|
||||
{
|
||||
"request": request,
|
||||
"user": current_user,
|
||||
}
|
||||
get_shop_context(request, user=current_user)
|
||||
)
|
||||
|
||||
|
||||
@@ -347,11 +525,18 @@ async def shop_about_page(request: Request):
|
||||
"""
|
||||
Render about us page.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/about.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -360,11 +545,18 @@ async def shop_contact_page(request: Request):
|
||||
"""
|
||||
Render contact us page.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/contact.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -373,11 +565,18 @@ async def shop_faq_page(request: Request):
|
||||
"""
|
||||
Render FAQ page.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/faq.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -386,11 +585,18 @@ async def shop_privacy_page(request: Request):
|
||||
"""
|
||||
Render privacy policy page.
|
||||
"""
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/privacy.html",
|
||||
{
|
||||
"request": request,
|
||||
}
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
@@ -399,9 +605,85 @@ async def shop_terms_page(request: Request):
|
||||
"""
|
||||
Render terms and conditions page.
|
||||
"""
|
||||
return templates.TemplateResponse(
|
||||
"shop/terms.html",
|
||||
{
|
||||
"request": request,
|
||||
logger.debug(
|
||||
f"[SHOP_HANDLER] shop_products_page REACHED",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"vendor": getattr(request.state, 'vendor', 'NOT SET'),
|
||||
"context": getattr(request.state, 'context_type', 'NOT SET'),
|
||||
}
|
||||
)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"shop/terms.html",
|
||||
get_shop_context(request)
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# 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: /shop/debug/context
|
||||
"""
|
||||
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')),
|
||||
}
|
||||
|
||||
# Return as JSON-like HTML for easy reading
|
||||
import json
|
||||
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')) == 'shop' else 'bad'}">
|
||||
Context Type: {str(getattr(request.state, 'context_type', 'NOT SET'))}
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
return HTMLResponse(content=html_content)
|
||||
|
||||
Reference in New Issue
Block a user