diff --git a/app.py b/app.py index 709b9ee..cef47d7 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ import os -from flask import Flask, Response, jsonify, render_template, request +from flask import Flask, Response, jsonify, redirect, render_template, request, url_for import jinja_partials from jinja2_fragments import render_block from flask_htmx import HTMX @@ -94,6 +94,25 @@ def create_http_function(): db.create_new_http_function(name, script_content) 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(): + name = request.args.get('name') + http_function = db.get_http_function(name) + if not http_function: + return jsonify({'error': 'Function not found'}), 404 + script = http_function['script_content'] + return render_template("dashboard/http_functions/edit.html", name=name, script=script) + +@ app.route("/dashboard/http_functions/edit", methods=["POST"]) +def edit_http_function(): + try: + 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')) + except Exception as e: + return render_template("dashboard/http_functions/edit.html", name=name, script=script_content) + @ 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 897a787..04107de 100644 --- a/db.py +++ b/db.py @@ -59,3 +59,7 @@ class DataBase(): def create_new_http_function(self, name, script_content): self.execute( 'INSERT INTO http_functions (NAME, script_content) VALUES (%s, %s)', [name, script_content], commit=True) + + 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) diff --git a/templates/dashboard/http_functions.html b/templates/dashboard/http_functions.html index a104f90..5befef5 100644 --- a/templates/dashboard/http_functions.html +++ b/templates/dashboard/http_functions.html @@ -48,8 +48,8 @@ diff --git a/templates/dashboard/http_functions/edit.html b/templates/dashboard/http_functions/edit.html new file mode 100644 index 0000000..33cfb72 --- /dev/null +++ b/templates/dashboard/http_functions/edit.html @@ -0,0 +1,154 @@ +