From 2ec4d9157ca77a4633953788fe1537f54bf6b5f2 Mon Sep 17 00:00:00 2001 From: Davide Scaini Date: Wed, 13 May 2026 23:19:19 +0200 Subject: [PATCH] Refactor: extract edit UI HTML into bincio/edit/templates/edit.html MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 285-line _HTML string literal in edit/server.py is replaced by a template file loaded at request time. The route handler is unchanged in behaviour — it still substitutes __SITE_URL__, __SPORT_OPTIONS__, and __STAT_CHECKBOXES__ before returning the response. Five new tests cover: 200 response, form presence, site_url injection, no unresolved placeholders, and template file existence on disk. --- bincio/edit/server.py | 293 +------------------------------- bincio/edit/templates/edit.html | 283 ++++++++++++++++++++++++++++++ refactoring.md | 2 +- tests/test_edit_server.py | 31 ++++ 4 files changed, 318 insertions(+), 291 deletions(-) create mode 100644 bincio/edit/templates/edit.html diff --git a/bincio/edit/server.py b/bincio/edit/server.py index 21e6f70..d00f43f 100644 --- a/bincio/edit/server.py +++ b/bincio/edit/server.py @@ -48,293 +48,7 @@ from bincio.shared.images import MAX_IMAGE_BYTES as _MAX_IMAGE_BYTES from bincio.shared.images import unique_image_name as _unique_image_name -# ── HTML UI ─────────────────────────────────────────────────────────────────── - -_HTML = """\ - - - - - -Edit Activity - - - -
-
- ← Back to site -

Edit Activity

-
-

- -
-
-

Identity

-
-
- - -
-
- - -
-
-
- - -
- -

Description

-
- - -
- -

Display

-
- -
- __STAT_CHECKBOXES__ -
-
-
- -
- - -
-
- -

Images

-
- -
- Drop images here or click to upload - -
-
-
- -
- - -
-
-
-
- - - - -""" +_TEMPLATE_PATH = Path(__file__).parent / "templates" / "edit.html" # ── Routes ──────────────────────────────────────────────────────────────────── @@ -359,13 +73,12 @@ async def edit_page(activity_id: str) -> str: f'' for s in STAT_PANELS ) - html = ( - _HTML + return ( + _TEMPLATE_PATH.read_text(encoding="utf-8") .replace("__SITE_URL__", site_url) .replace("__SPORT_OPTIONS__", sport_opts) .replace("__STAT_CHECKBOXES__", stat_cbs) ) - return html @app.get("/api/activity/{activity_id}") diff --git a/bincio/edit/templates/edit.html b/bincio/edit/templates/edit.html new file mode 100644 index 0000000..91bfb05 --- /dev/null +++ b/bincio/edit/templates/edit.html @@ -0,0 +1,283 @@ + + + + + +Edit Activity + + + +
+
+ ← Back to site +

Edit Activity

+
+

+ +
+
+

Identity

+
+
+ + +
+
+ + +
+
+
+ + +
+ +

Description

+
+ + +
+ +

Display

+
+ +
+ __STAT_CHECKBOXES__ +
+
+
+ +
+ + +
+
+ +

Images

+
+ +
+ Drop images here or click to upload + +
+
+
+ +
+ + +
+
+
+
+ + + + diff --git a/refactoring.md b/refactoring.md index 7b72d33..267baa0 100644 --- a/refactoring.md +++ b/refactoring.md @@ -490,7 +490,7 @@ def test_activity_geojson_missing_geometry(client, tmp_path, authenticated_sessi | # | Step | Status | |---|---|---| | 1 | Extract shared image utilities → `bincio/shared/images.py` | Done | -| 2 | Extract HTML template → `bincio/edit/templates/edit.html` | Not started | +| 2 | Extract HTML template → `bincio/edit/templates/edit.html` | Done | | 3 | Split `serve/server.py` into `deps.py` + `routers/*` | Not started | | 4 | Narrow broad `except Exception:` catches | Not started | diff --git a/tests/test_edit_server.py b/tests/test_edit_server.py index 3b15837..145d581 100644 --- a/tests/test_edit_server.py +++ b/tests/test_edit_server.py @@ -156,3 +156,34 @@ class TestDemEndpoint: monkeypatch.setattr(edit_server, "dem_url", "https://api.open-elevation.com") r = CLIENT.post("/api/activity/../../evil/recalculate-elevation/dem") assert r.status_code in (400, 404, 422) + + +# ── /edit/{activity_id} HTML template ──────────────────────────────────────── + +class TestEditPage: + AID = "2024-01-01T080000Z-my-ride" + + def test_returns_200_html(self): + r = CLIENT.get(f"/edit/{self.AID}") + assert r.status_code == 200 + assert r.headers["content-type"].startswith("text/html") + + def test_contains_form(self): + r = CLIENT.get(f"/edit/{self.AID}") + assert '