"""Fix indentation in app.py - rewrite broken toggle/reload/new-form/update section."""
import re
path = "webui/app.py"
with open(path) as f:
content = f.read()
# The broken section: from @app.post toggle to end of task_update
# Find the toggle endpoint
start_marker = '@app.post("/api/task/{uid}/toggle")'
end_marker = '# --- Календарь ---'
start_idx = content.index(start_marker)
end_idx = content.index(end_marker, start_idx)
# Replacement section (clean, correct indentation)
new_code = '''@app.post("/api/task/{uid}/toggle")
def task_toggle(uid: str, request: Request, _: AuthDep):
"""Toggle COMPLETED/NEEDS-ACTION. HTMX returns reload stub."""
res = radicale.toggle_task(uid)
if res.get("error"):
return {"error": res["error"]}
if request.headers.get("hx-request", "").lower() == "true":
return HTMLResponse('
')
return {"ok": True, "status": res["status"]}
@app.get("/api/tasks/reload")
def tasks_reload(request: Request, _: AuthDep):
"""HTMX tasks card: list + create button."""
tasks = radicale.get_tasks()
items = []
for t in tasks:
due_html = f'\\u0441\\u0440\\u043e\\u043a {t["due"]}
' if t.get("due") else ""
items.append(f'''
{t[\\'summary\\']}
{due_html}
''')
items_html = "".join(items) or '\\u0417\\u0430\\u0434\\u0430\\u0447 \\u043d\\u0435\\u0442 \\u0438\\u043b\\u0438 Radicale \\u043d\\u0435\\u0434\\u043e\\u0441\\u0442\\u0443\\u043f\\u0435\\u043d
'
active = sum(1 for t in tasks if not t["completed"])
btn = ''
html = f'''\\u2705 \\u0422\\u0435\\u043a\\u0443\\u0449\\u0438\\u0435 \\u0437\\u0430\\u0434\\u0430\\u0447\\u0438 {active}
{items_html}{btn}
'''
return HTMLResponse(html)
@app.get("/api/tasks/new-form")
def task_new_form(_: AuthDep):
"""HTMX create-task form fragment."""
return HTMLResponse('''
''')
@app.put("/api/task/{uid}/update")
async def task_update(uid: str, request: Request, _: AuthDep):
"""Update SUMMARY and/or DUE of a task."""
import json
raw = await request.body()
try:
data = json.loads(raw)
except Exception:
return {"error": "invalid JSON"}
summary = (data.get("summary") or "")[:120]
due = data.get("due", "") or ""
res = radicale.update_task(uid, summary, due)
if res.get("error"):
return {"error": res["error"]}
return {"ok": True}
'''
content = content[:start_idx] + new_code + content[end_idx:]
with open(path, "w") as f:
f.write(content)
# Verify syntax
compile(open(path).read(), path, 'exec')
print("OK - syntax valid")