From 1a496d244105a1bbdd0e304667732a4fc4b34727 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Wed, 27 Mar 2024 14:03:52 +1100 Subject: [PATCH] Fix issue where on page refresh when on function edit/new sub pages you would be redirected back to dashboard --- app.py | 54 +++++++++++-------- db.py | 5 ++ templates/dashboard.html | 2 + .../dashboard/http_functions/client.html | 7 +-- templates/dashboard/http_functions/edit.html | 16 ++++-- .../dashboard/http_functions/header.html | 19 +++---- templates/dashboard/http_functions/logs.html | 13 +++-- templates/dashboard/http_functions/new.html | 9 +++- .../dashboard/http_functions/overview.html | 9 ++-- templates/function_editor.html | 7 ++- 10 files changed, 89 insertions(+), 52 deletions(-) diff --git a/app.py b/app.py index 585cc6d..211bb16 100644 --- a/app.py +++ b/app.py @@ -95,16 +95,6 @@ def map_isolator_response_to_flask_response(response): def home(): return render_template("home.html", name='Try me', script=DEFAULT_SCRIPT, environment_info=DEFAULT_ENVIRONMENT) -@ app.route("/client//", methods=["GET"]) -@login_required -def client(user_id, function): - http_function = db.get_http_function(user_id, function) - if not http_function: - return jsonify({'error': 'Function not found'}), 404 - - http_function = create_http_function_view_model(http_function) - return render_template("dashboard/http_functions/client.html", **http_function) - @ app.route("/dashboard", methods=["GET"]) @login_required def dashboard(): @@ -125,8 +115,11 @@ def dashboard_http_functions(): @login_required def get_http_function_add_form(): user_id = current_user.id + if htmx: + return render_block(app.jinja_env, 'dashboard/http_functions/new.html', 'page', user_id=user_id, name=DEFAULT_FUNCTION_NAME, script=DEFAULT_SCRIPT, environment_info=DEFAULT_ENVIRONMENT, is_public=False, log_request=True, log_response=False) return render_template("dashboard/http_functions/new.html", user_id=user_id, name=DEFAULT_FUNCTION_NAME, script=DEFAULT_SCRIPT, environment_info=DEFAULT_ENVIRONMENT, is_public=False, log_request=True, log_response=False) + @ app.route("/dashboard/http_functions/create", methods=["POST"]) @login_required def create_http_function(): @@ -148,20 +141,23 @@ def create_http_function(): print(e) return { "status": "error", "message": str(e) } -@ app.route("/dashboard/http_functions/edit_form", methods=["GET"]) +@ app.route("/dashboard/http_functions//edit_form", methods=["GET"]) @login_required -def get_http_function_edit_form(): +def get_http_function_edit_form(function_id): user_id = current_user.id - name = request.args.get('name') - http_function = db.get_http_function(user_id, name) + http_function = db.get_http_function_by_id(user_id, function_id) if not http_function: return jsonify({'error': 'Function not found'}), 404 + name = http_function['name'] script = http_function['script_content'] environment_info = json.dumps(http_function['environment_info'], indent=2) is_public = http_function['is_public'] log_request = http_function['log_request'] log_response = http_function['log_response'] - return render_template("dashboard/http_functions/edit.html", user_id=user_id, name=name, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response) + + if htmx: + return render_block(app.jinja_env, 'dashboard/http_functions/edit.html', 'page', user_id=user_id, function_id=function_id, name=name, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response) + return render_template("dashboard/http_functions/edit.html", user_id=user_id, name=name, function_id=function_id, script=script, environment_info=environment_info, is_public=is_public, log_request=log_request, log_response=log_response) @ app.route("/dashboard/http_functions/edit", methods=["POST"]) @login_required @@ -197,19 +193,31 @@ def delete_http_function(): except Exception as e: return jsonify({"status": 'error', "message": str(e)}), 500 -@ app.route("/dashboard/http_functions/logs", methods=["GET"]) +@ app.route("/dashboard/http_functions//logs", methods=["GET"]) @login_required -def get_http_function_logs(): +def get_http_function_logs(function_id): user_id = current_user.id - name = request.args.get('name') - http_function = db.get_http_function(user_id, name) + http_function = db.get_http_function_by_id(user_id, function_id) + if not http_function: + return jsonify({'error': 'Function not found'}), 404 + name = http_function['name'] + http_function_invocations = db.get_http_function_invocations(function_id) + if htmx: + return render_block(app.jinja_env, 'dashboard/http_functions/logs.html', 'page', user_id=user_id, function_id=function_id, name=name, http_function_invocations=http_function_invocations) + return render_template("dashboard/http_functions/logs.html", user_id=user_id, name=name, function_id=function_id, http_function_invocations=http_function_invocations) + +@ app.route("/http_functions//client", methods=["GET"]) +@login_required +def client(function_id): + user_id = current_user.id + http_function = db.get_http_function_by_id(user_id, function_id) if not http_function: return jsonify({'error': 'Function not found'}), 404 - http_function_id = http_function['id'] - http_function_invocations = db.get_http_function_invocations(http_function_id) - return render_template("dashboard/http_functions/logs.html", user_id=user_id, name=name, http_function_invocations=http_function_invocations) - + http_function = create_http_function_view_model(http_function) + if htmx: + return render_block(app.jinja_env, 'dashboard/http_functions/client.html', 'page', function_id=function_id, **http_function) + return render_template("dashboard/http_functions/client.html", function_id=function_id, **http_function) @ app.route("/dashboard/timer_functions", methods=["GET"]) @login_required diff --git a/db.py b/db.py index 89a2365..a7d8609 100644 --- a/db.py +++ b/db.py @@ -57,6 +57,11 @@ class DataBase(): 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True) return http_function + def get_http_function_by_id(self, user_id, http_function_id): + http_function = self.execute( + 'SELECT id, user_id, NAME, script_content, invoked_count, environment_info, is_public, log_request, log_response FROM http_functions WHERE user_id=%s AND id=%s', [user_id, http_function_id], one=True) + return http_function + def create_new_http_function(self, user_id, name, script_content, environment_info, is_public, log_request, log_response): self.execute( 'INSERT INTO http_functions (user_id, NAME, script_content, environment_info, is_public, log_request, log_response) VALUES (%s, %s, %s, %s, %s, %s, %s)', diff --git a/templates/dashboard.html b/templates/dashboard.html index 55da0c9..c487aa6 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -89,7 +89,9 @@
+ {% block page %} {{ render_partial('dashboard/http_functions/overview.html', http_functions=http_functions) }} + {% endblock %}
diff --git a/templates/dashboard/http_functions/client.html b/templates/dashboard/http_functions/client.html index aa33e57..372f419 100644 --- a/templates/dashboard/http_functions/client.html +++ b/templates/dashboard/http_functions/client.html @@ -1,8 +1,9 @@ -{% extends 'base.html' %} +{% extends 'dashboard.html' %} -{% block content %} +{% block page %} -{{ render_partial('dashboard/http_functions/header.html', title='Try', user_id=user_id, name=name, +{{ render_partial('dashboard/http_functions/header.html', title='Try', user_id=user_id, function_id=function_id, +name=name, show_refresh=False, show_link=False, show_edit_form=True, show_client=True, show_logs=True) }}
diff --git a/templates/dashboard/http_functions/edit.html b/templates/dashboard/http_functions/edit.html index f413a1e..c3cb664 100644 --- a/templates/dashboard/http_functions/edit.html +++ b/templates/dashboard/http_functions/edit.html @@ -1,5 +1,13 @@ -{{ render_partial('dashboard/http_functions/header.html', title='Update', user_id=user_id, name=name, -refresh_url=url_for('get_http_function_edit_form', name=name), show_logs=True, show_client=True) }} +{% extends 'dashboard.html' %} -{{ render_partial('function_editor.html', name=name, script=script, environment_info=environment_info, -is_public=is_public, log_request=log_request, log_response=log_response, is_edit=True) }} \ No newline at end of file +{% block page %} + +{{ render_partial('dashboard/http_functions/header.html', title='Update', user_id=user_id, function_id=function_id, +name=name, +refresh_url=url_for('get_http_function_edit_form', function_id=function_id), show_logs=True, show_client=True) }} + +{{ render_partial('function_editor.html', function_id=function_id, name=name, script=script, +environment_info=environment_info, +is_public=is_public, log_request=log_request, log_response=log_response, is_edit=True) }} + +{% endblock %} \ No newline at end of file diff --git a/templates/dashboard/http_functions/header.html b/templates/dashboard/http_functions/header.html index 6010e38..bdacaec 100644 --- a/templates/dashboard/http_functions/header.html +++ b/templates/dashboard/http_functions/header.html @@ -8,7 +8,7 @@ {% if show_refresh|default(true, false) %}
+ hx-swap="innerHTML" hx-push-url="true"> @@ -20,8 +20,9 @@ {% endif %} {% if show_logs|default(false, true) %} -
+
+
+ hx-get="{{ url_for('get_http_function_edit_form', function_id=function_id) }}" hx-target="#container" + hx-swap="innerHTML" hx-push-url="true">
@@ -39,4 +44,6 @@ refresh_url=url_for('get_http_function_logs', name=name), show_edit_form=True, s {% endfor %}
{% endfor %} -
\ No newline at end of file +
+ +{% endblock %} \ No newline at end of file diff --git a/templates/dashboard/http_functions/new.html b/templates/dashboard/http_functions/new.html index 73fa762..c57bef4 100644 --- a/templates/dashboard/http_functions/new.html +++ b/templates/dashboard/http_functions/new.html @@ -1,8 +1,13 @@ +{% extends 'dashboard.html' %} + +{% block page %} + {{ render_partial('dashboard/http_functions/header.html', title='New HTTP function', user_id=user_id, show_name=False, show_refresh=False, show_logs=False, show_client=False, show_link=False) }} - {{ render_partial('function_editor.html', name=name, script=script, environment_info=environment_info, -is_public=is_public, log_request=log_request, log_response=log_response, is_add=True) }} \ No newline at end of file +is_public=is_public, log_request=log_request, log_response=log_response, is_add=True) }} + +{% endblock %} \ No newline at end of file diff --git a/templates/dashboard/http_functions/overview.html b/templates/dashboard/http_functions/overview.html index 5cc982e..75e8e59 100644 --- a/templates/dashboard/http_functions/overview.html +++ b/templates/dashboard/http_functions/overview.html @@ -2,7 +2,8 @@

HTTP functions

diff --git a/templates/function_editor.html b/templates/function_editor.html index 2d39a6a..20f6908 100644 --- a/templates/function_editor.html +++ b/templates/function_editor.html @@ -277,7 +277,7 @@