From 371bbe0390113ecbf032ae9bcfc6a93470ffad67 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 17 Feb 2025 19:32:08 +0200 Subject: Implement range query --- browser/app.py | 71 ++++++++++++++++++++++++++++-- browser/templates/frag_form_delete.html.j2 | 7 +++ browser/templates/frag_form_find.html.j2 | 2 +- browser/templates/frag_form_range.html.j2 | 20 +++++++-- browser/templates/frag_form_upsert.html.j2 | 5 +++ 5 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 browser/templates/frag_form_delete.html.j2 create mode 100644 browser/templates/frag_form_upsert.html.j2 diff --git a/browser/app.py b/browser/app.py index 92fcbbf..659e7a5 100644 --- a/browser/app.py +++ b/browser/app.py @@ -5,6 +5,7 @@ import os import sys import traceback import tempfile +from typing import cast from flask import Flask, render_template, request from flask_compress import Compress # type: ignore @@ -105,13 +106,73 @@ def query_find(): rows = rows, ) -@app.get("/range") -def page_range(): - rows = db.range_by("id", Bound.unbounded(), Bound.unbounded(), limit=100) +@app.post("/range") +def query_range(): + field = request.form.get("field") + if not field: raise ValueError("Field is required") + + from_type = request.form.get("from_type") + if not from_type: raise ValueError("From type is required") + + to_type = request.form.get("to_type") + if not to_type: raise ValueError("To type is required") + + field_index = db_fields.index(field) + if field_index == -1: raise ValueError(f"Field '{field}' not found") + + match from_type: + case "unbounded": + bound_lower = Bound.unbounded() + case "included": + from_value = request.form.get("from_value") + try: + if not from_value: raise ValueError("From value is required") + from_value = cast_to_value(field, from_value) + except ValueError as e: + return error(str(e), 400) + bound_lower = Bound.included(cast(Value, from_value)) + case "excluded": + from_value = request.form.get("from_value") + try: + if not from_value: raise ValueError("From value is required") + from_value = cast_to_value(field, from_value) + except ValueError as e: + return error(str(e), 400) + bound_lower = Bound.excluded(cast(Value, from_value)) + case _: + return error("Invalid from type", 400) + + match to_type: + case "unbounded": + bound_upper = Bound.unbounded() + case "included": + to_value = request.form.get("to_value") + try: + if not to_value: raise ValueError("To value is required") + to_value = cast_to_value(field, to_value) + except ValueError as e: + return error(str(e), 400) + bound_upper = Bound.included(cast(Value, to_value)) + case "excluded": + to_value = request.form.get("to_value") + try: + if not to_value: raise ValueError("To value is required") + to_value = cast_to_value(field, to_value) + except ValueError as e: + return error(str(e), 400) + bound_upper = Bound.excluded(cast(Value, to_value)) + case _: + return error("Invalid to type", 400) + + tagged_rows = db.range_by(field, bound_lower, bound_upper, limit=100) + rows = [[value_to_str(v) for v in row] for row in tagged_rows] htmp_target = request.form.get("htmp") or request.args.get("htmp") if htmp_target: - return render_template("frag_form_range.html.j2") + 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_range.html.j2", @@ -119,6 +180,8 @@ def page_range(): rows = rows, ) +# Utils + def cast_to_value(field: str, str_value: str) -> Value | None: str_value = str_value.strip() if str_value == "": return None diff --git a/browser/templates/frag_form_delete.html.j2 b/browser/templates/frag_form_delete.html.j2 new file mode 100644 index 0000000..a362075 --- /dev/null +++ b/browser/templates/frag_form_delete.html.j2 @@ -0,0 +1,7 @@ +
+ + + + + +
diff --git a/browser/templates/frag_form_find.html.j2 b/browser/templates/frag_form_find.html.j2 index f527b45..cadd44b 100644 --- a/browser/templates/frag_form_find.html.j2 +++ b/browser/templates/frag_form_find.html.j2 @@ -1,4 +1,4 @@ -
+ diff --git a/browser/templates/frag_form_range.html.j2 b/browser/templates/frag_form_range.html.j2 index 96fe8a8..84b8024 100644 --- a/browser/templates/frag_form_range.html.j2 +++ b/browser/templates/frag_form_range.html.j2 @@ -1,9 +1,21 @@ - - - - + + + + + + + +
diff --git a/browser/templates/frag_form_upsert.html.j2 b/browser/templates/frag_form_upsert.html.j2 new file mode 100644 index 0000000..68820f5 --- /dev/null +++ b/browser/templates/frag_form_upsert.html.j2 @@ -0,0 +1,5 @@ +
+ + + +
-- cgit v1.3