feat: Integrate HTML templates with FastAPI
- Created template_helpers.py for centralized template rendering - Added ux_features.css for enhanced UX styling - Implemented JavaScript modules for: - Keyboard shortcuts (Ctrl+K, Ctrl+R navigation) - User preferences persistence - Undo/redo functionality (Ctrl+Z/Ctrl+Y) - Mobile responsive features - Touch gesture support - Accessibility features (ARIA, focus management) - Screen reader support - Color contrast compliance (WCAG) - Multi-screen support - Updated page_controller.py and error_controller.py to use template helpers - Created comprehensive template integration tests - All templates verified: index.html, login.html, setup.html, queue.html, error.html - Maintained responsive layout and theme switching - Updated instructions.md (removed completed task) - Updated infrastructure.md with template integration details
This commit is contained in:
96
src/server/utils/template_helpers.py
Normal file
96
src/server/utils/template_helpers.py
Normal file
@@ -0,0 +1,96 @@
|
||||
"""
|
||||
Template integration utilities for FastAPI application.
|
||||
|
||||
This module provides utilities for template rendering with common context
|
||||
and helper functions.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
# Configure templates directory
|
||||
TEMPLATES_DIR = Path(__file__).parent.parent / "web" / "templates"
|
||||
templates = Jinja2Templates(directory=str(TEMPLATES_DIR))
|
||||
|
||||
|
||||
def get_base_context(
|
||||
request: Request, title: str = "Aniworld"
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Get base context for all templates.
|
||||
|
||||
Args:
|
||||
request: FastAPI request object
|
||||
title: Page title
|
||||
|
||||
Returns:
|
||||
Dictionary with base context variables
|
||||
"""
|
||||
return {
|
||||
"request": request,
|
||||
"title": title,
|
||||
"app_name": "Aniworld Download Manager",
|
||||
"version": "1.0.0"
|
||||
}
|
||||
|
||||
|
||||
def render_template(
|
||||
template_name: str,
|
||||
request: Request,
|
||||
context: Optional[Dict[str, Any]] = None,
|
||||
title: Optional[str] = None
|
||||
):
|
||||
"""
|
||||
Render a template with base context.
|
||||
|
||||
Args:
|
||||
template_name: Name of the template file
|
||||
request: FastAPI request object
|
||||
context: Additional context variables
|
||||
title: Page title (optional)
|
||||
|
||||
Returns:
|
||||
TemplateResponse object
|
||||
"""
|
||||
base_context = get_base_context(
|
||||
request,
|
||||
title or template_name.replace('.html', '').replace('_', ' ').title()
|
||||
)
|
||||
|
||||
if context:
|
||||
base_context.update(context)
|
||||
|
||||
return templates.TemplateResponse(template_name, base_context)
|
||||
|
||||
|
||||
def validate_template_exists(template_name: str) -> bool:
|
||||
"""
|
||||
Check if a template file exists.
|
||||
|
||||
Args:
|
||||
template_name: Name of the template file
|
||||
|
||||
Returns:
|
||||
True if template exists, False otherwise
|
||||
"""
|
||||
template_path = TEMPLATES_DIR / template_name
|
||||
return template_path.exists()
|
||||
|
||||
|
||||
def list_available_templates() -> list[str]:
|
||||
"""
|
||||
Get list of all available template files.
|
||||
|
||||
Returns:
|
||||
List of template file names
|
||||
"""
|
||||
if not TEMPLATES_DIR.exists():
|
||||
return []
|
||||
|
||||
return [
|
||||
f.name
|
||||
for f in TEMPLATES_DIR.glob("*.html")
|
||||
if f.is_file()
|
||||
]
|
||||
Reference in New Issue
Block a user