@@ -0,0 +1,84 @@
|
||||
"""Setup wizard state in data/wesp_install_state.json."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from app.services.admin_dashboard_service import get_install_and_warranty, install_state_path
|
||||
|
||||
SETUP_VERSION = 1
|
||||
|
||||
|
||||
def read_install_state(app: Flask) -> Dict[str, Any]:
|
||||
get_install_and_warranty(app)
|
||||
path = install_state_path(app)
|
||||
if not path.is_file():
|
||||
return {}
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
return data if isinstance(data, dict) else {}
|
||||
except (OSError, ValueError, TypeError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
|
||||
def write_install_state(app: Flask, partial: Dict[str, Any]) -> Dict[str, Any]:
|
||||
current = read_install_state(app)
|
||||
current.update(partial)
|
||||
path = install_state_path(app)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(json.dumps(current, indent=2, ensure_ascii=False), encoding="utf-8")
|
||||
return current
|
||||
|
||||
|
||||
def is_setup_completed(app: Flask) -> bool:
|
||||
return bool(read_install_state(app).get("setup_completed"))
|
||||
|
||||
|
||||
def mark_step_completed(app: Flask, step: str) -> List[str]:
|
||||
data = read_install_state(app)
|
||||
steps = list(data.get("completed_steps") or [])
|
||||
if step and step not in steps:
|
||||
steps.append(step)
|
||||
write_install_state(app, {"completed_steps": steps})
|
||||
return steps
|
||||
|
||||
|
||||
def mark_setup_complete(app: Flask) -> Dict[str, Any]:
|
||||
return write_install_state(
|
||||
app,
|
||||
{
|
||||
"setup_completed": True,
|
||||
"completed_at": datetime.now(timezone.utc).isoformat(),
|
||||
"setup_version": SETUP_VERSION,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def reset_setup(app: Flask) -> Dict[str, Any]:
|
||||
return write_install_state(
|
||||
app,
|
||||
{
|
||||
"setup_completed": False,
|
||||
"completed_at": None,
|
||||
"completed_steps": [],
|
||||
"device_role": None,
|
||||
"sync_server_connected": False,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def setup_snapshot(app: Flask) -> Dict[str, Any]:
|
||||
data = read_install_state(app)
|
||||
return {
|
||||
"setup_completed": bool(data.get("setup_completed")),
|
||||
"setup_version": int(data.get("setup_version") or SETUP_VERSION),
|
||||
"device_role": data.get("device_role"),
|
||||
"completed_steps": list(data.get("completed_steps") or []),
|
||||
"completed_at": data.get("completed_at"),
|
||||
"first_launch_at": data.get("first_launch_at"),
|
||||
"sync_server_connected": bool(data.get("sync_server_connected")),
|
||||
}
|
||||
Reference in New Issue
Block a user