34 lines
992 B
Python
34 lines
992 B
Python
import json
|
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
|
|
templates = Jinja2Templates(directory="app/templates")
|
|
|
|
|
|
def status_badge_config(status: str) -> tuple[str, str]:
|
|
config = {
|
|
"pending": ("等待中", "bg-secondary"),
|
|
"running": ("运行中", "bg-warning text-dark"),
|
|
"success": ("已完成", "bg-success"),
|
|
"failed": ("失败", "bg-danger"),
|
|
}
|
|
return config.get(status, ("未知", "bg-secondary"))
|
|
|
|
|
|
def platform_label(platform: str) -> str:
|
|
return {"xiaohongshu": "小红书", "douyin": "抖音"}.get(platform, platform)
|
|
|
|
|
|
def from_json_filter(value: str | None) -> list:
|
|
try:
|
|
parsed = json.loads(value) if value else []
|
|
except (TypeError, json.JSONDecodeError):
|
|
return []
|
|
return parsed if isinstance(parsed, list) else []
|
|
|
|
|
|
templates.env.globals["status_badge_config"] = status_badge_config
|
|
templates.env.filters["platform_label"] = platform_label
|
|
templates.env.filters["from_json"] = from_json_filter
|