- Fix list_by_type to return node_id (IPFS CID) instead of local hash - Fix effects count on home page (count from _effects/ directory) - Add nav_counts to all page templates (recipes, effects, runs, media, storage) - Add editable metadata section to cache/media detail page - Show more metadata on recipe detail page (ID, IPFS CID, step count) - Update tests for new list_by_type behavior Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
125 lines
4.1 KiB
Python
125 lines
4.1 KiB
Python
"""
|
|
Home and root routes for L1 server.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
import markdown
|
|
from fastapi import APIRouter, Request, Depends, HTTPException
|
|
from fastapi.responses import HTMLResponse, RedirectResponse, FileResponse
|
|
|
|
from artdag_common import render
|
|
from artdag_common.middleware import wants_html
|
|
|
|
from ..dependencies import get_templates, get_current_user
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/")
|
|
async def home(request: Request):
|
|
"""
|
|
Home page - show README and stats.
|
|
"""
|
|
import database
|
|
user = await get_current_user(request)
|
|
|
|
# Load README
|
|
readme_html = ""
|
|
try:
|
|
readme_path = Path(__file__).parent.parent.parent / "README.md"
|
|
if readme_path.exists():
|
|
readme_html = markdown.markdown(readme_path.read_text(), extensions=['tables', 'fenced_code'])
|
|
except Exception:
|
|
pass
|
|
|
|
# Get stats for current user
|
|
stats = {}
|
|
if user:
|
|
try:
|
|
stats["media"] = await database.count_user_items(user.actor_id)
|
|
except Exception:
|
|
pass
|
|
try:
|
|
from ..services.recipe_service import RecipeService
|
|
from ..dependencies import get_redis_client, get_cache_manager
|
|
import logging
|
|
logger = logging.getLogger(__name__)
|
|
recipe_service = RecipeService(get_redis_client(), get_cache_manager())
|
|
recipes = await recipe_service.list_recipes(user.actor_id)
|
|
stats["recipes"] = len(recipes)
|
|
logger.info(f"Home page: found {len(recipes)} recipes for {user.actor_id}")
|
|
except Exception as e:
|
|
import logging
|
|
logging.getLogger(__name__).error(f"Failed to get recipe count: {e}")
|
|
try:
|
|
from ..services.run_service import RunService
|
|
from ..dependencies import get_redis_client, get_cache_manager
|
|
run_service = RunService(database, get_redis_client(), get_cache_manager())
|
|
runs = await run_service.list_runs(user.actor_id)
|
|
stats["runs"] = len(runs)
|
|
except Exception:
|
|
pass
|
|
try:
|
|
storage_providers = await database.get_user_storage_providers(user.actor_id)
|
|
stats["storage"] = len(storage_providers) if storage_providers else 0
|
|
except Exception:
|
|
pass
|
|
try:
|
|
# Effects are stored in _effects/ directory, not in cache
|
|
from pathlib import Path
|
|
from ..dependencies import get_cache_manager
|
|
effects_dir = Path(get_cache_manager().cache_dir) / "_effects"
|
|
if effects_dir.exists():
|
|
stats["effects"] = len([d for d in effects_dir.iterdir() if d.is_dir()])
|
|
else:
|
|
stats["effects"] = 0
|
|
except Exception:
|
|
pass
|
|
|
|
templates = get_templates(request)
|
|
return render(templates, "home.html", request,
|
|
user=user,
|
|
readme_html=readme_html,
|
|
stats=stats,
|
|
nav_counts=stats, # Reuse stats for nav counts
|
|
active_tab="home",
|
|
)
|
|
|
|
|
|
@router.get("/login")
|
|
async def login_redirect(request: Request):
|
|
"""
|
|
Redirect to L2 for login.
|
|
"""
|
|
from ..config import settings
|
|
|
|
if settings.l2_server:
|
|
# Redirect to L2 login with return URL
|
|
return_url = str(request.url_for("auth_callback"))
|
|
login_url = f"{settings.l2_server}/login?return_to={return_url}"
|
|
return RedirectResponse(url=login_url, status_code=302)
|
|
|
|
# No L2 configured - show error
|
|
return HTMLResponse(
|
|
"<html><body><h1>Login not configured</h1>"
|
|
"<p>No L2 server configured for authentication.</p></body></html>",
|
|
status_code=503
|
|
)
|
|
|
|
|
|
# Client tarball path
|
|
CLIENT_TARBALL = Path(__file__).parent.parent.parent / "artdag-client.tar.gz"
|
|
|
|
|
|
@router.get("/download/client")
|
|
async def download_client():
|
|
"""Download the Art DAG CLI client."""
|
|
if not CLIENT_TARBALL.exists():
|
|
raise HTTPException(404, "Client package not found. Run build-client.sh to create it.")
|
|
return FileResponse(
|
|
CLIENT_TARBALL,
|
|
media_type="application/gzip",
|
|
filename="artdag-client.tar.gz"
|
|
)
|