Add HTML5 setup form actions
CI / python (push) Has been cancelled
CI / rust (push) Has been cancelled

This commit is contained in:
2026-05-16 21:26:54 +03:00
parent 2580cf9832
commit e71789f51e
3 changed files with 133 additions and 2 deletions
+59 -1
View File
@@ -17,7 +17,7 @@ from difflib import SequenceMatcher
from enum import Enum
from pathlib import Path
from typing import Any, Callable
from urllib.parse import quote, urljoin, urlsplit, urlunsplit
from urllib.parse import parse_qs, quote, urljoin, urlsplit, urlunsplit
from uuid import uuid4
from collaboration import (
@@ -1665,6 +1665,42 @@ async def html5_project_setup_summary(project_id: str) -> Response:
)
@app.post("/html5/projects/{project_id}/setup/source")
async def html5_project_setup_source(project_id: str, request: Request) -> Response:
form = await _html5_form_data(request)
source = ImportSourceKind(_form_value(form, "source") or ImportSourceKind.XML_DUMP.value)
current = _project_setup_response(project_id)
settings = current.settings.model_copy(update={"structure_source": source})
setup = await save_project_settings(project_id, settings)
return Response(
render_html5_setup_summary(project_id, setup),
media_type="text/html; charset=utf-8",
)
@app.post("/html5/projects/{project_id}/setup/import")
async def html5_project_setup_import(project_id: str, request: Request) -> Response:
form = await _html5_form_data(request)
source = ImportSourceKind(_form_value(form, "source") or _current_import_source(project_id).value)
structure_only = _form_value(form, "structure_only") in {"1", "true", "on", "yes"}
_execute_import_project(project_id, ImportRequest(source=source, structure_only=structure_only))
setup = _project_setup_response(project_id)
return Response(
render_html5_setup_summary(project_id, setup),
media_type="text/html; charset=utf-8",
)
@app.post("/html5/projects/{project_id}/setup/reindex")
async def html5_project_setup_reindex(project_id: str) -> Response:
await reindex_project(project_id)
setup = _project_setup_response(project_id)
return Response(
render_html5_setup_summary(project_id, setup),
media_type="text/html; charset=utf-8",
)
@app.get("/version")
async def version() -> dict[str, str]:
return {"name": "sfera", "version": "0.1.0"}
@@ -7700,6 +7736,28 @@ def _project_has_stored_snapshot(project_id: str) -> bool:
return _storage.has_snapshot(project_id)
async def _html5_form_data(request: Request) -> dict[str, list[str]]:
body = (await request.body()).decode("utf-8")
return parse_qs(body, keep_blank_values=True)
def _form_value(form: dict[str, list[str]], key: str) -> str | None:
values = form.get(key)
if not values:
return None
value = values[0].strip()
return value or None
def _current_import_source(project_id: str) -> ImportSourceKind:
setup = _project_setup_response(project_id)
if setup.current_source is not None:
return setup.current_source
if setup.settings.structure_source is not None:
return setup.settings.structure_source
return ImportSourceKind.XML_DUMP
def _project_summaries() -> list[ProjectSummaryResponse]:
project_ids = set(_project_setup.keys())
stored_snapshots = _storage.list_snapshot_refs()