From 204000a668a84d43ef4c6956cbf925d38ff75f4d Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 16 Dec 2023 22:35:28 +1100 Subject: [PATCH] Add ability to delete http functions --- app.py | 31 +++++++++-- db.py | 4 ++ templates/client.html | 9 ++++ templates/dashboard/http_functions/edit.html | 54 +++++++++++++------- templates/dashboard/http_functions/new.html | 33 ++++++++---- 5 files changed, 97 insertions(+), 34 deletions(-) diff --git a/app.py b/app.py index cef47d7..3c7f296 100644 --- a/app.py +++ b/app.py @@ -89,10 +89,16 @@ def get_http_function_add_form(): @ app.route("/dashboard/http_functions/create", methods=["POST"]) def create_http_function(): - name = request.json.get('name') - script_content = request.json.get('script_content') - db.create_new_http_function(name, script_content) - return render_template("dashboard/http_functions/new.html", name=name, script=script_content) + try: + name = request.json.get('name') + script_content = request.json.get('script_content') + db.create_new_http_function(name, script_content) + + http_functions = db.get_http_functions() + http_functions = create_http_functions_view_model(http_functions) + return render_template("dashboard/http_functions.html", http_functions=http_functions) + except Exception as e: + return render_template("dashboard/http_functions/new.html", name=name, script=script_content) @ app.route("/dashboard/http_functions/edit_form", methods=["GET"]) def get_http_function_edit_form(): @@ -109,10 +115,25 @@ def edit_http_function(): name = request.json.get('name') script_content = request.json.get('script_content') db.edit_http_function(name, script_content) - return redirect(url_for('dashboard_http_functions')) + + http_functions = db.get_http_functions() + http_functions = create_http_functions_view_model(http_functions) + return render_template("dashboard/http_functions.html", http_functions=http_functions) except Exception as e: return render_template("dashboard/http_functions/edit.html", name=name, script=script_content) +@ app.route("/dashboard/http_functions/delete", methods=["DELETE"]) +def delete_http_function(): + try: + name = request.args.get('name') + db.delete_http_function(name) + + http_functions = db.get_http_functions() + http_functions = create_http_functions_view_model(http_functions) + return render_template("dashboard/http_functions.html", http_functions=http_functions) + except Exception as e: + return jsonify({'error': str(e)}), 500 + @ app.route("/dashboard/timer_functions", methods=["GET"]) def dashboard_timer_functions(): return render_template("dashboard/timer_functions.html") diff --git a/db.py b/db.py index 04107de..77335ea 100644 --- a/db.py +++ b/db.py @@ -63,3 +63,7 @@ class DataBase(): def edit_http_function(self, name, script_content): self.execute( 'UPDATE http_functions SET script_content=%s WHERE NAME=%s', [script_content, name], commit=True) + + def delete_http_function(self, name): + self.execute( + 'DELETE FROM http_functions WHERE NAME=%s', [name], commit=True) diff --git a/templates/client.html b/templates/client.html index 4eb03f6..8a0b583 100644 --- a/templates/client.html +++ b/templates/client.html @@ -2,6 +2,15 @@ {% block content %} +
+

Try: {{ name }}

+ +
+
+ -
{{ script }}
+ +
+ + + + +
+
@@ -135,7 +153,7 @@ }); document.querySelector('#add-http-function').addEventListener('click', () => { - let name = document.querySelector('#function-name').value; + let name = '{{ name }}'; let script_content = editor.getValue(); fetch("{{ url_for('edit_http_function') }}", { diff --git a/templates/dashboard/http_functions/new.html b/templates/dashboard/http_functions/new.html index c94fc36..e7dfc6b 100644 --- a/templates/dashboard/http_functions/new.html +++ b/templates/dashboard/http_functions/new.html @@ -1,5 +1,10 @@

New HTTP function

+
@@ -29,22 +34,23 @@ -
{{ script }}
+ + +
@@ -144,5 +150,10 @@ }, body: JSON.stringify({ name, script_content }), }) + .then(response => response.text()) + .then(text => { + document.querySelector('#container').innerHTML = text + htmx.process(htmx.find('#container')) + }) }) \ No newline at end of file