Add feature to restore script content to a specific version on history page

This commit is contained in:
Peter Stockings
2025-11-25 15:43:12 +11:00
parent 17518c3fcc
commit c0970835ab
5 changed files with 131 additions and 6 deletions

View File

@@ -393,3 +393,47 @@ def editor(function_id):
)
return render_template("dashboard/http_functions/editor.html", **editor_data)
@http.route("/restore/<int:function_id>", methods=["POST"])
@login_required
def restore(function_id):
try:
user_id = current_user.id
version_number = request.json.get("version_number")
if not version_number:
return jsonify({"status": "error", "message": "Version number is required"}), 400
# Verify ownership and existence of the function
http_function = db.execute(
"SELECT id FROM http_functions WHERE id = %s AND user_id = %s",
[function_id, user_id],
one=True
)
if not http_function:
return jsonify({"status": "error", "message": "Function not found"}), 404
# Fetch the content of the selected version
version_data = db.execute(
"SELECT script_content FROM http_functions_versions WHERE http_function_id = %s AND version_number = %s",
[function_id, version_number],
one=True
)
if not version_data:
return jsonify({"status": "error", "message": "Version not found"}), 404
# Update the function with the old script content
# This will trigger the database function to create a new version entry
db.execute(
"UPDATE http_functions SET script_content = %s WHERE id = %s",
[version_data["script_content"], function_id],
commit=True
)
return jsonify({"status": "success", "message": f"Restored to version {version_number}"})
except Exception as e:
print(e)
return jsonify({"status": "error", "message": str(e)}), 500