Éditeur : brouillon, bouton Rétablir et catalogue mis à jour en direct

- Chaque modification est écrite dans src/config.draft.json ; config.json
  ne change qu'au clic sur Enregistrer. L'aperçu affiche le brouillon tant
  qu'il existe.
- Nouveau bouton "Rétablir l'original" qui abandonne le brouillon.
- Le catalogue YunoHost est filtré côté éditeur selon l'état courant : une
  app retirée avec la croix y réapparaît immédiatement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
paraandClaude Opus 5.5 committed 2026-11-13 14:53:58 +01:00
1 parent a35b732fa6
commit d42141eb9f
6 files changed
+162 -32

No files matched your search

+63 -15
View File
@@ -12,7 +12,7 @@ import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from yunohost_sync import build_app_list, fetch_remote_apps, known_urls, load_env, sync_target
from yunohost_sync import build_app_list, fetch_remote_apps, load_env, sync_target
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
@@ -21,8 +21,13 @@ PROJECT_ROOT = SCRIPT_DIR.parent
SRC_DIR = PROJECT_ROOT / "src"
OUTPUT_DIR = PROJECT_ROOT / "output"
DEMO_CATALOG_PATH = SRC_DIR / "catalog.demo.json"
CONFIG_PATH = SRC_DIR / "config.json"
# Brouillon de l'editeur : ecrit a chaque modification, sert de config a
# l'apercu tant qu'il existe, devient config.json au clic sur Enregistrer
# et est supprime par "Retablir l'original".
DRAFT_PATH = SRC_DIR / "config.draft.json"
WATCHED_FILES = ["index.html", "style.css", "app.js", "config.json"]
WATCHED_FILES = ["index.html", "style.css", "app.js", "config.json", "config.draft.json"]
EXPORTED_FILES = ["index.html", "style.css", "app.js", "config.json"]
RELOAD_SNIPPET = b"""
@@ -80,6 +85,16 @@ class ReloadingHandler(http.server.SimpleHTTPRequestHandler):
self._send_catalog()
return
route = self.path.split("?", 1)[0]
if route == "/config.json":
draft = DRAFT_PATH.exists()
self._send_file(DRAFT_PATH if draft else CONFIG_PATH, draft)
return
if route == "/__original":
self._send_file(CONFIG_PATH, False)
return
if self.path in ("/", "/index.html"):
try:
html = (SRC_DIR / "index.html").read_bytes()
@@ -96,17 +111,20 @@ class ReloadingHandler(http.server.SimpleHTTPRequestHandler):
super().do_GET()
def do_POST(self):
if self.path == "/__save":
length = int(self.headers.get("Content-Length", 0))
raw = self.rfile.read(length)
try:
data = json.loads(raw)
except json.JSONDecodeError as exc:
self.send_error(400, f"JSON invalide: {exc}")
if self.path in ("/__save", "/__draft"):
data = self._read_json_body()
if data is None:
return
with open(SRC_DIR / "config.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
f.write("\n")
if self.path == "/__save":
self._write_json(CONFIG_PATH, data)
DRAFT_PATH.unlink(missing_ok=True)
else:
self._write_json(DRAFT_PATH, data)
self._send_json({"ok": True})
return
if self.path == "/__discard":
DRAFT_PATH.unlink(missing_ok=True)
self._send_json({"ok": True})
return
@@ -139,16 +157,46 @@ class ReloadingHandler(http.server.SimpleHTTPRequestHandler):
if not ssh_target or not domain:
self._send_json({"error": "SYNC_SSH_HOST (ou REMOTE_HOST) et PORTAL_DOMAIN manquants dans .env"})
return
# Liste complete : l'editeur filtre lui-meme les apps deja placees,
# pour que le catalogue suive les ajouts/suppressions sans recharger.
try:
with open(SRC_DIR / "config.json", encoding="utf-8") as f:
config = json.load(f)
remote = fetch_remote_apps(ssh_target, domain)
apps = build_app_list(remote, domain, self_app_id, exclude_urls=known_urls(config))
apps = build_app_list(remote, domain, self_app_id)
except Exception as exc: # SSH/JSON/reseau : on remonte un message lisible
self._send_json({"error": str(exc)})
return
self._send_json({"apps": apps})
def _read_json_body(self):
length = int(self.headers.get("Content-Length", 0))
raw = self.rfile.read(length)
try:
return json.loads(raw)
except json.JSONDecodeError as exc:
self.send_error(400, f"JSON invalide: {exc}")
return None
@staticmethod
def _write_json(path, data):
tmp = path.with_suffix(".tmp")
with open(tmp, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
f.write("\n")
tmp.replace(path)
def _send_file(self, path, is_draft):
try:
body = path.read_bytes()
except OSError:
self.send_error(404)
return
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.send_header("X-Homepage-Draft", "1" if is_draft else "0")
self.end_headers()
self.wfile.write(body)
def _send_json(self, payload):
body = json.dumps(payload).encode()
self.send_response(200)