we do a little exporting

This commit is contained in:
2026-03-04 17:49:26 -06:00
parent 843baa15ad
commit fc5b3cd795
7 changed files with 720 additions and 58 deletions

View File

@@ -2,9 +2,13 @@
from __future__ import annotations
from flask import Blueprint, flash, redirect, render_template, url_for
import json
from flask import Blueprint, flash, redirect, render_template, request, url_for
from flask import Response
from physcom.seed.transport_example import load_transport_seed
from physcom.snapshot import export_snapshot, import_snapshot
from physcom_web.app import get_repo
bp = Blueprint("admin", __name__, url_prefix="/admin")
@@ -38,14 +42,50 @@ def reseed():
return redirect(url_for("admin.admin_index"))
@bp.route("/wipe-and-reseed", methods=["POST"])
def wipe_and_reseed():
@bp.route("/wipe", methods=["POST"])
def wipe():
repo = get_repo()
repo.clear_all()
counts = load_transport_seed(repo)
total = counts["platforms"] + counts["actuators"] + counts["energy_storages"]
flash("Wiped all data.", "success")
return redirect(url_for("admin.admin_index"))
@bp.route("/snapshot/export")
def snapshot_export():
repo = get_repo()
data = export_snapshot(repo)
body = json.dumps(data, indent=2)
return Response(
body,
mimetype="application/json",
headers={"Content-Disposition": "attachment; filename=physcom_snapshot.json"},
)
@bp.route("/snapshot/import", methods=["POST"])
def snapshot_import():
repo = get_repo()
clear = "clear" in request.form
file = request.files.get("file")
if not file or not file.filename:
flash("No file selected.", "error")
return redirect(url_for("admin.admin_index"))
try:
raw = file.read().decode("utf-8")
data = json.loads(raw)
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
flash(f"Invalid JSON file: {exc}", "error")
return redirect(url_for("admin.admin_index"))
counts = import_snapshot(repo, data, clear=clear)
mode = "Cleared DB and imported" if clear else "Merged"
flash(
f"Wiped all data and reseeded — {total} entities, {counts['domains']} domains.",
f"{mode} snapshot — {counts['dimensions']} dimensions, "
f"{counts['entities']} entities, {counts['domains']} domains, "
f"{counts['combinations']} combinations, {counts['results']} results, "
f"{counts['scores']} scores.",
"success",
)
return redirect(url_for("admin.admin_index"))