simplify: one scoring path, bare AST formulas, localhost-only dev server

- move _score_raw into scorer.score_raw; Scorer delegates to it
- compile_formula returns ast.Expression directly (drop CompiledFormula)
- secret key from env or random; drop hand-rolled .env parsing/appending
- dev server binds 127.0.0.1 by default (HOST env override); Docker sets
  HOST=0.0.0.0 so container port mapping still works
- trim pipeline comments narrating past bugs (history lives in git)
- snapshot free-var/formula (de)serialization via asdict/**kwargs
- domains routes: shared form parsing and partial rendering helpers

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-09-25 15:03:59 -05:00
parent 3429bce8d0
commit b2ccd94fd5
8 changed files with 111 additions and 270 deletions

View File

@@ -14,29 +14,6 @@ from physcom.units import format_quantity
DEFAULT_DB = Path("data/physcom.db")
_ENV_FILE = Path(".env")
def _load_or_generate_secret_key() -> str:
"""Return FLASK_SECRET_KEY from env, .env file, or auto-generate and persist it."""
key = os.environ.get("FLASK_SECRET_KEY", "").strip()
if key:
return key
# Try to load from .env
if _ENV_FILE.exists():
for line in _ENV_FILE.read_text().splitlines():
line = line.strip()
if line.startswith("FLASK_SECRET_KEY="):
key = line[len("FLASK_SECRET_KEY="):].strip()
if key:
return key
# Generate, persist, and return
key = secrets.token_hex(32)
with _ENV_FILE.open("a") as f:
f.write(f"FLASK_SECRET_KEY={key}\n")
return key
def get_repo() -> Repository:
@@ -56,7 +33,7 @@ def close_db(exc: BaseException | None = None) -> None:
def create_app() -> Flask:
app = Flask(__name__)
app.secret_key = _load_or_generate_secret_key()
app.secret_key = os.environ.get("FLASK_SECRET_KEY") or secrets.token_hex(32)
app.jinja_env.filters["qty"] = format_quantity
@@ -97,4 +74,4 @@ def create_app() -> Flask:
def run() -> None:
"""Entry point for `physcom-web` script."""
app = create_app()
app.run(host="0.0.0.0", debug=True, port=int(os.environ.get("PORT", "5000")))
app.run(host=os.environ.get("HOST", "127.0.0.1"), debug=True, port=int(os.environ.get("PORT", "5000")))

View File

@@ -122,55 +122,50 @@ def metric_delete(domain_id: int, metric_id: int):
# ── Free variable CRUD (HTMX partials) ───────────────────────
def _render(domain_id: int, partial: str):
domain = get_repo().get_domain_by_id(domain_id)
return render_template(f"domains/{partial}", domain=domain)
def _fv_from_form() -> FreeVariable:
return FreeVariable(
name=request.form["name"].strip(),
floor_formula=request.form.get("floor_formula", "").strip(),
ceiling_formula=request.form.get("ceiling_formula", "").strip(),
sort_order=request.form.get("sort_order", 0, type=int),
)
def _mf_from_form() -> MetricFormula:
return MetricFormula(
metric_name=request.form["metric_name"].strip(),
formula=request.form.get("formula", "").strip(),
)
@bp.route("/<int:domain_id>/free-vars/add", methods=["POST"])
def free_var_add(domain_id: int):
repo = get_repo()
name = request.form["name"].strip()
floor_formula = request.form.get("floor_formula", "").strip()
ceiling_formula = request.form.get("ceiling_formula", "").strip()
try:
sort_order = int(request.form.get("sort_order", "0"))
except ValueError:
sort_order = 0
if not name or not floor_formula or not ceiling_formula:
fv = _fv_from_form()
if not fv.name or not fv.floor_formula or not fv.ceiling_formula:
flash("Name, floor formula, and ceiling formula are required.", "error")
else:
fv = FreeVariable(
name=name, floor_formula=floor_formula, ceiling_formula=ceiling_formula,
sort_order=sort_order,
)
repo.add_free_variable(domain_id, fv)
flash(f"Free variable '{name}' added.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_free_vars_table.html", domain=domain)
get_repo().add_free_variable(domain_id, fv)
flash(f"Free variable '{fv.name}' added.", "success")
return _render(domain_id, "_free_vars_table.html")
@bp.route("/<int:domain_id>/free-vars/<int:fv_id>/edit", methods=["POST"])
def free_var_edit(domain_id: int, fv_id: int):
repo = get_repo()
try:
sort_order = int(request.form.get("sort_order", "0"))
except ValueError:
sort_order = 0
fv = FreeVariable(
name=request.form["name"].strip(),
floor_formula=request.form.get("floor_formula", "").strip(),
ceiling_formula=request.form.get("ceiling_formula", "").strip(),
sort_order=sort_order,
)
repo.update_free_variable(fv_id, fv)
get_repo().update_free_variable(fv_id, _fv_from_form())
flash("Free variable updated.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_free_vars_table.html", domain=domain)
return _render(domain_id, "_free_vars_table.html")
@bp.route("/<int:domain_id>/free-vars/<int:fv_id>/delete", methods=["POST"])
def free_var_delete(domain_id: int, fv_id: int):
repo = get_repo()
repo.delete_free_variable(fv_id)
get_repo().delete_free_variable(fv_id)
flash("Free variable removed.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_free_vars_table.html", domain=domain)
return _render(domain_id, "_free_vars_table.html")
# ── Metric formula CRUD (HTMX partials) ──────────────────────
@@ -178,35 +173,24 @@ def free_var_delete(domain_id: int, fv_id: int):
@bp.route("/<int:domain_id>/formulas/add", methods=["POST"])
def formula_add(domain_id: int):
repo = get_repo()
metric_name = request.form["metric_name"].strip()
formula = request.form.get("formula", "").strip()
if not metric_name or not formula:
mf = _mf_from_form()
if not mf.metric_name or not mf.formula:
flash("Metric name and formula are required.", "error")
else:
repo.add_metric_formula(domain_id, MetricFormula(metric_name=metric_name, formula=formula))
flash(f"Formula for '{metric_name}' added.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_formulas_table.html", domain=domain)
get_repo().add_metric_formula(domain_id, mf)
flash(f"Formula for '{mf.metric_name}' added.", "success")
return _render(domain_id, "_formulas_table.html")
@bp.route("/<int:domain_id>/formulas/<int:formula_id>/edit", methods=["POST"])
def formula_edit(domain_id: int, formula_id: int):
repo = get_repo()
mf = MetricFormula(
metric_name=request.form["metric_name"].strip(),
formula=request.form.get("formula", "").strip(),
)
repo.update_metric_formula(formula_id, mf)
get_repo().update_metric_formula(formula_id, _mf_from_form())
flash("Formula updated.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_formulas_table.html", domain=domain)
return _render(domain_id, "_formulas_table.html")
@bp.route("/<int:domain_id>/formulas/<int:formula_id>/delete", methods=["POST"])
def formula_delete(domain_id: int, formula_id: int):
repo = get_repo()
repo.delete_metric_formula(formula_id)
get_repo().delete_metric_formula(formula_id)
flash("Formula removed.", "success")
domain = repo.get_domain_by_id(domain_id)
return render_template("domains/_formulas_table.html", domain=domain)
return _render(domain_id, "_formulas_table.html")