diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-02-19 18:34:39 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-02-19 18:34:39 +0200 |
| commit | cdc78ce436459047b06163f98fc661df9f2026f4 (patch) | |
| tree | 3bc6d63ee587c6dc19fdb5ad77173088b6b45262 | |
| parent | 371bbe0390113ecbf032ae9bcfc6a93470ffad67 (diff) | |
Work on stuff
| -rw-r--r-- | browser/app.py | 33 | ||||
| -rw-r--r-- | browser/static/htmp.js | 49 | ||||
| -rw-r--r-- | browser/templates/frag_form_delete.html.j2 | 4 |
3 files changed, 58 insertions, 28 deletions
diff --git a/browser/app.py b/browser/app.py index 659e7a5..be79432 100644 --- a/browser/app.py +++ b/browser/app.py @@ -180,6 +180,39 @@ def query_range(): rows = rows, ) +@app.post("/delete") +def query_delete(): + field = request.form.get("field") + if not field: raise ValueError("Field is required") + + value = request.form.get("value") + if not value: raise ValueError("Value is required") + + field_index = db_fields.index(field) + if field_index == -1: raise ValueError(f"Field '{field}' not found") + + try: + value = cast_to_value(field, value) + except ValueError as e: + return error(str(e), 400) + + rows = db.delete_by(field, cast(Value, value)) + rows = [[value_to_str(v) for v in row] for row in rows] + + htmp_target = request.form.get("htmp") or request.args.get("htmp") + if htmp_target: + return render_template("frag_results.html.j2", + field_names = ["id", "name"], + rows = rows, + ) + else: + return render_template('page_main.html.j2', + selected_form = "frag_form_delete.html.j2", + field_names = ["id", "name"], + rows = rows, + ) + + # Utils def cast_to_value(field: str, str_value: str) -> Value | None: diff --git a/browser/static/htmp.js b/browser/static/htmp.js index 62956a4..27d963b 100644 --- a/browser/static/htmp.js +++ b/browser/static/htmp.js @@ -1,41 +1,38 @@ const iframe = document.createElement("iframe"); iframe.name = "htmp"; iframe.hidden = true; -document.body.appendChild(iframe); - -document - .querySelector("iframe[name='htmp']") - .addEventListener("load", function () { - setTimeout(() => { - const cd = this.contentDocument; - const cw = this.contentWindow; - // If the server responds with an entire HTML document, replace the current document with it. - // To check whether the response is an entire page we check the presence of this very snippet. - if (cd.querySelector("iframe[name='htmp']")) { - document.documentElement.replaceWith(cd.documentElement); - // If the server responds with a fragment, replace the target element with it. - } else { - document - .querySelector(cw.location.hash || null) - ?.replaceWith(...cd.body.childNodes); - const url = new URL(cw.location.href); - url.searchParams.delete("htmp"); - url.hash = ""; - history.replaceState({}, "", url.toString()); - } - }); +iframe.addEventListener("load", function () { + setTimeout(() => { + const cd = this.contentDocument; + const cw = this.contentWindow; + // If the server responds with an entire HTML document, replace the current document with it. + // To check whether the response is an entire page we check the presence of this very snippet. + if (cd.querySelector("iframe[name='htmp']")) { + document.documentElement.replaceWith(cd.documentElement); + // If the server responds with a fragment, replace the target element with it. + } else { + document + .querySelector(cw.location.hash || null) + ?.replaceWith(...cd.body.childNodes); + const url = new URL(cw.location.href); + url.searchParams.delete("htmp"); + url.hash = ""; + history.replaceState({}, "", url.toString()); + } }); +}); +document.body.appendChild(iframe); document.querySelectorAll("form[htmp]").forEach(function (el) { - const re = el.attributes.replace.value; + const r = el.attributes.replace.value; const input = document.createElement("input"); input.type = "hidden"; input.name = "htmp"; - input.value = re; + input.value = r; el.appendChild(input); const action = new URL(el.action); - action.hash = re; + action.hash = r; el.action = action.toString(); el.target = "htmp"; }); diff --git a/browser/templates/frag_form_delete.html.j2 b/browser/templates/frag_form_delete.html.j2 index a362075..22306c1 100644 --- a/browser/templates/frag_form_delete.html.j2 +++ b/browser/templates/frag_form_delete.html.j2 @@ -1,7 +1,7 @@ <form id="form_query" action="/delete" method="post" htmp replace=results> <label for="field">Field</label> <input type="text" name="field" id="field" required> - <label for="values">Values (one per line)</label> - <textarea name="values" id="values" required></textarea> + <label for="value">Value</label> + <input type="text" name="value" id="value" required> <button type="submit">Run query</button> </form> |
