""" Ticket System Frontend Views HTML template routes for ticket management UI """ import logging from fastapi import APIRouter, Request, HTTPException, Form from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.templating import Jinja2Templates from typing import Optional, Dict, Any from datetime import date from app.core.database import execute_query, execute_update, execute_query_single, table_has_column logger = logging.getLogger(__name__) router = APIRouter() templates = Jinja2Templates(directory="app") def _case_start_date_sql(alias: str = "s") -> str: """Select start_date only when the live schema actually has it.""" if table_has_column("sag_sager", "start_date"): return f"{alias}.start_date" return "NULL::date AS start_date" def _case_type_sql(alias: str = "s") -> str: """Select case type across old/new sag schemas.""" if table_has_column("sag_sager", "type"): return f"COALESCE({alias}.template_key, {alias}.type, 'ticket') AS case_type" return f"COALESCE({alias}.template_key, 'ticket') AS case_type" @router.get("/", include_in_schema=False) async def ticket_root_redirect(): return RedirectResponse(url="/sag", status_code=302) def _format_long_text(value: Optional[str]) -> str: if not value: return "" lines = value.splitlines() blocks = [] buffer = [] in_list = False def flush_paragraph(): nonlocal buffer if buffer: blocks.append("

" + " ".join(buffer).strip() + "

") buffer = [] def close_list(): nonlocal in_list if in_list: blocks.append("") in_list = False for raw_line in lines: line = raw_line.strip() if not line: flush_paragraph() close_list() continue if line.startswith(("- ", "* ", "• ")): flush_paragraph() if not in_list: blocks.append("