From cb3afa77fc88d74cec1f45b002de2fe0940e24e4 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Fri, 29 Mar 2024 21:20:35 +1100 Subject: [PATCH] In function editor template modify add/delete functionality so it uses htmx.ajax and updates url --- app.py | 22 +++++++++++----------- db.py | 4 ++-- templates/function_editor.html | 23 +++++++++-------------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/app.py b/app.py index fedb6a2..9c5b636 100644 --- a/app.py +++ b/app.py @@ -126,18 +126,18 @@ def get_http_function_add_form(): def create_http_function(): try: user_id = current_user.id - name = request.json.get('name') - script_content = request.json.get('script_content') - environment_info = request.json.get('environment_info') - is_public = request.json.get('is_public') - log_request = request.json.get('log_request') - log_response = request.json.get('log_response') + name = request.form.get('name') + script_content = request.form.get('script_content') + environment_info = request.form.get('environment_info') + is_public = request.form.get('is_public') + log_request = request.form.get('log_request') + log_response = request.form.get('log_response') db.create_new_http_function(user_id, name, script_content, environment_info, is_public, log_request, log_response) http_functions = db.get_http_functions_for_user(user_id) http_functions = create_http_functions_view_model(http_functions) - return render_template("dashboard/http_functions/overview.html", http_functions=http_functions) + return render_template("dashboard/http_functions/overview.html", http_functions=http_functions), 200, {"HX-Push-Url": url_for('dashboard_http_functions')} except Exception as e: print(e) return { "status": "error", "message": str(e) } @@ -179,17 +179,17 @@ def edit_http_function(function_id): print(e) return { "status": "error", "message": str(e) } -@ app.route("/dashboard/http_functions/delete", methods=["DELETE"]) +@ app.route("/dashboard/http_functions//delete", methods=["DELETE"]) @login_required -def delete_http_function(): +def delete_http_function(function_id): try: user_id = current_user.id name = request.args.get('name') - db.delete_http_function(user_id, name) + db.delete_http_function(user_id, function_id) http_functions = db.get_http_functions_for_user(user_id) http_functions = create_http_functions_view_model(http_functions) - return render_template("dashboard/http_functions/overview.html", http_functions=http_functions) + return render_template("dashboard/http_functions/overview.html", http_functions=http_functions), 200, {"HX-Push-Url": url_for('dashboard_http_functions')} except Exception as e: return jsonify({"status": 'error', "message": str(e)}), 500 diff --git a/db.py b/db.py index a2921ba..a42324f 100644 --- a/db.py +++ b/db.py @@ -80,9 +80,9 @@ class DataBase(): self.execute( 'UPDATE http_functions SET environment_info=%s, invoked_count = invoked_count + 1 WHERE user_id=%s AND NAME=%s', [json.dumps(environment_info), user_id, name], commit=True) - def delete_http_function(self, user_id, name): + def delete_http_function(self, user_id, function_id): self.execute( - 'DELETE FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], commit=True) + 'DELETE FROM http_functions WHERE user_id=%s AND id=%s', [user_id, function_id], commit=True) def add_http_function_invocation(self, http_function_id, status, request_data, response_data, logs): self.execute( diff --git a/templates/function_editor.html b/templates/function_editor.html index 977f0d7..288c73c 100644 --- a/templates/function_editor.html +++ b/templates/function_editor.html @@ -276,8 +276,8 @@